def readSensor(serial): try: # request data 21 serial.flushInput() string = "5A E8 5A DF 5A CB" #Long range - Fast mode - Start periodic readout cmd_bytes = bytearray.fromhex(string) ser.write(cmd_bytes) msg_b = serial.read(8) encoded = str(base64.b16encode(msg_b)) encoded = encoded.replace("b'", "") encoded = encoded.replace("'", "") B = read_hex(encoded) #print(B) return B except (Exception, KeyboardInterrupt) as e: print('sensor communication broke down') print(str(e)) serial.flushInput() time.sleep(0.2) serial.flushOutput() time.sleep(0.2) serial.close() # Only executes once the loop exits sys.exit()
def readSensor(serial): try: # request data 21 serial.flushInput() string = "5A E8 5A DF 5A CB" #Long range - Fast mode - Start periodic readout cmd_bytes = bytearray.fromhex(string) ser.write(cmd_bytes) msg_b = serial.read(8) encoded = str(base64.b16encode(msg_b)) encoded = encoded.replace("b'","") encoded = encoded.replace("'","") B = read_hex(encoded) #print(B) return B except KeyboardInterrupt: print("User interrupt encountered. Exiting...") time.sleep(3) serial.flushInput() serial.flushOutput() serial.close() sys.exit() except: # for all other kinds of error, but not specifying which one print("Device disconnected") time.sleep(3) serial.flushInput() serial.flushOutput() serial.close() sys.exit()
def readSensor(serial): try: # request data 21 serial.flushInput() serial.write('90 232'.encode('utf8')) serial.write('90 223'.encode('utf8')) serial.write('90 203'.encode('utf8')) #print(ser.out_waiting) msg_b = serial.read(8) encoded = str(base64.b16encode(msg_b)) encoded = encoded.replace("b'","") encoded = encoded.replace("'","") B = read_hex(encoded) #print(B) return B except (Exception,KeyboardInterrupt) as e: print('sensor communication broke down') print(str(e)) serial.flushInput() time.sleep(0.2) serial.flushOutput() time.sleep(0.2) serial.close() # Only executes once the loop exits sys.exit()
def flush(): global serial print "FLUSH SERIAL" #serial.flushInput() serial.flushOutput()
def on_connect(): print('连接成功') if serial.isOpen(): time.sleep(8) serial.readlines() serial.flushOutput() printmessage_json = { 'id': 1, 'on_off': 1, 'status': { 'chambertemperature': 0, 'bedtemperature': 0 }, 'printing': ps.printing, 'endprint': ps.endprint, 'startprint': ps.startprint } sio.emit('status', printmessage_json) print("打印机上线,发送打印机状态:") print(printmessage_json) else: printmessage_json = { 'id': 1, 'on_off': 0, 'status': { 'chambertemperature': 0, 'bedtemperature': 0, }, 'printing': ps.printing, 'endprint': ps.endprint, 'startprint': ps.startprint } sio.emit('status', printmessage_json) print("打印机连接失败") print(printmessage_json)
def send(self,serial,data): # send it print " ENCRYPTED: %s" % data.encode('hex') data = b64encode ( data ) print " ENCODED: %s" % data serial.write(data + '\x00'); serial.flushOutput() print "\n"
def send(self, serial, data): # send it print " ENCRYPTED: %s" % data.encode('hex') data = b64encode(data) print " ENCODED: %s" % data serial.write(data + '\x00') serial.flushOutput() print "\n"
def sendCtrlC(serial): global reStr tmpReStr = reStr reStr = r"^.*()+.*$" # Ctrl-C => 0x03 cmd = chr(0x03).encode("gbk") serial.write(cmd) serial.flushOutput() delayedWork = threading.Thread(target=setReStrAfter, args=(tmpReStr, fullLogTime)) delayedWork.start()
def ASK_Slave(serial, cmd_data): global recv_data, recv_n global abs_path timeout_count = 0 timeout_total = 0 print 'recv_n', recv_n print 'recv_data_B', recv_data while True: print "Motor is moving: ", timeout_count, " times" time.sleep(0.5) recv_n = serial.inWaiting() print cmd_data, '&& recv_n:', recv_n if recv_n >= 2: recv_data = serial.read(recv_n) print recv_n, ' && ', recv_data serial.flushInput() if 'OK' in recv_data and cmd_data[0] in recv_data: print 'recv_data_A', recv_data, '+', recv_n print 'Moving Finished.' recv_n = 0 recv_data = 'N' serial.flushOutput() time.sleep(0.5) # 1 # Journal = open(abs_path,'a') # Journal.write(cmd_data[0] + "->") # Journal.close() break else: print "Wrong Control,Break!" recv_n = 0 recv_data = 'N' serial.flushOutput() timeout_count += 1 if timeout_count >= 600: print "No response Slave,Repeat send cmd please." serial.write(cmd_data) timeout_total += timeout_count timeout_count = 0 Journal = open(abs_path, 'a') if timeout_total <= 600: Journal.write("\nSlave No response for CMD:" + cmd_data[0] + " " + str(timeout_total) + " times,Repeat Send CMD...\n") else: Journal.write("\nSlave No response for CMD:" + cmd_data[0] + " " + str(timeout_total) + " times,Shutdown System...\n") Journal.write("System died at: " + Get_time_str(':') + '.\n') os.system('sudo shudown -h now') Journal.close()
def read_hex_from_ee(address): serial.flushOutput() serial.flushInput() msg = '\x02' msg += dec2hex(address) if serial.isOpen(): serial.write(msg) else: print 'Error, the serial port is not open.' return value = serial.readline() if value == '': print 'Error, did not receive response from the Micro Controller.' return None value = value.strip() return value
def tagRead(): import serial try: serial= serial.Serial("/dev/ttyACM0", baudrate=9600) n=0 serial.flushInput() serial.flushOutput() while True: data=serial.readline() n=n+1 if data[0:3]=="ISO" and n>3: myString1=data.find('[')+1 myString2=data.find(',') serial.flush() serial.close() return data[myString1:myString2] except Exception as e: return 0
def on_connect(): global printing global endprint global startprint print('connection established') if serial.isOpen(): time.sleep(8) serial.readlines() serial.flushOutput() serial.write("M105\n".encode('ascii') ) time.sleep(0.5) tem_message=serial.readline() time.sleep(0.5) printmessage_json = { 'id': 1, 'on_off': 1, 'statue': { 'chambertemperature':random.randint(1,20), 'bedtemperature': random.randint(1,20) }, 'printing':printing, 'endprint':endprint, 'startprint':startprint } sio.emit('state', printmessage_json) print("打印机上线") print(printmessage_json) else: printmessage_json = { 'id': 1, 'on_off': 0, 'statue': { 'chambertemperature':'', 'bedtemperature': '', }, 'printing':printing, 'endprint':endprint, 'startprint':startprint } sio.emit('state', printmessage_json) print("打印机连接失败") print(printmessage_json)
def handleCmd(serial, command, fileHandler, filterFileHandler=None): global reStr if command == "": cmd = "\n".encode("gbk") serial.write(cmd) serial.flushOutput() elif command[0] == ':': handleSettingCmd(command[1:], serial, fileHandler, filterFileHandler) else: tmpReStr = reStr reStr = r"^.*()+.*$" cmd = command + "\n" cmd = cmd.encode("gbk") # write to file fileHandler.write(command + "\n") if filterFileHandler: filterFileHandler.write(command + "\n") # write to serial serial.write(cmd) serial.flushOutput() delayedWork = threading.Thread(target=setReStrAfter, args=(tmpReStr, fullLogTime)) delayedWork.start()
else: unit = "Minutes" print 'Estimated Scan time =', str(estimated) + " " + str( unit) + " [Pessimistic]" #Please note: laser PWM is controlled by M700 SXXX print "\n ---------- Initializing ---------- \n" '''#### SERIAL PORT COMMUNICATION ####''' serial_port = config.get('serial', 'port') serial_baud = config.get('serial', 'baud') serial = serial.Serial(serial_port, serial_baud, timeout=0.5) serial.flushInput() serial.flushOutput() time.sleep(1) #give it a second! serial.write('G90\r\n') #set absolute movement if (begin != 0): #if an offset is set, rotates to the specified A angle. serial.write('G0 X' + str(begin) + ' F5000\r\n') #set zero pos = begin #set start position as defined offset. if (a_offset != 0): #if an offset is set, rotates to the specified A angle. serial.write('G0 E' + str(a_offset) + '\r\n') #set zero pos = begin #set start position as defined offset.
''' SERIAL FLUSH INPUT & OUTPUT BUFFER ''' import serial import ConfigParser config = ConfigParser.ConfigParser() config.read('/var/www/lib/serial.ini') serial_port = config.get('serial', 'port') serial_baud = config.get('serial', 'baud') serial = serial.Serial(serial_port, serial_baud, timeout=0.5) serial.flushInput() serial.flushOutput() serial.flush() serial.close()
def ASK_Slave(serial, cmd_data): global recv_data, recv_n global abs_path timeout_count = 0 timeout_total = 0 print 'recv_n', recv_n print 'recv_data_B', recv_data recv_n = 0 recv_data = 'N' while True: print "Motor is moving: ", timeout_count, " times" time.sleep(0.5) USART_Status = 'BUSY' inWaiting_count = 0 while USART_Status == 'BUSY': try: recv_n = serial.inWaiting() USART_Status = 'FREE' inWaiting_count = 0 pass except Exception, e: print 'USART Wrong!' USART_Status = 'BUSY' Journal_log(log=Get_time_str(':') + "-USART Error:" + str(Exception) + str(e) + '\n') inWaiting_count += 1 if inWaiting_count > 3: inWaiting_count = 0 print 'Goto Repeate CMD Send.' break time.sleep(3) pass print cmd_data, '&& recv_n:', recv_n if recv_n >= 2: recv_data = serial.read(recv_n) print recv_n, ' && ', recv_data serial.flushInput() if 'OK' in recv_data and cmd_data[0] in recv_data: print 'recv_data_A', recv_data, '+', recv_n print 'Moving Finished.' recv_data_return = recv_data recv_n = 0 recv_data = 'N' serial.flushOutput() time.sleep(0.5) # 1 return recv_data_return else: print "Wrong Control,Break!" recv_n = 0 recv_data = 'N' serial.flushOutput() timeout_count += 1 if timeout_count >= 30: print "No response Slave,Repeat send cmd please." serial.write(cmd_data) timeout_total += timeout_count timeout_count = 0 if timeout_total <= 60: Journal_log(log="Slave No response for CMD:" + cmd_data[0] + " " + str(timeout_total) + " times,Repeat Send CMD...\n") else: Journal_log(log="Slave No response for CMD:" + cmd_data[0] + " " + str(timeout_total) + " times,Shutdown System...\n") died_time = time.localtime(time.time()) Journal_log(log="System died at: " + Get_time_str(':') + '.\n') os.system('sync') time.sleep(1) print 'sudo shutdown -r now' os.system('sudo shutdown -r now')
def send_request(serial, data): if serial.isOpen: #print(" send ", data) serial.flushInput() serial.flushOutput() serial.write(data)