not sure what you are getting from:

Modbus.read_input_registers()

but if it is a binary stream then you can put it all in one numpy array
(probably type uint8 (byte)).

then you can manipulate the type with arr.astype() and arr.byteswap()

astype will tell numpy to interpret the same block of data as a different
type.

You also may be able to create the array with np.fromstring() or
np.frombuffer() in the fisrst place.

-CHB





On Thu, Sep 14, 2017 at 10:11 AM, Nissim Derdiger <niss...@elspec-ltd.com>
wrote:

> Hi all!
>
> I'm writing a Modbus TCP client using *pymodbus3* library.
>
> When asking for some parameters, the response is always a list of int16.
>
> In order to make the values usable, I need to transfer them into 32bit
> bites, than put them in the correct order (big\little endian wise), and
> then to cast them back to the desired format (usually int32 or float)
>
> I've solved it with a pretty naïve code, but I'm guessing there must be a
> more elegant and fast way to solve it with NumPy.
>
> Your help would be very much appreciated!
>
> Nissim.
>
>
>
> My code:
>
> def Read(StartAddress, NumOfRegisters, FunctionCode,ParameterType,
> BitOrder):
>
>                 # select the Parameters format
>
>                 PrmFormat = 'f' # default is float
>
>                 if ParameterType == 'int':
>
>                                 PrmFormat = 'i'
>
>                 # select the endian state - maybe move to the connect
> function?
>
>                 endian = '<I'  # default is big endian
>
>                 if BitOrder == 'little':
>
>                                 endian = '>I'
>
>                 # start asking for the payload
>
>                 payload = None
>
>                 while payload == None:
>
>                 payload = Modbus.read_input_registers(StartAddress,
> NumOfRegisters)
>
>                 #### parse the answer
>
>                 ResultRegisters = []
>
>                 # convert the returned registers from list of int16 to
> list of 32 bits bitmaks
>
>                 for reg in range(int(NumOfRegisters / 2)):
>
>                                 ResultRegisters[reg] =
> struct.pack(endian, payload.registers[2 * reg]) +
> struct.pack(endian,payload.registers[2 * reg + 1])
>
>                 # convert this list to a list with the real parameter
> format
>
>                 for reg in range(len(ResultRegisters)):
>
>                                 ResultRegisters[reg]=
> struct.unpack(PrmFormat,ResultRegisters(reg))
>
>                 # return results
>
>                 return ResultRegisters
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
>


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

chris.bar...@noaa.gov
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@python.org
https://mail.python.org/mailman/listinfo/numpy-discussion

Reply via email to