sending hex via uart

  • I have a sensor, which communicates with my bouard via uart.
    I have to send this hex: 0x01 0x03 0x00 0x00 0x00 0x01 0x84 0x0A
    My quesion is what is the correct syntax to do that?

    This doesn't throw a syntax error, but I am not sure this means what I think it means....
    uart.write(b'\x01\x03\x00\x00\x00\x01\x84\x0A') If this correct what other alternatives are acceptable?
    I am unable to communicate with the sensor, and I am not sure where is the issu so far. One of my guess is this.

  • @robert-hh I messed up the indexing. Thanks. :)

  • @tttadam In that case it's:
    str(ustruct.unpack(">h", r[3:5])[0] / 10)

  • @robert-hh
    This is a sample from the datasheet:
    0x01 0x03 0x02 0x02 0xC9 0x79 0x72

    0x02 0xC9 is the decibel value --> 02C9H --> 713 --> 71,3 declibel
    0x79 0x72 this is the ?checksum?

  • @tttadam my question would be:
    at what place in the buffer is the value you want to have, and how is it coded. At the moment unpack assumes, that it is a 2 Byte small endian value in bytes 4 and 5. If you just need a single byte, you can use r[4].

  • @robert-hh hmmm, I am not sure how does unpack() works, but the value seems to be off to me. The db value is the

    result is: b'\x01\x03\x02\x02y x\xc6'
    db value is: 31096
    result is: b'\x01\x03\x02\x02\x83\xf8\x85'
    db value is: -31752
    result is: b'\x01\x03\x02\x02\x85x\x87'
    db value is: -31368
    result is: b'\x01\x03\x02\x02\x868\x86'
    db value is: -31176
    result is: b'\x01\x03\x02\x02yx\xc6'
    db value is: 31096

    my code:

    def f():
        r =
        print("result is: " + str(r))
        print("db value is: " + str(ustruct.unpack(">h", r[4:6])[0])) # if the change the range to 4:5 which are the db values, i get a value error buffer too small 

  • @tttadam You can use ustruct.unpack() to decode the message. So you would have a 2 byte integer to unpack from the message at position 4 and 5, like.

    import ustruct
    noise = ustruct.unpack("<h", msg[4:6])[0]

    If the value you need is big endian, replace in the format code of unpack the < by >

  • eventually I found the issue.
    I had to play around a little bit more with the timing. (I have to invest a logic level analyser)
    But now a I facing an other one. The board converts some of the repy array to ascii caracters, but I need the value. Is there a way to get the hex value? the 4th and 5th values are suppose to be the noise level (in hex).

    result is: b'\x01\x03\x02\x02\x8a8\x83'
    result is: b'\x01\x03\x02\x02\x829E'
    result is: b'\x01\x03\x02\x02\x83\xf8\x85'
    result is: b'\x01\x03\x02\x02\x88\xb9B'
    result is: b'\x01\x03\x02\x03\x8a9\x13'
    result is: b'\x01\x03\x02\x02\xe3\xf8\xad'

  • I will write down my issue a little bit more specific.
    I would like to communicate with an rs485 device over an wsh-4777 converter.
    With an usb-rs485 and usb uart converter we manage to debug that the issue probarly that the pycom is unable to pull high enough the rse pin on the wsh-4777 converter.
    If i hook the sensor to a pc via rs485-usb converter then it's work fine.
    If i hook the pycom board to the pc via an usb-uart converter, then I see the correct hex array
    If I listen in between the wsh-4777 and sensor, when it hooked to the pycom, then the sensor gets the value, send the answer back, but the pycom didn't received it.
    (The rs485 device btw is an RT-ZS-BZ-485 noise sensor)

    this is the code what I am using:

    p = Pin('P6', mode=Pin.OUT, pull=Pin.PULL_UP)
    uart = UART(1, baudrate=9600, pins=('P20','P21'),parity=None, stop=1,bits=8)
    def f():
        r =
        print("result is: " + str(r))

  • @tttadam This should be correct. You can connect your UART to your computer and see what you receive there to verify that it is probably sending what you intend.

    Have you checked:

    • The speed/bits/parity settings?
    • Whether level-shifting (3V/5V...) is needed?
    • That you didn't switch RX and TX?
    • Whether any other signals (DTR, RTS/CTS) are needed?

Log in to reply

Pycom on Twitter