ser.write() hex string Raspberry

我的未来我决定 提交于 2020-01-06 14:49:11

问题


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!

来源:https://stackoverflow.com/questions/34149991/ser-write-hex-string-raspberry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!