Pyserial 从串行端口读取字节 return 垃圾值
Pyserial read bytes return garbage values from serial port
在我的 python 程序中,我使用 PySerial 库从串行端口以字节形式读取数据。但是大多数字节都是垃圾值。我正在使用此 python 模块提供源以及读取字节的输出。
所以请告诉我(正确的配置设置)使用 PySerial 库从串行端口读取正确的值?如果没有,我们在 Python 中是否有任何其他串行库,我可以在其中读取字节而不会出现上述问题。
import serial
import sys
import _thread
import serial.tools.list_ports
import threading
class SerialPort:
ReceiveCallback: None
def __init__(self):
self.comportName = ""
self.baud = 0
self.timeout = None
self.ReceiveCallback = None
self.isopen = False
self.receivedMessage = None
self.serialport = serial.Serial()
def __del__(self):
try:
if self.serialport.is_open():
self.serialport.close()
except:
print("Destructor error closing COM port: ", sys.exc_info()[0])
def RegisterReceiveCallback(self, aReceiveCallback):
self.ReceiveCallback = aReceiveCallback
try:
ListnerThread = threading.Thread(target=self.SerialReadBytesThread)
ListnerThread.start()
except:
print("Error starting Read thread: ", sys.exc_info()[0])
def SerialReadlineThread(self):
while True:
try:
if self.isopen:
self.receivedMessage = self.serialport.readline()
if self.receivedMessage != "":
self.ReceiveCallback(self.receivedMessage)
except:
print("Error reading COM port: ", sys.exc_info()[0])
def SerialReadBytesThread(self):
while True:
try:
if self.isopen:
size = self.serialport.inWaiting()
if size > 0:
readBytes = self.serialport.read(size)
self.receivedMessage = bytearray(readBytes)
if self.receivedMessage != b'':
self.ReceiveCallback(self.receivedMessage)
except serial.SerialException as e:
print("Error reading com port:", e.strerror)
def IsOpen(self):
return self.isopen
def Open(self, portname, baudrate, timeout=0):
if not self.isopen:
# serialPort = 'portname', baudrate, bytesize = 8, parity = 'N', stopbits = 1, timeout = None, xonxoff = 0, rtscts = 0)
self.serialport.port = portname
self.serialport.baudrate = baudrate
self.serialport.timeout = timeout
try:
self.serialport.open()
self.isopen = True
self.serialport.flushInput()
except:
print("Error opening COM port: ", sys.exc_info()[0])
def Close(self):
if self.isopen:
try:
self.serialport.close()
self.isopen = False
except:
print("Close error closing COM port: ", sys.exc_info()[0])
def Send(self, message):
if self.isopen:
try:
# Ensure that the end of the message has both \r and \n, not just one or the other
newmessage = message.strip()
newmessage += '\r\n'
self.serialport.write(newmessage.encode('utf-8'))
except:
print("Error sending message: ", sys.exc_info()[0])
else:
return True
else:
return False
def GetComPort(vendorProductID):
portName = " "
ports = serial.tools.list_ports.comports()
for port, des, hwID in ports:
if vendorProductID in hwID:
portName = port
return portName
return portName
if __name__ == '__main__':
portName = SerialPort.GetComPort("VID:PID=0DB5:0180")
comport = SerialPort()
comport.Open(portName, 115200)
t=threading.Thread(target=comport.SerialReadBytesThread)
t.start()
t.join()
结果从Python模块读取数据:
b'\x02\x02\x0e\x01\x0b\x010\x02A\x01\x17 \x19\t%\x11\x00\x11\xe3\xb0\xc4B\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99o\xb9$\'\xaeA\xe4d\x9b\x93L\xa4\x95\x99\x1bxR\xb8U\x01\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x03\xff\xff\xff\x01\x00\xb28\x04\x80\x08\xc0\x8a\x02\x00\x00\x00\x00\x00\x00\x00\x030\x00\x00\x00\x00\x00\x00\x01\x00\t%\x11\x00\x11<\x00i\x11\x00\x11\t%\x05rQ1909251100114Vx\x90\txR"\x01S\x00\x00\x00\x01\x01A\x82\x02\x00\x95\x05\x00\x00\x00\x00\x00\x9c\x01\x00_*\x02\tx_4\x01\x00\x9f\x02\x06\x00\x00\x00\x00\x01\x00\x9f\x03\x06\x00\x00\x00\x00\x00\x00\x9f\x10\x07\x06\r\n\x03\xa0\x00\x9f\x1a\x02\x08@\x9f&\x08\x93?]\x8cL\xb1\x1b\xd4\x9f\'\x01\x80\x9f6\x02\x00\xc2\x9f7\x04\xc3\xe8\xee\xf9\x9f9\x01\x07\x9ff\x04\xa0\x00\xc0\x00\x9fn\x04p\x00\x00\xcf\x01\x04W\x13I)Ew\x89\x17\x06\xd2\x102\x01\x00\x13\x10\x00\x00\x00\x1f\x9a\x03\x19\t%\xdf\xdf#\x02\x08@\xdf\xdf\'\x01\x00\x00\x10\x92\xcf\xeb\x93\xb2\xe0\xfe\x00\x00,\x00$:\xd54\x14\x89\x96\x8b\t\xdb\xb9\xd4\xc9\xdb6}\x8e\x9eP\xc1?\x91a\xdd\x06~\xc8\xca\x85\xd8^\xa7\xda\xa9\x03\x03 '
预期value/Actual值:(我确实从串口截图工具中得到了这个值,而运行我的python模块)
02 02 0E 01 0B 01 30 02 41 01 17 20 19 09 25 11 02 10 E3 B0 C4 42 98 FC 1C 14 9A FB F4 C8 99 6F B9 24 27 AE 41 E4 64 9B 93 4C A4 95 1B 78 52 B8 55 01 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法00 03 FF FF FF 01 00 B2 38 04 80 08 C0 8A 02 00 00 00 00 00 00 00 03 30 00 00 00 00 00 00 01 00 09 25 11 02 10 3C 00 6A 11 03 7 5 9 09 30 39 32 33 31 31 31 31 31 31 30 34 56 78 90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 09 78 52 22 22 01 53 00 00 00 00 00 01 01 01 41 41 41 82 20 00 95 95 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9C 01 00 5F 2A 02 09 78 5F 34 01 00 9F 02 06 00 00 00 00 00 00 00 01 00 9F 03 06 006 00 00 00 00 00 00 00 00 00 9F 10 07 06 06 0D 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 03 A0 20 00 9F 1A 02 02 02 02 02 02 02 02 02 02 02 08 40 9F 26 08 8. EB 12 3F BB 4A 81 AB 37 9F 27 01 80 9F 36 02 00 C3 9F 37 04 0E D2 3B E0 9F 39 01 07 9F 66 04 A0 00 C0 00 9F 6E 04 20 70 00 00 CF 01 1 9 4 5 4 77 89 17 20 06 D2 10 32 01 00 13 10 00 00 00 1F 9A 03 19 09 25 DF DF 23 02 08 40 DF DF 27 01 0 0 00 10 92 CF
EB 93 B2 E0 FE 00 00 2D 00 24 B8 CD E0 22 A4 03 6E 2D D3 96 9E 0C 39 3A 6F 9C 01 45 88 F2 AC AF F2 C1 07 67 3F 07 52 16 FF 3D 26 35 03
将转义字节字符串编码为 HEX 时基本相同
02020e010b01300241011720190925110011e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0003ffffff0100b238048008c08a02000000000000000330000000000000010009251100113c00691100110925057251313930393235313130303131345678902009785222015300000001014182022000950500000000009c01005f2a0209785f3401009f02060000000001009f03060000000000009f1007060d0a03a020009f1a0208409f2608933f5d8c4cb11bd49f2701809f360200c29f3704c3e8eef99f3901079f6604a000c0009f6e0420700000cf01045713492945778917 2006d21032010013100000001f9a03190925dfdf23020840dfdf270100001092cfeb93b2e0fe00002c00243ad5341489968b09dbb9d4c9db367d8e9e50c13f9161dd067ec8ca85d85ea7daa90303
你可以调用它的hex方法将bytes转换成hex
hex_string=bytes.hex()
在我的 python 程序中,我使用 PySerial 库从串行端口以字节形式读取数据。但是大多数字节都是垃圾值。我正在使用此 python 模块提供源以及读取字节的输出。
所以请告诉我(正确的配置设置)使用 PySerial 库从串行端口读取正确的值?如果没有,我们在 Python 中是否有任何其他串行库,我可以在其中读取字节而不会出现上述问题。
import serial
import sys
import _thread
import serial.tools.list_ports
import threading
class SerialPort:
ReceiveCallback: None
def __init__(self):
self.comportName = ""
self.baud = 0
self.timeout = None
self.ReceiveCallback = None
self.isopen = False
self.receivedMessage = None
self.serialport = serial.Serial()
def __del__(self):
try:
if self.serialport.is_open():
self.serialport.close()
except:
print("Destructor error closing COM port: ", sys.exc_info()[0])
def RegisterReceiveCallback(self, aReceiveCallback):
self.ReceiveCallback = aReceiveCallback
try:
ListnerThread = threading.Thread(target=self.SerialReadBytesThread)
ListnerThread.start()
except:
print("Error starting Read thread: ", sys.exc_info()[0])
def SerialReadlineThread(self):
while True:
try:
if self.isopen:
self.receivedMessage = self.serialport.readline()
if self.receivedMessage != "":
self.ReceiveCallback(self.receivedMessage)
except:
print("Error reading COM port: ", sys.exc_info()[0])
def SerialReadBytesThread(self):
while True:
try:
if self.isopen:
size = self.serialport.inWaiting()
if size > 0:
readBytes = self.serialport.read(size)
self.receivedMessage = bytearray(readBytes)
if self.receivedMessage != b'':
self.ReceiveCallback(self.receivedMessage)
except serial.SerialException as e:
print("Error reading com port:", e.strerror)
def IsOpen(self):
return self.isopen
def Open(self, portname, baudrate, timeout=0):
if not self.isopen:
# serialPort = 'portname', baudrate, bytesize = 8, parity = 'N', stopbits = 1, timeout = None, xonxoff = 0, rtscts = 0)
self.serialport.port = portname
self.serialport.baudrate = baudrate
self.serialport.timeout = timeout
try:
self.serialport.open()
self.isopen = True
self.serialport.flushInput()
except:
print("Error opening COM port: ", sys.exc_info()[0])
def Close(self):
if self.isopen:
try:
self.serialport.close()
self.isopen = False
except:
print("Close error closing COM port: ", sys.exc_info()[0])
def Send(self, message):
if self.isopen:
try:
# Ensure that the end of the message has both \r and \n, not just one or the other
newmessage = message.strip()
newmessage += '\r\n'
self.serialport.write(newmessage.encode('utf-8'))
except:
print("Error sending message: ", sys.exc_info()[0])
else:
return True
else:
return False
def GetComPort(vendorProductID):
portName = " "
ports = serial.tools.list_ports.comports()
for port, des, hwID in ports:
if vendorProductID in hwID:
portName = port
return portName
return portName
if __name__ == '__main__':
portName = SerialPort.GetComPort("VID:PID=0DB5:0180")
comport = SerialPort()
comport.Open(portName, 115200)
t=threading.Thread(target=comport.SerialReadBytesThread)
t.start()
t.join()
结果从Python模块读取数据:
b'\x02\x02\x0e\x01\x0b\x010\x02A\x01\x17 \x19\t%\x11\x00\x11\xe3\xb0\xc4B\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99o\xb9$\'\xaeA\xe4d\x9b\x93L\xa4\x95\x99\x1bxR\xb8U\x01\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x03\xff\xff\xff\x01\x00\xb28\x04\x80\x08\xc0\x8a\x02\x00\x00\x00\x00\x00\x00\x00\x030\x00\x00\x00\x00\x00\x00\x01\x00\t%\x11\x00\x11<\x00i\x11\x00\x11\t%\x05rQ1909251100114Vx\x90\txR"\x01S\x00\x00\x00\x01\x01A\x82\x02\x00\x95\x05\x00\x00\x00\x00\x00\x9c\x01\x00_*\x02\tx_4\x01\x00\x9f\x02\x06\x00\x00\x00\x00\x01\x00\x9f\x03\x06\x00\x00\x00\x00\x00\x00\x9f\x10\x07\x06\r\n\x03\xa0\x00\x9f\x1a\x02\x08@\x9f&\x08\x93?]\x8cL\xb1\x1b\xd4\x9f\'\x01\x80\x9f6\x02\x00\xc2\x9f7\x04\xc3\xe8\xee\xf9\x9f9\x01\x07\x9ff\x04\xa0\x00\xc0\x00\x9fn\x04p\x00\x00\xcf\x01\x04W\x13I)Ew\x89\x17\x06\xd2\x102\x01\x00\x13\x10\x00\x00\x00\x1f\x9a\x03\x19\t%\xdf\xdf#\x02\x08@\xdf\xdf\'\x01\x00\x00\x10\x92\xcf\xeb\x93\xb2\xe0\xfe\x00\x00,\x00$:\xd54\x14\x89\x96\x8b\t\xdb\xb9\xd4\xc9\xdb6}\x8e\x9eP\xc1?\x91a\xdd\x06~\xc8\xca\x85\xd8^\xa7\xda\xa9\x03\x03 '
预期value/Actual值:(我确实从串口截图工具中得到了这个值,而运行我的python模块)
02 02 0E 01 0B 01 30 02 41 01 17 20 19 09 25 11 02 10 E3 B0 C4 42 98 FC 1C 14 9A FB F4 C8 99 6F B9 24 27 AE 41 E4 64 9B 93 4C A4 95 1B 78 52 B8 55 01 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法FF FF FF FF FF FF FF FF FF FF FF FF FF
FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 法00 03 FF FF FF 01 00 B2 38 04 80 08 C0 8A 02 00 00 00 00 00 00 00 03 30 00 00 00 00 00 00 01 00 09 25 11 02 10 3C 00 6A 11 03 7 5 9 09 30 39 32 33 31 31 31 31 31 31 30 34 56 78 90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 09 78 52 22 22 01 53 00 00 00 00 00 01 01 01 41 41 41 82 20 00 95 95 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 9C 01 00 5F 2A 02 09 78 5F 34 01 00 9F 02 06 00 00 00 00 00 00 00 01 00 9F 03 06 006 00 00 00 00 00 00 00 00 00 9F 10 07 06 06 0D 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 0A 03 A0 20 00 9F 1A 02 02 02 02 02 02 02 02 02 02 02 08 40 9F 26 08 8. EB 12 3F BB 4A 81 AB 37 9F 27 01 80 9F 36 02 00 C3 9F 37 04 0E D2 3B E0 9F 39 01 07 9F 66 04 A0 00 C0 00 9F 6E 04 20 70 00 00 CF 01 1 9 4 5 4 77 89 17 20 06 D2 10 32 01 00 13 10 00 00 00 1F 9A 03 19 09 25 DF DF 23 02 08 40 DF DF 27 01 0 0 00 10 92 CF
EB 93 B2 E0 FE 00 00 2D 00 24 B8 CD E0 22 A4 03 6E 2D D3 96 9E 0C 39 3A 6F 9C 01 45 88 F2 AC AF F2 C1 07 67 3F 07 52 16 FF 3D 26 35 03
将转义字节字符串编码为 HEX 时基本相同
02020e010b01300241011720190925110011e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b8550100ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0003ffffff0100b238048008c08a02000000000000000330000000000000010009251100113c00691100110925057251313930393235313130303131345678902009785222015300000001014182022000950500000000009c01005f2a0209785f3401009f02060000000001009f03060000000000009f1007060d0a03a020009f1a0208409f2608933f5d8c4cb11bd49f2701809f360200c29f3704c3e8eef99f3901079f6604a000c0009f6e0420700000cf01045713492945778917 2006d21032010013100000001f9a03190925dfdf23020840dfdf270100001092cfeb93b2e0fe00002c00243ad5341489968b09dbb9d4c9db367d8e9e50c13f9161dd067ec8ca85d85ea7daa90303
你可以调用它的hex方法将bytes转换成hex
hex_string=bytes.hex()