title: Modbus, python and minimal modbus
body: <p>I'm using the python library Minimal Modbus to read an energy meter (RS-485).</p>
  
  <p>I'm trying to use the function to read multiple registers at once </p>
  
  <blockquote>
    <p>read_registers(registeraddress, numberOfRegisters, functioncode=3)</p>
  </blockquote>
  
  <p>Here is the code of the python library:</p>
  
  <pre><code>def read_registers(self, registeraddress, numberOfRegisters, functioncode=3):
          """Read integers from 16-bit registers in the slave.
  
          The slave registers can hold integer values in the range 0 to 65535 ("Unsigned INT16").
  
          Args:
              * registeraddress (int): The slave register start address (use decimal numbers, not hex).
              * numberOfRegisters (int): The number of registers to read.
              * functioncode (int): Modbus function code. Can be 3 or 4.
  
          Any scaling of the register data, or converting it to negative number (two's complement)
          must be done manually.
  
          Returns:
              The register data (a list of int).
  
          Raises:
              ValueError, TypeError, IOError
  
          """
          _checkFunctioncode(functioncode, [3, 4])
          _checkInt(numberOfRegisters, minvalue=1, description='number of registers')
          return self._genericCommand(functioncode, registeraddress, \
              numberOfRegisters=numberOfRegisters, payloadformat='registers')
  </code></pre>
  
  <p>The problem I have got is that the registers hold the data as <code>Long</code>but this function returns them as a list of <code>int</code> and looking at the values it doesn't seem correct.</p>
  
  <p>This is my script at the moment:</p>
  
  <pre><code>##!/usr/bin/env python
  
  import minimalmodbus
  import time
  import glob
  import sys
  import MySQLdb
  
  instrument = minimalmodbus.Instrument('/dev/ttyUSB1', 7)
  
  #Debug Options
  
  #instrument.debug = True #debug modbus
  
  read = False
  
  def convert(value):
          data=chr(value)
          return data
  
  def read_registers_ime():
          data = instrument.read_registers(4096,20)
          return data
  
  while True:
          try:
                  while read == False:
                          data = read_registers_ime()
                          print data
                          time.sleep(0.11)
                          read = True
  
                  break
  
          except KeyboardInterrupt:
                  print "Stopped"
                  sys.exit()
  
          except TypeError:
                  print "TypeError"
  
          except IOError:
                  print "IOError"
  </code></pre>
  
  <p>AT the moment it returns the following:</p>
  
  <blockquote>
    <p>[3, 39192, 3, 44592, 3, 44592, 0, 423, 0, 0, 0, 0, 0, 0, 6, 19884, 6, 24584, 6, 19884]</p>
  </blockquote>
  
  <p>Everything I have tried so far to convert the data back to the original format has failed. I would really appreciate some help here.</p>
  
  <p>Many thanks,</p>
  
  <p>Bryan</p>