如何在Python(数字测斜仪)中读取串行数据。
这是文档:
这是我的代码:
import binascii
import serial
SERIAL_PORT = 'COM1'
SERIAL_RATE = 9600
def main():
ser = serial.Serial(SERIAL_PORT, SERIAL_RATE)
while True:
if ser.isOpen():
s = ser.read()
hex_string = binascii.hexlify(s).decode('utf-8')
print(hex_string)
else:
print("Can not open serial port")
if __name__ == "__main__":
main()
ser = serial.Serial的输出(port = SERIAL_PORT,baudrate = SERIAL_RATE,bytesize = serial.EIGHTBITS,stopbits = serial.STOPBITS_ONE,parity = serial.PARITY_NONE,timeout = 1)