Using the Modbus TCP client

2,256 views
Skip to first unread message

Petter

unread,
Jun 29, 2011, 5:02:30 AM6/29/11
to pymodbus
Hi, i'm trying to use the Modbus TCP client to read and write some
values to the memory on my PLC-unit. This is what i'm running to test
it out:

#!/usr/bin/env python

from pymodbus.client.sync import ModbusTcpClient as ModbusClient

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

BC9000 = ModbusClient(host='192.168.1.4', port=502)

testconnect = BC9000.connect()
print(testconnect)

rq = BC9000.write_register(1, 3)

rr = BC9000.read_holding_registers(1, 1)

The program outputs the following:

True
DEBUG:pymodbus.client.sync:Running transaction 1
DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x3 0x0 0x86 0x2
DEBUG:pymodbus.factory:Factory Response[134]
DEBUG:pymodbus.client.sync:Running transaction 2
DEBUG:pymodbus.transaction:0x0 0x2 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0x0
DEBUG:pymodbus.factory:Factory Response[3]

True is good, since that indicates that the connection was successful,
but what does the rest of it mean?

I can also use software from the manufacturer of the PLC unit to read
its memory, and the values aren't changing as I want them to.

I'm pretty new to all this, so please respond in a way that you think
a new-born could understand :)

Albert Brandl

unread,
Jul 8, 2011, 3:38:54 AM7/8/11
to pymo...@googlegroups.com
Hi!

On Wed, Jun 29, 2011 at 02:02:30AM -0700, Petter wrote:
> DEBUG:pymodbus.client.sync:Running transaction 1
> DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x3 0x0 0x86 0x2
> DEBUG:pymodbus.factory:Factory Response[134]

This indicates that the request to write the register has failed:
134 is the error code for the function with number 6 (Write Single
Register). It is computed by adding 128 to the function number.

Are you sure that the second holding register is available on
the server? The first register is addressed as register _0_.

> DEBUG:pymodbus.client.sync:Running transaction 2
> DEBUG:pymodbus.transaction:0x0 0x2 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0x0
> DEBUG:pymodbus.factory:Factory Response[3]

This shows that the request to read holding registers (function code 3)
was successful. You should be able to access the values of the registers
by calling the method getRegister() of rr.

Best regards,
--
Albert Brandl
Weiermayer Solutions GmbH | Abteistra�e 12, A-4813 Altm�nster
phone: +43 (0) 720 70 30 14 | fax: +43 (0) 7612 20 3 56
web: http://www.weiermayer.com

Mesias

unread,
Sep 4, 2012, 10:52:59 AM9/4/12
to pymo...@googlegroups.com
Where i can find those answer codes ? it comes from the device ?


Em sexta-feira, 8 de julho de 2011 04h38min54s UTC-3, Albert Brandl escreveu:
Hi!

On Wed, Jun 29, 2011 at 02:02:30AM -0700, Petter wrote:
> DEBUG:pymodbus.client.sync:Running transaction 1
> DEBUG:pymodbus.transaction:0x0 0x1 0x0 0x0 0x0 0x3 0x0 0x86 0x2
> DEBUG:pymodbus.factory:Factory Response[134]

This indicates that the request to write the register has failed:
134 is the error code for the function with number 6 (Write Single
Register). It is computed by adding 128 to the function number.

Are you sure that the second holding register is available on
the server? The first register is addressed as register _0_.

> DEBUG:pymodbus.client.sync:Running transaction 2
> DEBUG:pymodbus.transaction:0x0 0x2 0x0 0x0 0x0 0x5 0x0 0x3 0x2 0x0 0x0
> DEBUG:pymodbus.factory:Factory Response[3]

This shows that the request to read holding registers (function code 3)
was successful. You should be able to access the values of the registers
by calling the method getRegister() of rr.

Best regards,
--
Albert Brandl

Weiermayer Solutions GmbH      | Abteistra�e 12, A-4813 Altm�nster

Bashwork

unread,
Sep 4, 2012, 12:10:37 PM9/4/12
to pymo...@googlegroups.com
They are defined in the modbus specification.  They are copied in the code here:  https://github.com/bashwork/pymodbus/blob/master/pymodbus/pdu.py#L125 
Reply all
Reply to author
Forward
0 new messages