ser.write() hex string Raspberry

2019-09-11 03:40发布

问题:

I have a problem with sending/reading hex data over RS485. I'm not sure if I send the correct string over the serial port. The hex code is: E1 14 75 81. I have read that i can write:

data = "\xE1\x14\x75\x81" 
ser.write(data)

To check if the correct code is send, I added print(ser.write(data)) But I get this Output back:

True
4
▒

I also want to read the answer of the hex string. For this i only have to added ser.read()

Here the ful code: import serial import struct

    ser = serial.Serial(
        port='/dev/ttyAMA0',
        baudrate=19200,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS
    )

    print(ser.isOpen())
    data = "\xE1\x14\x75\x81"
    ser.write(data)
    print(ser.write(data))
    ser.read()
    print(ser.read())
    ser.close()

Many thank for your help!