reading signed int from holding register

470 views
Skip to first unread message

volzon

unread,
Apr 8, 2011, 5:35:38 PM4/8/11
to modbus-tk
Hi.

I'm reading holding register from a Unitronics V570 Using the sample
code.

Trying to read numbers lower than 0.
- the int -800 in the PLC gives 64736 in python
- the int 1 in the PLC gives 1 in python

I guess it have to do with signed/unsigned, but I can't figure it out.

I've been using a C library earlier and had no problems, so I know the
PLC is returning the right values.

Is there a way to tell modbus-tk that I want to read signed integers?

Luc JEAN

unread,
Apr 11, 2011, 1:29:39 AM4/11/11
to modb...@googlegroups.com
Hello,

Yes it is possible.
The execute method has a data_format optional argument. It defines how to cast the received bytes into expected values.

By default, for a reading holding register, the format is ">"+quantity_of_registers*"H"
If you put something like ">"+quantity_of_registers*"h" as data_format, you should receive signed values.

See http://docs.python.org/library/struct.html#format-characters for more detail about the format

I hope it helps
Best
luc

2011/4/8 volzon <asmund...@gmail.com>


--
http://www.apidev.fr
Reply all
Reply to author
Forward
0 new messages