Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    sending hex via uart

    Scheduled Pinned Locked Moved MicroPython
    micropythonuarthex
    10 Posts 3 Posters 348 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • T Offline
      tttadam
      last edited by

      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.
      Thanks

      J 1 Reply Last reply Reply Quote 0
      • J Offline
        jcaron @tttadam
        last edited by

        @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?
        1 Reply Last reply Reply Quote 0
        • T Offline
          tttadam
          last edited by

          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():
              p(1)
              utime.sleep_us(50)
              uart.write(b'\x01\x03\x00\x00\x00\x01\x84\x0A')
              utime.sleep_us(50)
              p(0)
              utime.sleep_us(50)
              r = uart.read()
              print("result is: " + str(r))
          
          1 Reply Last reply Reply Quote 0
          • T Offline
            tttadam
            last edited 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'
            
            robert-hhR 1 Reply Last reply Reply Quote 0
            • robert-hhR Offline
              robert-hh @tttadam
              last edited by robert-hh

              @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 >

              T 1 Reply Last reply Reply Quote 0
              • T Offline
                tttadam @robert-hh
                last edited by tttadam

                @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():
                    uart.read()
                    re(1)
                    de(1)
                    utime.sleep_ms(20)
                    uart.write(b'\x01\x03\x00\x00\x00\x01\x84\x0A')
                    utime.sleep_us(8500)
                    re(0)
                    de(0)
                    utime.sleep_ms(500)
                    r = uart.read()
                    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 
                
                robert-hhR 1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @tttadam
                  last edited by

                  @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].

                  T 1 Reply Last reply Reply Quote 0
                  • T Offline
                    tttadam @robert-hh
                    last edited by

                    @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?

                    robert-hhR 1 Reply Last reply Reply Quote 0
                    • robert-hhR Offline
                      robert-hh @tttadam
                      last edited by

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

                      T 1 Reply Last reply Reply Quote 0
                      • T Offline
                        tttadam @robert-hh
                        last edited by

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

                        1 Reply Last reply Reply Quote 0

                        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                        With your input, this post could be even better 💗

                        Register Login
                        • 1 / 1
                        • First post
                          Last post

                        Pycom on Twitter

                        Pycom