Help with ModbusSerialClient...

5,255 views
Skip to first unread message

Michael Williamson

unread,
Feb 17, 2011, 2:14:00 PM2/17/11
to pymo...@googlegroups.com
Hi,

I was looking at using pymodbus to interface to a slave temperature controller via a serial port using binary protocol.  I am trying to read 1 register at address 100 (16 bit value).

Does the ModbusSerialClient in sync mode work?

I am struggling a little bit with the examples and how to do this and noticed a couple of things:

- Is it possible to specify the COM port?  It looks like it's hardcoded to COM1.
- How do you specify the slave address?
- If I want to read a single register, is the code below what I need to do?

Thanks.  BTW, this looks like a very handy library.  Thanks for putting it out there...

I'll be glad to contribute, but it's been a couple years since I've written python...
-Mike
 

---
#---------------------------------------------------------------------------#
# import the various server implementations
#---------------------------------------------------------------------------#
from pymodbus.client.sync import ModbusSerialClient as ModbusClient

client = ModbusClient(port=0,stopbits=1,bytesize=8,parity=N,baudrate=19200)

# do I need to call .connect?

# How to set the slave address?
rr = client.read_input_registers(100,1)


Bashwork

unread,
Mar 2, 2011, 9:57:07 AM3/2/11
to pymodbus
Mike,

For the serial implementations we use the pyserial module which uses
relative addressing (as well as explicit addressing) to reference a
serial port. This means that you can use the values 0,1,2,..N and if
you are on windows this will correlate to COM1,COM2,COM3. You can
also just specify COM1 in the constructor(port="COM0") and that will
work fine as well.

To specify the slave address, simply set the unit parameter to
whatever slave you want to hit:
rr = client.read_input_registers(100, 1, unit=0x12)

The rest of your code looks about right (connect is called if you
haven't already called it). Let me know if you are having any other
problems.

On Feb 17, 1:14 pm, Michael Williamson <mike.a.william...@gmail.com>
wrote:

mark2526

unread,
Oct 19, 2012, 1:45:13 PM10/19/12
to pymo...@googlegroups.com
The next step here is to do something with the data, but I can't figure out how to see the data being returned. That is (in my case):
 
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import time
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
 
client = ModbusClient(method='rtu', port='com7', baudrate='115200', timeout=1)
print client
 
for x in range(1, 10):
    coil1 = client.read_coils(0, 2, unit=1)
    print coil1
    time.sleep(1)
client.close()
 
When I run this, the connection is good, my port monitor shows successful requests and responses, but "print coil1" displays "None". I apologize in advance is this is a ridiculous rookie mistake, but I can't seem to find any examples to guide me. Thanks.
--Mark

Bashwork

unread,
Nov 5, 2012, 3:14:09 PM11/5/12
to pymo...@googlegroups.com
Is anything coming out of the logging?  It seems that there may be an error in decoding the return data.

Caglar Sekmen

unread,
Aug 12, 2014, 10:13:34 AM8/12/14
to pymo...@googlegroups.com
This is what I did;

import pymodbus
import serial
from pymodbus.pdu import ModbusRequest
from pymodbus.client.sync import ModbusSerialClient as ModbusClient #initialize a serial RTU client instance
from pymodbus.transaction import ModbusRtuFramer

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

#count= the number of registers to read
#unit= the slave unit this request is targeting
#address= the starting address to read from

client= ModbusClient(method = "rtu", port="/dev/ttyUSB0",stopbits = 1, bytesize = 8, parity = 'E' baudrate= 9600)

#connect to the serial modbus server
connection = client.connect()
print connection

#starting add, num of reg to read, slave unit.
result= client.read_holding_registers(0x00,2,unit= 0xff)

print(result)

#closes the underlying socket connection
client.close()
The output I am having is shown below:
True
DEBUG:pymodbus.transaction:Running transaction 1
DEBUG:pymodbus.transaction:getting transaction 1
None
What could be the mistake here ?
Reply all
Reply to author
Forward
0 new messages