writing negative value with client.write_register

1,491 views
Skip to first unread message

Khalil Tabbal

unread,
Oct 16, 2014, 4:02:54 PM10/16/14
to pymo...@googlegroups.com
Hi everyone,

I have implemented a sync client to write to register

I have a problem using this code
   
client.open()
rq
= client.write_register(52000, -10, unit=0x01)
print rq
client
.close()

I get the following error :
return struct.pack('>HH', self.address, self.value)
error: integer out of range for 'H' format code

Is there another way to write negative integers ton a register ?

Best regards,

Galen Collins

unread,
Oct 16, 2014, 4:14:46 PM10/16/14
to pymo...@googlegroups.com
Khalil,

Take a look at the payload builder which is useful for shoving various types into the modbus registers.  Here is an example you can work with:


Depending on the size of your negative value, use the builder.add_8bit_int (or 16, 32, 64).

Galen

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

Khalil Tabbal

unread,
Oct 16, 2014, 4:48:20 PM10/16/14
to pymo...@googlegroups.com
Galen,

Thank you for your quick answer, I have a look at the payload builder, I need to write 16 bit signed

my values are -360 < x < 360 

Khalil Tabbal

unread,
Oct 17, 2014, 11:35:59 AM10/17/14
to pymo...@googlegroups.com
Hello,

I am setting a target_power which is an integer -360 < target_power < 360

My working code is the following but I can only send target_power >= 0 

# -*- coding: utf-8 -*-

import sys
import logging
from pymodbus.client.sync import ModbusTcpClient as ModbusClient

target_power
= 10

logging
.basicConfig()
log
= logging.getLogger()
log
.setLevel(logging.DEBUG)


client
= ModbusClient('192.168.1.100', port=502) # client
client
.connect() # open the client
result
= client.write_register(52000, 10, unit=0x01)
print result
client
.close() # close the client


DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x6 0x20 0xcb 0xa 0x0


With payload : 

# -*- coding: utf-8 -*-

import sys
import logging
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.payload import BinaryPayloadBuilder
from pymodbus.client.sync import ModbusTcpClient as ModbusClient

target_power
= 10

logging
.basicConfig()
log
= logging.getLogger()
log
.setLevel(logging.DEBUG)


client
= ModbusClient('192.168.1.100', port=502) # client
client
.connect() # open the client


# Modbus payload building
builder
= BinaryPayloadBuilder(endian=Endian.Little)
builder
.add_16bit_int(target_power)
payload
= builder.build()


result
= client.write_registers(52000, payload, unit=0x01, skip_encode=True) # Ecriture des donnees
print result
client
.close() # close the client


I get the following debug : DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x6 0x1 0x10 0x21 0xcb 0x1 0x0

Best regards,
Reply all
Reply to author
Forward
0 new messages