signed and signed floating point values

671 views
Skip to first unread message

Wolfgang Bertram

unread,
Mar 6, 2014, 11:27:31 AM3/6/14
to modb...@googlegroups.com
Excuse my second question but I'm new at the topic Modbus.

When I have Values like this:

1234.456

this:
-12.456

or this:
-12

I meen signed values and signed floating point values

Which possibility’s I have to provide them in one Register for each value?

Does anyone have examples for this?

Thanks for the help and best regards

Keith Gray

unread,
Mar 6, 2014, 11:36:44 AM3/6/14
to modb...@googlegroups.com
Wolfgang,

Modbus requires that you use 2 registers for floating point numbers. If you want to stick with 1 register, you can keep using 1 register but you will lose precision. In your first example, if you wanted the client to display a value with 1 decimal place, you would need to multiply the value (1234.456) by 10 before sending it out over Modbus. The Modbus slave would truncate the decimal and send a value of 12344. The client would then have to divide by 10 in order to get the value 1234.4 back out.

Signed numbers can be sent and received as long as both sides are expecting it and can interpret it correctly.

Keith


--
You received this message because you are subscribed to the Google Groups "modbus-tk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modbus-tk+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Wolfgang Bertram

unread,
Mar 6, 2014, 12:08:41 PM3/6/14
to modb...@googlegroups.com
Keith,
Thanks for the quick and very accurate assistance.
Kind regards

Wolfgang Bertram

unread,
Mar 8, 2014, 6:30:48 AM3/8/14
to modb...@googlegroups.com
Keith,
Excuse the additional question.

As an example wehen I Built this Value:

VAL02 = -1234

And write it in an Register:

slave.set_values("a", 1, VAL02)

I get back as Failure:

handle request failed: integer out of range for 'H' format code

Python Interprets it as an int.

When I build the Value:

VAL02= long(-1234)

I get back the same Error.

What am I doing wrong?
Thank you for your patient assistance
Wolfgang

Keith Gray

unread,
Mar 8, 2014, 12:55:09 PM3/8/14
to modb...@googlegroups.com
I haven't reviewed the source for modbus-tk in quite a while but that error is related to the python struct module. The documentation is here: http://docs.python.org/2/library/struct.html. The 'H' code attempts to interpret a unsigned short value. that is the reason it doesn't like signed values.

Can you post the entire stack trace?

Keith


For more options, visit https://groups.google.com/d/optout.

Wolfgang Bertram

unread,
Mar 8, 2014, 2:25:21 PM3/8/14
to modb...@googlegroups.com
Good evening Keith,
The whole issue is


sudo python mudbus_server_00_00_03.py
VAL01: 1234
<type 'int'>
VAL02: -1234
<type 'int'>
('192.168.0.111', 49775) is connected with socket 4...

handle request failed: integer out of range for 'H' format code
4 is disconnected

Is there a chance to get a more accurate output (I did not have much experience in Python)?
Wolfgang

Åsmund Rabbe

unread,
Mar 8, 2014, 2:54:51 PM3/8/14
to modb...@googlegroups.com
Hi.

Think I had the sam problem some time back...
Try this for writing single word:

if VAL02 < 0: VAL02 = 65536 + VAL02

read single and double words:

    def u2s(unsignedInt):
        if int(unsignedInt) > 32767: return (65536 - int(unsignedInt)) * -1
        else: return int(unsignedInt)
 
    def du2s(unsignedInt):
        if int(unsignedInt) > 2147483647: return (4294967296 - int(unsignedInt)) * -1
        else: return int(unsignedInt)

Åsmund

Wolfgang Bertram

unread,
Mar 8, 2014, 5:15:10 PM3/8/14
to modb...@googlegroups.com
Hello Åsmund
Thank you for help.

The Calculation with the range are clever and a good solution.

My Problem are tha the reading part are only for testing. The reading is done by a very strange system to which I have unfortunately no influence. The
 requirement for this part is unfortunately very closely.

Luc JEAN

unread,
Mar 10, 2014, 3:17:28 AM3/10/14
to modb...@googlegroups.com
Hello guys,

The execute method has a 'data_format' argument which allows to change the format ('h' can be used rather than 'H' for getting signed values).

I hope it helps
Best
luc


--

Wolfgang Bertram

unread,
Mar 10, 2014, 7:49:13 AM3/10/14
to modb...@googlegroups.com
Hello Luc,
Can you describe to me a little more detail?
Best
Wolfgang

Vinod Kumar

unread,
Feb 21, 2015, 3:49:52 AM2/21/15
to modb...@googlegroups.com
Hello,
The problem arises because python natively stores numbers with unlimited precision. For example:
python
>>> c=123456790123456790123456790
>>> c
123456790123456790123456790L
Python would be allocating storage in basic units of 8 or 16 bit words. If a number cannot be represented in a word, it will allocate further words.
But modbus talks about 16 bit signed integers and 32 bit signed fixed point (not floating point) numbers.

Consider the python code for -1234

python
>>> a=-1234
>>> c = a & 0xFFFFFFFF
>>> print c
4294966062
>>> print "%X"%(c)
FFFFFB2E

Here we mask the integer a by a 32 bit all one number (0xFFFFFFFF) and the result becomes positive (4294966062). This will not happen in programming language c.

How to convert from a modbus representation to python representation will be shown in another mail.

esch...@gmail.com

unread,
Feb 25, 2015, 6:00:45 AM2/25/15
to modb...@googlegroups.com

I do it that way:


Hope it helps...

BR
Reply all
Reply to author
Forward
0 new messages