title: Python: convert array of uint16_t to string (from Arduino over RS-485)
body: <p>I'm trying to send json over modbus rtu (I know, it is very bad use of modbus.)</p>
  
  <p>My schema is similar to:
  <a href="https://i.stack.imgur.com/ez2lr.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ez2lr.jpg" alt="enter image description here"></a></p>
  
  <p>I have connected Arduino USB to PC as COM5 and RS485 converter connected to USB-RS485 to PC as COM4.</p>
  
  <p><a href="https://i.stack.imgur.com/4b1E8.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4b1E8.jpg" alt="enter image description here"></a></p>
  
  <p>If I read data from Arduino using <a href="http://qmodbus.sourceforge.net/" rel="nofollow noreferrer">QModBus</a> application, I will see a lot 
   of bytes (COM4). </p>
  
  <p>For control: after I send from QModBus "read holding registers" Arduino serial monitor on "Arduino usb port" (COM5) print valid string. So I mean that modbus bytes are ok:</p>
  
  <p><a href="https://i.stack.imgur.com/MO6GE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MO6GE.png" alt="enter image description here"></a></p>
  
  <p>There is my Arduino code:</p>
  
  <pre><code>#include &lt;ArduinoJson.h&gt;
  #include &lt;ModbusRtu.h&gt;
  #define ID   1
  
  // assign the Arduino pin that must be connected to RE-DE RS485 transceiver
  #define TXEN  2 
  
  Modbus slave(ID, 0, TXEN); // this is slave ID and RS-232 or USB-FTDI
  
  // data array for modbus network sharing
  uint16_t au16data[100];
  
  boolean state;
  String json = "{\"idx\":1430,\"nvalue\":0,\"svalue\":\"-58.00\"}";
  
  void setup() {
    slave.begin( 9600 );
    StaticJsonBuffer&lt;200&gt; jsonBuffer;  
    JsonObject&amp; root = jsonBuffer.parseObject(json); 
  }
  
  void loop() {
    // https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino/blob/master/ModbusRtu.h#L1391
    byte x = 0;
    for (byte i = 0; i &lt; json.length(); i += 2) {
      uint16_t temp;
      temp = word(
        json[i],
        json[i+1]);  
      au16data[x] = temp;
      x++;
    }
  
    state = slave.poll( au16data, 100 );  
  } 
  </code></pre>
  
  <p>But I don't know how to convert these bytes back to json string in python. My code:</p>
  
  <pre><code>import serial
  import minimalmodbus
  
  MODBUS_3  = 3  # Read holding registers
  
  dev1 = minimalmodbus.Instrument('COM4', 1)  # port name, slave address (in decimal)
  dev1.serial.baudrate = 9600
  dev1.serial.bytesize = 8
  dev1.serial.stopbits = 1
  dev1.serial.parity = serial.PARITY_NONE
  dev1.debug = False
  
  data = dev1.read_registers(0, 20, MODBUS_3)
  print(data)
  </code></pre>
  
  <p>Code print my the same values as QModBus:</p>
  
  <pre><code>[31522, 26980, 30754, 14897, 13363, 12332, 8814, 30305, 27765, 25890, 14896, 11298, 29558, 24940, 30053, 8762, 8749, 13624, 11824, 12322]
  </code></pre>
  
  <p>Can you please help, how can I convert these numbers to json string as you can see in arduino serial monitor?</p>
  
  <p>And how to convert python string to "uint_16t" for sending over modbus.</p>
  
  <p>Thank you!</p>