0

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!

  • ser.write returns the number of bytes written (4) ... if you try and print hex it will look really wrong (since its not ascii ...) try instead `print(repr(ser.read()))` as this will give you a better idea of what you are getting back or `print(map(hex,map(ord,ser.read())))` would also give you the appropriate representation ... or even `binascii.hexlify(ser.read()))` (but im not sure if `ser.read` only reads a single byte by default ...) – Joran Beasley Dec 08 '15 at 07:41
  • If your RS485 transmitter isn't connected to a shared wire (i.e. to which your receiver is also connected), or doesn't have a loopback to the receiver, or the remote system isn't echoing what you send, then you won't receive anything using the ser.read() command. – DisappointedByUnaccountableMod Dec 08 '15 at 09:26
  • Hello, Thanks for the help! `print(map(hex,map(ord,ser.read())))` works perfect. But you are wirght. `ser.read()` reads only a single byte. did you know how i can change it? I expect 46 bytes. – spiegelkabinett Dec 08 '15 at 10:34
  • Okay i found the solution! `ser.read(46)` works.. – spiegelkabinett Dec 08 '15 at 10:44

0 Answers0