Python modbus as server

1

Does anyone know how to modify this Python file to be a server instead of a client?

from pymodbus.client.sync import ModbusTcpClient

client = ModbusTcpClient('127.0.0.1')
client.write_coil(1, True)
result = client.read_coils(1,1)
print result.bits[0]
client.close()

I need it to be a server but I do not handle those books well.

    
asked by AlberM 28.11.2016 в 10:05
source

1 answer

1

You must use StartTcpServer and not ModbusTcpClient .

See the server example that use here :

#!/usr/bin/env python
from pymodbus.server.async import StartTcpServer

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

#---------------------------------------------------------------------------# 
# initialize your data store
#---------------------------------------------------------------------------# 
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)

#---------------------------------------------------------------------------# 
# run the server
#---------------------------------------------------------------------------# 
StartTcpServer(context)

Links to resources that may be of interest to you:

The simulator uses the output of the scraper to create a server with the data of a real server that has been scraping.

    
answered by 28.11.2016 в 10:32