Closed patfrench closed 1 year ago
It is internal helper function to unpack one value. What are you trying to do and why are you using it directly?
I need to do a read_registers but with the signed option. then i do
minimalmodbus._unpack('>hhh', minimalmodbus._valuelist_to_bytestring(tete_ngt.read_registers(100,3,3), 3)
I need read a multiple registers int16, I hope I am clear ?
You have several possibilities
It should be easy to add support for parameter signed
also for multiple registers read/write.
It seems for me, that it is rather artifical decision and everything is there to support it properly.
Then send a pull request.
So you can later do
res = tete_ngt.read_registers(100, 3, 3, signed=True) # not supported in current version
It is easy to implement two's complement function and convert value for only registers you really want.
def twos_comp(val, bits):
if val & (1 << (bits - 1)):
val = val - (1 << bits)
return val
# one register
res = tete_ngt.read_registers(100, 3, 3)
res[2] = twos_comp(res[2], 16)
# all registers
res = [twos_comp(r, 16) for r in tete_ngt.read_registers(100, 3, 3)]
Instead of using private functions, just use standard ones
import struct
def signed_registers(registers):
repack = struct.pack("H"*len(registers), *registers)
return struct.unpack("h"*len(registers), repack)
res = signed_registers(tete_ngt.read_registers(100, 3, 3))
Thanks for the examples. My skills in python are limited but I will try to submit a code. then I will do the "pull request"
Hi I have to try :
Why there is in source code, line 2605, index 0 ?
Thank you