Reputation: 11
We have been trying to integrate a conveyor into our application. The supplier told us that they have a modbus master that updates the values every 10ms and that we should have a slave on our end on a certain IP/port. Upon investigating, we discovered that we need to spin up a modbus tcp server. We have tried to use:
PyModbusTCP
from pyModbusTCP.server import ModbusServer
server = ModbusServer(host="", port=502)
server.start()
pymodbus:
import logging
import asyncio
from pymodbus.version import version
from pymodbus.server.async_io import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.datastore import ModbusSequentialDataBlock
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
# --------------------------------------------------------------------------- #
# configure the service logging
# --------------------------------------------------------------------------- #
FORMAT = ('%(asctime)-15s %(threadName)-15s'
' %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s')
logging.basicConfig(format=FORMAT)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
async def run_server(host: str = '10.0.0.2',
port: int = 502,
debug: bool = True):
"""Run server."""
logger.debug(f'Starting server at host="{host}", port="{port}"')
store = ModbusSlaveContext(
di=ModbusSequentialDataBlock.create(),
co=ModbusSequentialDataBlock.create(),
hr=ModbusSequentialDataBlock.create(),
ir=ModbusSequentialDataBlock.create(),
zero_mode=True,
)
context = ModbusServerContext(slaves=store, single=True)
server = await StartTcpServer(
context,
address=(host, port), # nosec
allow_reuse_address=True,
defer_start=True,
console=debug,
)
asyncio.get_event_loop().call_later(20, lambda: server.serve_forever)
await server.serve_forever()
if __name__ == '__main__':
host: str = ""
port: int = 502
debug: bool = 1
asyncio.run(run_server(host, port, debug))
For both of them:
However, the values written by the second client does not get updated in the master. In fact, the supplier says that they cannot see any values at all although they can see that the modbus connection is active.
The logs from the server(slave) while writing from second client have an error which we do not know what it is:
2022-06-22 12:38:11,796 MainThread DEBUG selector_events:59 Using selector: EpollSelector
2022-06-22 12:38:11,796 MainThread DEBUG server :93 Starting server at host="", port="503"
2022-06-22 12:38:19,008 MainThread DEBUG async_io :76 Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,008 MainThread DEBUG async_io :282 TCP client connection established [10.42.0.148:60480]
2022-06-22 12:38:19,008 MainThread DEBUG async_io :76 Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,008 MainThread DEBUG async_io :282 TCP client connection established [10.42.0.148:60478]
2022-06-22 12:38:19,008 MainThread DEBUG async_io :168 Handling data: 0x4d 0x4b 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:19,008 MainThread DEBUG socket_framer :147 Processing: 0x4d 0x4b 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:19,008 MainThread DEBUG factory :137 Factory Request[ReadHoldingRegistersRequest: 3]
2022-06-22 12:38:19,008 MainThread DEBUG context :63 validate: fc-[3] address-24576: count-1
2022-06-22 12:38:19,008 MainThread DEBUG context :77 getValues fc-[3] address-24576: count-1
2022-06-22 12:38:19,009 MainThread DEBUG async_io :233 send: [ReadHoldingRegistersResponse (1)]- b'4d4b000000050103020000'
2022-06-22 12:38:19,009 MainThread DEBUG async_io :76 Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,009 MainThread DEBUG async_io :282 TCP client connection established [10.42.0.148:60482]
2022-06-22 12:38:19,009 MainThread DEBUG async_io :76 Socket [10.42.0.149:503] opened
2022-06-22 12:38:19,009 MainThread DEBUG async_io :282 TCP client connection established [10.42.0.148:60484]
2022-06-22 12:38:19,009 MainThread DEBUG async_io :168 Handling data: 0xd2 0x19 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:19,009 MainThread DEBUG socket_framer :147 Processing: 0xd2 0x19 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:19,009 MainThread DEBUG factory :137 Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:19,010 MainThread DEBUG context :63 validate: fc-[1] address-16384: count-1
2022-06-22 12:38:19,010 MainThread DEBUG context :77 getValues fc-[1] address-16384: count-1
2022-06-22 12:38:19,010 MainThread DEBUG async_io :233 send: [ReadCoilsResponse(1)]- b'd2190000000401010100'
2022-06-22 12:38:19,012 MainThread DEBUG async_io :168 Handling data: 0xec 0x78 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:19,012 MainThread DEBUG socket_framer :147 Processing: 0xec 0x78 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:19,012 MainThread DEBUG factory :137 Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:19,012 MainThread DEBUG context :63 validate: fc-[1] address-16385: count-1
2022-06-22 12:38:19,012 MainThread DEBUG context :77 getValues fc-[1] address-16385: count-1
2022-06-22 12:38:19,012 MainThread DEBUG async_io :233 send: [ReadCoilsResponse(1)]- b'ec780000000401010100'
2022-06-22 12:38:20,011 MainThread DEBUG async_io :168 Handling data: 0xf 0x14 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0x0 0x0
2022-06-22 12:38:20,011 MainThread DEBUG socket_framer :147 Processing: 0xf 0x14 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0x0 0x0
2022-06-22 12:38:20,011 MainThread DEBUG factory :137 Factory Request[WriteSingleCoilRequest: 5]
2022-06-22 12:38:20,011 MainThread DEBUG context :63 validate: fc-[5] address-0: count-1
2022-06-22 12:38:20,011 MainThread DEBUG context :90 setValues[5] 0:1
2022-06-22 12:38:20,011 MainThread DEBUG context :77 getValues fc-[5] address-0: count-1
2022-06-22 12:38:20,011 MainThread DEBUG async_io :233 send: [WriteCoilResponse(0) => 0]- b'0f1400000006010500000000'
2022-06-22 12:38:20,012 MainThread DEBUG async_io :168 Handling data: 0x1a 0x83 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:20,012 MainThread DEBUG socket_framer :147 Processing: 0x1a 0x83 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:20,012 MainThread DEBUG factory :137 Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:20,012 MainThread DEBUG context :63 validate: fc-[1] address-16384: count-1
2022-06-22 12:38:20,012 MainThread DEBUG context :77 getValues fc-[1] address-16384: count-1
2022-06-22 12:38:20,013 MainThread DEBUG async_io :233 send: [ReadCoilsResponse(1)]- b'1a830000000401010100'
2022-06-22 12:38:21,014 MainThread DEBUG async_io :168 Handling data: 0xd1 0x28 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:21,014 MainThread DEBUG socket_framer :147 Processing: 0xd1 0x28 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
2022-06-22 12:38:21,014 MainThread DEBUG factory :137 Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:21,014 MainThread DEBUG context :63 validate: fc-[1] address-16384: count-1
2022-06-22 12:38:21,014 MainThread DEBUG context :77 getValues fc-[1] address-16384: count-1
2022-06-22 12:38:21,014 MainThread DEBUG async_io :233 send: [ReadCoilsResponse(1)]- b'd1280000000401010100'
2022-06-22 12:38:21,015 MainThread DEBUG async_io :76 Socket [10.42.0.149:503] opened
2022-06-22 12:38:21,015 MainThread DEBUG async_io :282 TCP client connection established [10.42.0.148:60512]
2022-06-22 12:38:21,015 MainThread DEBUG async_io :168 Handling data: 0x80 0xb6 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2022-06-22 12:38:21,015 MainThread DEBUG socket_framer :147 Processing: 0x80 0xb6 0x0 0x0 0x0 0x6 0x1 0x5 0x0 0x0 0xff 0x0
2022-06-22 12:38:21,015 MainThread DEBUG factory :137 Factory Request[WriteSingleCoilRequest: 5]
2022-06-22 12:38:21,015 MainThread DEBUG context :63 validate: fc-[5] address-0: count-1
2022-06-22 12:38:21,015 MainThread DEBUG context :90 setValues[5] 0:1
2022-06-22 12:38:21,016 MainThread DEBUG context :77 getValues fc-[5] address-0: count-1
2022-06-22 12:38:21,016 MainThread DEBUG async_io :233 send: [WriteCoilResponse(0) => 1]- b'80b60000000601050000ff00'
2022-06-22 12:38:21,016 MainThread ERROR async_io :54 Handler for stream [10.42.0.148:60512] has been canceled
2022-06-22 12:38:21,016 MainThread DEBUG async_io :291 TCP client disconnected [10.42.0.148:60512]
2022-06-22 12:38:21,016 MainThread ERROR async_io :54 Handler for stream [10.42.0.148:60512] has been canceled
2022-06-22 12:38:22,016 MainThread DEBUG async_io :168 Handling data: 0xd1 0x8 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:22,016 MainThread DEBUG socket_framer :147 Processing: 0xd1 0x8 0x0 0x0 0x0 0x6 0x1 0x3 0x60 0x0 0x0 0x1
2022-06-22 12:38:22,016 MainThread DEBUG factory :137 Factory Request[ReadHoldingRegistersRequest: 3]
2022-06-22 12:38:22,016 MainThread DEBUG context :63 validate: fc-[3] address-24576: count-1
2022-06-22 12:38:22,016 MainThread DEBUG context :77 getValues fc-[3] address-24576: count-1
2022-06-22 12:38:22,016 MainThread DEBUG async_io :233 send: [ReadHoldingRegistersResponse (1)]- b'd108000000050103020000'
2022-06-22 12:38:22,016 MainThread DEBUG async_io :168 Handling data: 0xe4 0x7f 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:22,016 MainThread DEBUG socket_framer :147 Processing: 0xe4 0x7f 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x1 0x0 0x1
2022-06-22 12:38:22,016 MainThread DEBUG factory :137 Factory Request[ReadCoilsRequest: 1]
2022-06-22 12:38:22,017 MainThread DEBUG context :63 validate: fc-[1] address-16385: count-1
2022-06-22 12:38:22,017 MainThread DEBUG context :77 getValues fc-[1] address-16385: count-1
2022-06-22 12:38:22,017 MainThread DEBUG async_io :233 send: [ReadCoilsResponse(1)]- b'e47f0000000401010100'
2022-06-22 12:38:22,017 MainThread DEBUG async_io :168 Handling data: 0x4f 0x95 0x0 0x0 0x0 0x6 0x1 0x1 0x40 0x0 0x0 0x1
Any help would be highly appreciated! Thanks in advance!
Upvotes: 0
Views: 621
Reputation: 11
This is now resolved. The problem was that the supplier had set it up for a hardware connection, so they were using different functions to read:
Whereas they were using FC2s and FC4s, etc. Hence the master could not read our values.
Upvotes: 1
Reputation: 18400
From your description it's not really clear what the problem is (as you say " have an error which we do not know what it is"). The logs from the server don't show any errors but they do show something that might explain what you are seeing.
The Write Single Coil request is: 0f 14 00 00 00 06 01 05 00 00 00 00
. This is a request to set the value of coil 01
(physical 00
) to off
.
Then we see a read request 1a 83 00 00 00 06 01 01 40 00 00 01
. This is request for 1 coil starting at coil 0x4001
(physical 0x4000
).
So it appears that you are not accessing the same coil (writing to 0x01
but reading from 0x4001
(so not a surprise that the value has not changed). Note that I've only quickly scanned the logs so may have missed something.
Upvotes: 0