Server time out in reposne to client request

76 views
Skip to first unread message

Kim Monks

unread,
Oct 3, 2016, 9:05:26 PM10/3/16
to pymodbus
Hello

I am new to pymodbus so please forgive my ignorance.

I am trying to set up a simulation of a number of devices over TCP to test anomaly detection (the plan is to feed readings from a model to the various slaves). My first step has been to test run a client and server. I have been able to run a test pymodbus client against a modbus slave from modbustools.

I cannot get it to run against the pymodbus server in the same environment as I get a timeout. (ERROR:pymodbus.client.sync:Connection to (192.168.0.14, 502) failed: timed out)

I am using the python 3 branch.

Have I misunderstood something? 

Thanks for any assistance
Kim

Code follows from the example

Server


#!/usr/bin/env python
'''
Pymodbus Synchronous Server Example
--------------------------------------------------------------------------

The synchronous server is implemented in pure python without any third
party libraries (unless you need to use the serial protocols which require
pyserial). This is helpful in constrained or old environments where using
twisted just is not feasable. What follows is an examle of its use:
'''
#---------------------------------------------------------------------------#
# import the various server implementations
#---------------------------------------------------------------------------#
from pymodbus.server.sync import StartTcpServer
#from pymodbus.server.sync import StartUdpServer
#from pymodbus.server.sync import StartSerialServer

from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

#---------------------------------------------------------------------------#
# configure the service logging
#---------------------------------------------------------------------------#
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------#
# initialize your data store
#---------------------------------------------------------------------------#
# The datastores only respond to the addresses that they are initialized to.
# Therefore, if you initialize a DataBlock to addresses of 0x00 to 0xFF, a
# request to 0x100 will respond with an invalid address exception. This is
# because many devices exhibit this kind of behavior (but not all)::
#
#     block = ModbusSequentialDataBlock(0x00, [0]*0xff)
#
# Continuting, you can choose to use a sequential or a sparse DataBlock in
# your data context.  The difference is that the sequential has no gaps in
# the data while the sparse can. Once again, there are devices that exhibit
# both forms of behavior::
#
#     block = ModbusSparseDataBlock({0x00: 0, 0x05: 1})
#     block = ModbusSequentialDataBlock(0x00, [0]*5)
#
# Alternately, you can use the factory methods to initialize the DataBlocks
# or simply do not pass them to have them initialized to 0x00 on the full
# address range::
#
#     store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create())
#     store = ModbusSlaveContext()
#
# Finally, you are allowed to use the same DataBlock reference for every
# table or you you may use a seperate DataBlock for each table. This depends
# if you would like functions to be able to access and modify the same data
# or not::
#
#     block = ModbusSequentialDataBlock(0x00, [0]*0xff)
#     store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
#
# The server then makes use of a server context that allows the server to
# respond with different slave contexts for different unit ids. By default
# it will return the same context for every unit id supplied (broadcast
# mode). However, this can be overloaded by setting the single flag to False
# and then supplying a dictionary of unit id to context mapping::
#
#     slaves  = {
#         0x01: ModbusSlaveContext(...),
#         0x02: ModbusSlaveContext(...),
#         0x03: ModbusSlaveContext(...),
#     }
#     context = ModbusServerContext(slaves=slaves, single=False)
#---------------------------------------------------------------------------#
store = ModbusSlaveContext(
    di = ModbusSequentialDataBlock(0, [17]*100),
    co = ModbusSequentialDataBlock(0, [17]*100),
    hr = ModbusSequentialDataBlock(0, [17]*100),
    ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------#
# initialize the server information
#---------------------------------------------------------------------------#
# If you don't set this or any fields, they are defaulted to empty strings.
#---------------------------------------------------------------------------#
identity = ModbusDeviceIdentification()
identity.VendorName  = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl   = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName   = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'

#---------------------------------------------------------------------------#
# run the server you want
#---------------------------------------------------------------------------#
StartTcpServer(context, identity=identity, address=("localhost", 502))
#StartUdpServer(context, identity=identity, address=("localhost", 502))
#StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1)


and the client script follows

#!/usr/bin/env python
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
#---------------------------------------------------------------------------#
# configure the client logging
#---------------------------------------------------------------------------#
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#---------------------------------------------------------------------------#
# We are going to use a simple client to send our requests
#---------------------------------------------------------------------------#
client = ModbusClient('192.168.0.14',port=502)

client.connect()
controller = 0x01
address = 0x01
count   = 8
result  = client.read_holding_registers(address, count, unit=controller)
print (result.registers)

client.close()





















































































#StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1)#StartUdpServer(context, identity=identity, address=("localhost", 502))StartTcpServer(context, identity=identity, address=("localhost", 502))#---------------------------------------------------------------------------# # run the server you want#---------------------------------------------------------------------------# identity.MajorMinorRevision = '1.0'identity.ModelName   = 'Pymodbus Server'identity.ProductName = 'Pymodbus Server'identity.VendorUrl   = 'http://github.com/bashwork/pymodbus/'identity.ProductCode = 'PM'identity.VendorName  = 'Pymodbus'identity = ModbusDeviceIdentification()#---------------------------------------------------------------------------# # If you don't set this or any fields, they are defaulted to empty strings.#---------------------------------------------------------------------------# # initialize the server information#---------------------------------------------------------------------------# context = ModbusServerContext(slaves=store, single=True)    ir = ModbusSequentialDataBlock(0, [17]*100))    hr = ModbusSequentialDataBlock(0, [17]*100),    co = ModbusSequentialDataBlock(0, [17]*100),    di = ModbusSequentialDataBlock(0, [17]*100),store = ModbusSlaveContext(#---------------------------------------------------------------------------# #     context = ModbusServerContext(slaves=slaves, single=False)#     }#         0x03: ModbusSlaveContext(...),#         0x02: ModbusSlaveContext(...),#         0x01: ModbusSlaveContext(...),#     slaves  = {## and then supplying a dictionary of unit id to context mapping::# mode). However, this can be overloaded by setting the single flag to False# it will return the same context for every unit id supplied (broadcast# respond with different slave contexts for different unit ids. By default# The server then makes use of a server context that allows the server to##     store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)#     block = ModbusSequentialDataBlock(0x00, [0]*0xff)## or not::# if you would like functions to be able to access and modify the same data# table or you you may use a seperate DataBlock for each table. This depends# Finally, you are allowed to use the same DataBlock reference for every##     store = ModbusSlaveContext()#     store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create())## address range::# or simply do not pass them to have them initialized to 0x00 on the full# Alternately, you can use the factory methods to initialize the DataBlocks##     block = ModbusSequentialDataBlock(0x00, [0]*5)#     block = ModbusSparseDataBlock({0x00: 0, 0x05: 1})## both forms of behavior::# the data while the sparse can. Once again, there are devices that exhibit# your data context.  The difference is that the sequential has no gaps in# Continuting, you can choose to use a sequential or a sparse DataBlock in##     block = ModbusSequentialDataBlock(0x00, [0]*0xff)## because many devices exhibit this kind of behavior (but not all)::# request to 0x100 will respond with an invalid address exception. This is# Therefore, if you initialize a DataBlock to addresses of 0x00 to 0xFF, a# The datastores only respond to the addresses that they are initialized to.#---------------------------------------------------------------------------# # initialize your data store#---------------------------------------------------------------------------# log.setLevel(logging.DEBUG)log = logging.getLogger()logging.basicConfig()import logging#---------------------------------------------------------------------------# # configure the service logging#---------------------------------------------------------------------------# from pymodbus.datastore import ModbusSlaveContext, ModbusServerContextfrom pymodbus.datastore import ModbusSequentialDataBlockfrom pymodbus.device import ModbusDeviceIdentification#from pymodbus.server.sync import StartSerialServer#from pymodbus.server.sync import StartUdpServerfrom pymodbus.server.sync import StartTcpServer#---------------------------------------------------------------------------# # import the various server implementations#---------------------------------------------------------------------------# '''twisted just is not feasable. What follows is an examle of its use:pyserial). This is helpful in constrained or old environments where usingparty libraries (unless you need to use the serial protocols which requireThe synchronous server is implemented in pure python without any third--------------------------------------------------------------------------Pymodbus Synchronous Server Example'''#!/usr/bin/env python


Galen Collins

unread,
Oct 4, 2016, 3:11:29 PM10/4/16
to pymo...@googlegroups.com
You are binding the server to localhost, perhaps change it to "0.0.0.0" or the public ip address you want to be exposed on "192.168.0.14".

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages