-1

I am trying to poll data from an instrument that uses ModBus protocol via pyserial. I need to know how to prevent pyserial from sending the command in ascii.

Here is what I have done:

  1. Using RealTerm, I connected to the instrument and sent this command and hit "Send as Numbers": 0x33 0x03 0x9D 0xA4 0x00 0x02 0xAE 0x56. Sending this line as numbers was able to poll the data correctly: 33 03 04 BE CE 5C A8 94 99; for example, "BE CE 5C A8" translates to -0.4030, which is the number that the instrument shows on its screen.

  2. Using RealTerm and the same command, if I hit "Send as ASCII", the polled data would be incorrect: 30 F8 04 33 CC 44 A0 04 48 16 78 B0 04 85 DA, which I cannot relate to the number shown on the instrument's screen.

  3. Going back to Python and pyserial, when I tried writing over the serial port using serial.write("0x33 0x03 0x9D 0xA4 0x00 0x02 0xAE 0x56"), I got the same data as in step 2.

Therefore, it seems to me that serial.write() sends the command as ASCII. Does anyone know how I can send this data in a similar manner to RealTerm step 1?

MortezaA
  • 1
  • 1
  • Possible duplicate of [Writing bytes out](https://stackoverflow.com/questions/43729606/writing-bytes-out) – Alex P. May 29 '19 at 01:51

1 Answers1

0

You can use bytes or byte array:

msg = bytes.fromhex("33 03 9D A4 00 02 AE 56")
serial.write(msg)
AdamTL
  • 170
  • 11
  • Thanks Adam! I don't have access to the instrument right now to test your method but I have a question. The python code reads msg from a json file (config file) and I am not very familiar with json files but I know that it cannot process python commands and therefore putting these two lines into the json files would throw an error (am I right?). Since msg = b'3\x03\x9d\xa4\x00\x02\xaeV', do you know how I should exactly write it in the json file? – MortezaA May 29 '19 at 02:56