def get_sensor_datapoint(serial=ARDUINO): '''Gets sensor data from arduino.''' serial.flush() serial.read_all() serial.write('getTempHumidity\n'.encode()) time.sleep(0.2) s = serial.readline().strip().decode() humidity, temp = s.split(',') return DataPoint(float(humidity), float(temp))
def recv(serial): while True: if select == "2": data = binascii.b2a_hex(serial.read_all()) file.write(data) #data = serial.read_all() #data = "test" else: #data = binascii.b2a_hex(serial.read_all()) data = serial.read_all() file.write(data) return data
def recv(serial): while True: if select == "1": data = serial.read_all() #data = "test" else: data = binascii.b2a_hex(serial.read_all()) if data == '': continue else: break #sleep(0.02) return data
def start_app(): '''Starts app ''' C = [0x55, 0x55, ord('J'), ord('A'), 0x00] crc = calc_crc( C[2:4] + [0x00]) # for some reason must add a payload byte to get correct CRC crc_msb = (crc & 0xFF00) >> 8 crc_lsb = (crc & 0x00FF) C.insert(len(C), crc_msb) C.insert(len(C), crc_lsb) serial.write(C) sleep(2) #print (C) #R = serial.read() #7 R = serial.read_all() print(len(R)) ''' if (R[0]) == 85 and (R[1]) == 85: packet_type = R[2] + R[3] print(packet_type) ''' if R[0] == 85 and R[1] == 85: packet_type = '{0:1c}'.format(R[2]) + '{0:1c}'.format(R[3]) print(packet_type)
def recvWakeup(serial): txt = 'xiaomei' i = 0 # 5秒内收到数据 new_data="" while i < 50: data = serial.read_all() if len(data) == 0: # continue time.sleep(0.1) i += 1 elif isinstance(data,bytes): Log.info(data) try: # new_data = bytes.decode(data) new_data=data.decode() except Exception as e: Log.error("解析出错:"+e) new_data='' else: Log.info(new_data) break ret=new_data.find(txt) if ret != -1: Log.info('wakeup success') return True else: Log.warning('wakeup failure') return False
def recv(serial): while True: data = serial.read_all() if data != b'' : break return data.decode()
def start_bootloader(): '''Starts bootloader :returns: True if bootloader mode entered, False if failed ''' print("start") C = [0x55, 0x55, ord('J'), ord('I'), 0x00] crc = calc_crc( C[2:4] + [0x00]) # for some reason must add a payload byte to get correct CRC crc_msb = (crc & 0xFF00) >> 8 crc_lsb = (crc & 0x00FF) C.insert(len(C), crc_msb) C.insert(len(C), crc_lsb) serial.write(C) time.sleep(2) # must wait for boot loader to be ready #R = serial.read(5) R = serial.read_all() print(R) if R[0] == 85 and R[1] == 85: #packet_type = '{0:1c}'.format(R[2]) + '{0:1c}'.format(R[3]) packet_type = '{0:1c}'.format(R[2]) + '{0:1c}'.format(R[3]) print(packet_type) if packet_type == 'JI': serial.read(R[4] + 2) print('bootloader ready') time.sleep(2) if boot_mode == 0: print('resync with device') time.sleep(2) return True else: return False else: return False
def recvCmd(serial, txt): # codetype = sys.getfilesystemencoding() # txt = 'asr' global cntCmdSucc, cntCmdFail, cntCmdTotal i = 0 res = 'ASR结果 ' # 10 秒内收到回应 for i in range(20): data = serial.read_all() print(data) data = str(data, encoding="utf8") print("识别次数:" + str(i) + "识别结果" + data) fileObject.write("识别次数:" + str(i) + "识别结果" + data) print(data) if data == "": time.sleep(0.8) else: break print("ASR结果:" + data) cntCmdTotal = cntCmdTotal + 1 ret = data.find(txt) if ret != -1: cntCmdSucc = cntCmdSucc + 1 print('Cmd asr success') return True, data else: cntCmdFail = cntCmdFail + 1 print('Cmd asr failure') return False, data
def __recv(self, serial): # 检查接口是否有参数,有则返回 while True: data = serial.read_all() if data != '': break return data
def receive(serial): while True: data = serial.read_all() time.sleep(0.2) if data == '': continue else: break return data
def recv(serial): while True: data = serial.read_all() if data == '': continue else: break sleep(0.2) return data
def recv(serial): while True: if select == "2": data = binascii.b2a_hex(serial.read_all()) file.write(data) #data = serial.read_all() #data = "test" else: #data = binascii.b2a_hex(serial.read_all()) data = serial.read_all() file.write(data) ''' if data == '': continue else: break ''' #sleep(0.02) return data
def recv(serial): start = time.time() while True: data = serial.read_all() if data == '': if time.time()-start >10: break else: break sleep(0.02) return data
def recv(serial): while True: try: data_serial = serial.read_all() #if data_serial == b'': if data_serial == b'': continue else: break sleep(0.02) except KeyboardInterrupt: mySerial.close() return data_serial
def get_data(serial): raw_data = b'' while raw_data == b'': try: raw_data = serial.read_all() except Exception as e: if self.debug_mode == True: print( "DEBUG MODE: Failed to read from the controller, read the exception error below:" ) print(e) print("\n") if self.exit_on_interrupt == True: quit() raw_data = b' ' if self.debug_mode == True: print(f"DEBUG MODE: Rx:{raw_data}") return raw_data
def recv(serial): print('2') data = '' while serial.inWaiting() > 0: print(serial.inWaiting()) print('3') # data += str(serial.read(15)) # ok 要配合timeout 使用, 否则要传入已知 的 size # data += str(serial.readline()) # ok 要配合timeout 使用 # data += str(serial.readlines()) # ok 要配合timeout 使用 # data += str(serial.readall()) # ok 要配合timeout 使用 data += str(serial.read_all()) # ok 要配合timeout 使用 print("************************************") #print(serial.read(13)) print('准备打印data') # data = str(serial.read(19)) print(data) print('data:%s' % data) print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") return data
def recvWakeup(self,serial, logfd): try: i = 0 # 3秒内收到数据 iswakeup = False while i < 30: data = serial.read_all() if len(data) == 0: time.sleep(0.1) i += 1 elif isinstance(data, bytes): new_data = data.decode(encoding='utf-8', errors='ignore') log_ts_str = "[" + datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S.%f') + "] " + new_data self.write_file(logfd, log_ts_str) if new_data.find(self.wakemark) != -1: iswakeup = True break return iswakeup except Exception as e: print("异常信息如下: " + str(e)) return False
def recvWakeup(serial): txt = 'xiaomei' i = 0 # 5秒内收到数据 data = "" while i < 50: data = serial.read_all() print(data) if data == '': # continue time.sleep(0.1) i += 1 else: break data = str(data, encoding="utf8") print(data) ret = data.find(txt) if ret != -1: print('wakeup success') return True else: print('wakeup failure') return False
def start_bootloader(serial): '''Starts bootloader :returns: True if bootloader mode entered, False if failed ''' C = [0x55, 0x55, ord('J'), ord('I'), 0x00] crc = calc_crc( C[2:4] + [0x00]) # for some reason must add a payload byte to get correct CRC crc_msb = (crc & 0xFF00) >> 8 crc_lsb = (crc & 0x00FF) C.insert(len(C), crc_msb) C.insert(len(C), crc_lsb) serial.write(C) #print (C) sleep(2) # must wait for boot loader to be ready #R = serial.read(5) R = serial.read_all() #7 #print(R) try: if R[0] == 85 and R[1] == 85: packet_rev = chr(R[0]) + chr(R[1]) packet_type = R[2] + R[3] #print(packet_rev) if (packet_type == 'JI') or (packet_rev == 'UU'): serial.read(R[4] + 2) #print('bootloader ready') sleep(2) return True else: return False else: return False except: return False
def recvWakeup(serial, logfilepath, wakeup_mark): try: i = 0 # 3秒内收到数据 iswakeup = False new_data = "" while i < 20: data = serial.read_all() if len(data) == 0: time.sleep(0.1) i += 1 elif isinstance(data, bytes): new_data = new_data + data.decode(encoding='utf-8', errors='ignore') write_file(logfilepath, new_data) if new_data.find(wakeup_mark) != -1: iswakeup = True #print("唤醒成功。。。。。。。") break return iswakeup except Exception as e: print("异常信息如下: " + str(e)) return False
import serial import time # 十六进制显示 #将输入的二进制数据流转换为16进制 def hexShow(data): arr = '' dlen = len(data) for i in range(dlen): mid = str(int(data[i], 16)) arr += mid + ' ' print(mid) if __name__ == '__main__': serial = serial.Serial("COM6", 115200, timeout=0.01) while (1): serial.write([0x01, 0x04, 0x01, 0xA1, 0x00, 0x17, 0xE0, 0x1A]) data = serial.read_all() time.sleep(0.5) #将bytes16进制的数字转换为10进制数字 try: print(hexShow(data)) except Exception: pass
def ack_iot(serial): #iot终端向onenet平台的回复信息 sleep(0.5) data = serial.read_all().decode() #print("recv:"+data) #ack_data[1]表示当前来自云平台的报文id if "+MIPLOBSERVE:" in data: ack_data = data.split(',') #print(ack_data) observe_list.append(ack_data[1]) send_data = "AT+MIPLOBSERVERSP=0,%s,1\r\n" % (ack_data[1], ) serial.write(send_data.encode()) sleep(0.5) #跟云平台确认对象的存在 elif "+MIPLDISCOVER:" in data: ack_data = data.split(',') # print(ack_data) send_data = 'AT+MIPLDISCOVERRSP=0,%s,1,34,"5700;5701;5601;5602;5603;5604;5605"\r\n' % ( ack_data[1], ) #send_data = 'AT+MIPLDISCOVERRSP=0,%s,1,34,"5700;5701;5601;5602;5603;5604;5605"\r\n' % (ack_data[1],) #print(send_data) serial.write(send_data.encode()) sleep(0.5) elif "+MIPLREAD:" in data: #通知读取结果 print("recv:" + data) ack_data = data.split(',') send_data = 'AT+MIPLREADRSP=0,%s,1,3303,0,5701,1,3,zwd,0,0\r\n' % ( ack_data[1], ) #print(send_data) serial.write(send_data.encode()) sleep(0.5) elif "+MIPLWRITERSP:" in data: #通知写入的消息结果 print("recv:" + data) ack_data = data.split(',') send_data = 'AT+MIPLREADRSP=0,%s,1,3303,0,5700,4,4,20.123,0,0' % ( ack_data[1], ) print(send_data) serial.write(send_data.encode()) elif "+MIPLEXECUTERSP:" in data: #通知执行操作果 print("recv:" + data) ack_data = data.split(',') send_data = 'AT+MIPLREADRSP=0,%s,1,3303,0,5700,4,4,20.123,0,0' % ( ack_data[1], ) print(send_data) serial.write(send_data.encode()) elif "+MIPLOBSERVERSP:" in data: #通知观测指令是否有效 print("recv:" + data) ack_data = data.split(',') send_data = 'AT+MIPLREADRSP=0,%s,1,3303,0,5700,4,4,20.123,0,0' % ( ack_data[1], ) print(send_data) serial.write(send_data.encode()) else: print(data)
def recv(serial): while True: data = serial.read_all() f_com.write(data) return data
def read_buffer(serial): """ Reads the serial port bufer and decodes it """ resp = serial.read_all() return resp.decode()
def auto_connect(serial, cmd_iot): serial.write(cmd_iot.encode()) data = serial.read_all().decode() print(data) sleep(1)