def serial_handler(connect): if connect == '1': # print 'js is asking to connect serial' if not SerialManager.is_connected(): try: global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND) + '<br>' time.sleep(1.0) # allow some time to receive a prompt/welcome SerialManager.flush_input() SerialManager.flush_output() return ret except serial.SerialException: SERIAL_PORT = None print "Failed to connect to serial." return "" elif connect == '0': # print 'js is asking to close serial' if SerialManager.is_connected(): if SerialManager.close(): return "1" else: return "" elif connect == "2": # print 'js is asking if serial connected' if SerialManager.is_connected(): return "1" else: return "" else: print 'ambigious connect request from js: ' + connect return ""
def serial_handler(connect): if connect == '1': # print 'js is asking to connect serial' if not SerialManager.is_connected(): try: global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device( GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND) + '<br>' time.sleep(1.0) # allow some time to receive a prompt/welcome SerialManager.flush_input() SerialManager.flush_output() return ret except serial.SerialException: SERIAL_PORT = None print "Failed to connect to serial." return "" elif connect == '0': # print 'js is asking to close serial' if SerialManager.is_connected(): if SerialManager.close(): return "1" else: return "" elif connect == "2": # print 'js is asking if serial connected' if SerialManager.is_connected(): return "1" else: return "" else: print 'ambigious connect request from js: ' + connect return ""
def get_status(): if args.raspberrypi: powertimer.reset() status = copy.deepcopy(SerialManager.get_hardware_status()) status['serial_connected'] = SerialManager.is_connected() print "Connected: %d" % SerialManager.is_connected() status['lasaurapp_version'] = VERSION global user_approved global user_admin global current_user if args.disable_rfid: return json.dumps(status) card_id = reader.getid() print "Card ID %s" % card_id username = '' global current_cardid if len(card_id) == 0: print "No card inserted" username = '******' user_approved = False if current_user != '': logger.log(current_user, 'Card removed') current_user = '' elif len(card_id) == 12: if not card_id in card_data: print "Card not found" username = '******' user_approved = False if card_id != current_cardid: logger.log(card_id, 'Unknown card') else: print "Card found" data = card_data[card_id] username = data['name'] if data['approved']: user_approved = True else: user_approved = False if data['admin']: user_admin = True else: user_admin = False if current_user == '': logger.log(username, 'Card inserted') current_user = username print "Approved: %s" % user_approved current_cardid = card_id else: print "Bad length: %d" % len(card_id) status['username'] = username global shutdown_msg status['shutdown_msg'] = shutdown_msg shutdown_msg = '' return json.dumps(status)
def gcode_handler(gcode_line): if SerialManager.is_connected(): print gcode_line SerialManager.queue_for_sending(gcode_line) return "Queued for sending." else: return ""
def job_submit_handler(): job_data = request.forms.get('job_data') if job_data and SerialManager.is_connected(): SerialManager.queue_gcode(job_data) return "__ok__" else: return "serial disconnected"
def flash_firmware_handler(): if SerialManager.is_connected(): SerialManager.close() global SERIAL_PORT, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX) flash_upload(SERIAL_PORT, resources_dir()) return '<h2>flashing finished!</h2> Check Log window for possible errors.<br><a href="/">return</a>'
def flash_firmware_handler(firmware_file=FIRMWARE): global SERIAL_PORT, GUESS_PREFIX return_code = 1 if SerialManager.is_connected(): SerialManager.close() # get serial port by url argument # e.g: /flash_firmware?port=COM3 if 'port' in request.GET.keys(): serial_port = request.GET['port'] if serial_port[:3] == "COM" or serial_port[:4] == "tty.": SERIAL_PORT = serial_port # get serial port by enumeration method # currenty this works on windows only for updating the firmware if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) # resort to brute force methode # find available com ports and try them all if not SERIAL_PORT: comport_list = SerialManager.list_devices(BITSPERSECOND) for port in comport_list: print "Trying com port: " + port return_code = flash_upload(port, resources_dir(), firmware_file, HARDWARE) if return_code == 0: print "Success with com port: " + port SERIAL_PORT = port break else: return_code = flash_upload(SERIAL_PORT, resources_dir(), firmware_file, HARDWARE) ret = [] ret.append('Using com port: %s<br>' % (SERIAL_PORT)) ret.append('Using firmware: %s<br>' % (firmware_file)) if return_code == 0: print "SUCCESS: Arduino appears to be flashed." ret.append('<h2>Successfully Flashed!</h2><br>') ret.append('<a href="/">return</a>') return ''.join(ret) else: print "ERROR: Failed to flash Arduino." ret.append( '<h2>Flashing Failed!</h2> Check terminal window for possible errors. ' ) ret.append( 'Most likely LasaurApp could not find the right serial port.') ret.append('<br><a href="/flash_firmware/' + firmware_file + '">try again</a> or <a href="/">return</a><br><br>') if os.name != 'posix': ret.append( 'If you know the COM ports the Arduino is connected to you can specifically select it here:' ) for i in range(1, 13): ret.append( '<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i)) return ''.join(ret)
def job_submit_handler(): job_data = request.forms.get('job_data') if job_data and SerialManager.is_connected(): lines = job_data.split('\n') print "Adding to queue %s lines" % len(lines) for line in lines: SerialManager.queue_gcode_line(line) return "__ok__" else: return "serial disconnected"
def gcode_submit_handler(): gcode_program = request.forms.get('gcode_program') if gcode_program and SerialManager.is_connected(): lines = gcode_program.split('\n') print "Adding to queue %s lines" % len(lines) for line in lines: SerialManager.queue_gcode_line(line) return "__ok__" else: return "serial disconnected"
def gcode_submit_handler(): gcode_program = request.forms.get("gcode_program") if gcode_program and SerialManager.is_connected(): lines = gcode_program.split("\n") print "Adding to queue %s lines" % len(lines) for line in lines: SerialManager.queue_for_sending(line) return "Queued for sending." else: return ""
def job_submit_handler(): job_data = request.forms.get('job_data') global user_approved global current_user if not user_approved and job_data[0] != "!": print 'User not approved' return 'Access denied' if job_data and SerialManager.is_connected(): SerialManager.queue_gcode(job_data) logger.log(current_user, 'Run job: '+re.sub('[\s+]', ' ', job_data)[:50]) return "__ok__" else: return "serial disconnected"
def job_submit_handler(): job_data = request.forms.get('gcode_program') if not job_data: return HTTPResponse(status=400) if SerialManager.is_connected(): lines = job_data.split('\n') print "Adding to queue %s lines" % len(lines) for line in lines: SerialManager.queue_gcode_line(line) return "__ok__" else: return HTTPResponse(status=502, body="serial disconnected")
def flash_firmware_handler(firmware_file=FIRMWARE): global user_approved global user_admin if not user_approved and user_admin: return 'Access denied' logger.log(current_user, 'Flashing ATMEGA') return_code = 1 if SerialManager.is_connected(): SerialManager.close() # get serial port by url argument # e.g: /flash_firmware?port=COM3 if 'port' in request.GET.keys(): serial_port = request.GET['port'] if serial_port[:3] == "COM" or serial_port[:4] == "tty.": SERIAL_PORT = serial_port # get serial port by enumeration method # currenty this works on windows only for updating the firmware if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) # resort to brute force methode # find available com ports and try them all if not SERIAL_PORT: comport_list = SerialManager.list_devices(BITSPERSECOND) for port in comport_list: print "Trying com port: " + port return_code = flash_upload(port, resources_dir(), firmware_file, HARDWARE) if return_code == 0: print "Success with com port: " + port SERIAL_PORT = port break else: return_code = flash_upload(SERIAL_PORT, resources_dir(), firmware_file, HARDWARE) ret = [] ret.append('Using com port: %s<br>' % (SERIAL_PORT)) ret.append('Using firmware: %s<br>' % (firmware_file)) if return_code == 0: print "SUCCESS: Arduino appears to be flashed." ret.append('<h2>Successfully Flashed!</h2><br>') ret.append('<a href="/">return</a>') return ''.join(ret) else: print "ERROR: Failed to flash Arduino." ret.append('<h2>Flashing Failed!</h2> Check terminal window for possible errors. ') ret.append('Most likely LasaurApp could not find the right serial port.') ret.append('<br><a href="/flash_firmware/'+firmware_file+'">try again</a> or <a href="/">return</a><br><br>') if os.name != 'posix': ret. append('If you know the COM ports the Arduino is connected to you can specifically select it here:') for i in range(1,13): ret. append('<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i)) return ''.join(ret)
def get_status(): status = copy.deepcopy(SerialManager.get_hardware_status()) status['serial_connected'] = SerialManager.is_connected() if HARDWARE == 'raspberrypi': status['power'] = RPiPowerControl.get_power_status() status['assist_air'] = RPiPowerControl.get_assist_air_status() RPiPowerControl.set_process_status(status['serial_connected'] and not status['ready']) else: status['power'] = 1; status['assist_air'] = 1; status['lasaurapp_version'] = VERSION status['admin'] = admin_check() status['user'] = accessUser status['accounts'] = accountsTable status['statistics'] = statistics return status
def flash_firmware_handler(): global SERIAL_PORT, GUESS_PREFIX return_code = 1 if SerialManager.is_connected(): SerialManager.close() # get serial port by url argument # e.g: /flash_firmware?port=COM3 if "port" in request.GET.keys(): serial_port = request.GET["port"] if serial_port[:3] == "COM" or serial_port[:4] == "tty.": SERIAL_PORT = serial_port # get serial port by enumeration method # currenty this works on windows only for updating the firmware if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) # resort to brute force methode # find available com ports and try them all if not SERIAL_PORT: comport_list = SerialManager.list_devices(BITSPERSECOND) for port in comport_list: print "Trying com port: " + port return_code = flash_upload(port, resources_dir()) if return_code == 0: print "Success with com port: " + port SERIAL_PORT = port break else: return_code = flash_upload(SERIAL_PORT, resources_dir()) ret = [] ret.append("Using com port: %s<br>" % (SERIAL_PORT)) if return_code == 0: print "SUCCESS: Arduino appears to be flashed." ret.append("<h2>Successfully Flashed!</h2><br>") ret.append('<a href="/">return</a>') return "".join(ret) else: SERIAL_PORT = None print "ERROR: Failed to flash Arduino." ret.append("<h2>Flashing Failed!</h2> Check Log window for possible errors. ") ret.append('Most likely LasaurApp could not find the right serial port.<br><a href="/">return</a><br><br>') if os.name != "posix": ret.append("If you know the COM ports the Arduino is connected to you can specifically select it here:") for i in range(1, 13): ret.append('<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i)) return "".join(ret)
def get_status(): status = copy.deepcopy(SerialManager.get_hardware_status()) status['serial_connected'] = SerialManager.is_connected() return json.dumps(status)
def get_status(): status = copy.deepcopy(SerialManager.get_hardware_status()) status['serial_connected'] = SerialManager.is_connected() status['lasaurapp_version'] = VERSION return json.dumps(status)
SERIAL_PORT = args.port print "Using serial device '" + SERIAL_PORT + "' from command line." else: print 'Please select a serialport via args' if os.name == 'nt': #sys.platform == 'win32': GUESS_PREFIX = "Arduino" elif os.name == 'posix': if sys.platform == "linux" or sys.platform == "linux2": GUESS_PREFIX = "2341" # match by arduino VID else: GUESS_PREFIX = "tty.usbmodem" else: GUESS_PREFIX = "no prefix" if __name__ == '__main__': #if not SERIAL_PORT: # SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) while SerialManager.is_connected(): command = raw_input("Input your command>>>") if len(command) != 0: SerialManager.read_existing() #clear rx_buffer if SerialManager.write(command + '\r'): str = SerialManager.read_to('\r') print(str) else: print("write error!") break print("Port closed!")
def get_status(): status = copy.deepcopy(SerialManager.get_hardware_status()) status["serial_connected"] = SerialManager.is_connected() return json.dumps(status)
def get_status(): status = copy.deepcopy(tm.status) status['serial_connected'] = SerialManager.is_connected() return json.dumps(status)