-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModbus_server.py
More file actions
29 lines (23 loc) · 1.25 KB
/
Modbus_server.py
File metadata and controls
29 lines (23 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/bin/env python3
"""Asynchronous Modbus Server Build in Python3 using the pyModbus module."""
from pymodbus.server.asynchronous import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
# create a datastore and populate it with test data
store = ModbusSlaveContext(
di=ModbusSequentialDataBlock(0, [17]*100), # Discrete inputs initializer
co=ModbusSequentialDataBlock(0, [17]*100), # Coils initializer
hr=ModbusSequentialDataBlock(0, [17]*100), # Holding register initializer
ir=ModbusSequentialDataBlock(0, [17]*100)) # Input register initializer
context = ModbusServerContext(slaves=store, single=True)
# Populate the Modbus server information fields, these gert returned as responses to identity queries
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'https://github.com/riptideio/pyModbus'
identity.ProductName = 'Modbus Server'
identity.MajorMinorRevision = '1.0'
# Start the listening server
print('Starting Modbus server...')
StartTcpServer(context, identity=identity, address=('0.0.0.0', 502))