forked from zxlin/Modbus-PLC-Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus-server.py
executable file
·91 lines (75 loc) · 3.37 KB
/
modbus-server.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
'''
Copyright 2015 Zhi Xiang Lin
Python based Modbus server
'''
#---------------------------------------------------------------------------#
# import various server implementations
#---------------------------------------------------------------------------#
from pymodbus.server.async import StartTcpServer
from pymodbus.server.async import StartUdpServer
from pymodbus.server.async import StartSerialServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer
from twisted.internet.task import LoopingCall
import sys
import logging
import logging.handlers
#---------------------------------------------------------------------------#
# configure server parameters
#---------------------------------------------------------------------------#
server_address = "172.31.1.242" # Make sure your box can bind to this IP
server_port = 502 # default port for Modbus is 502
if len(sys.argv) == 3: # set server params if given cmd line args
server_address = sys.argv[1]
server_port = int(sys.argv[2])
#---------------------------------------------------------------------------#
# configure service logging
#---------------------------------------------------------------------------#
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler('/dev/log')
log.addHandler(handler)
#---------------------------------------------------------------------------#
# callback processes for simulating register updating on its own
#---------------------------------------------------------------------------#
# increments holding regsiters register #0x0
def updating_writer(a):
log.debug("register update simulation")
context = a[0]
register = 3
slave_id = 0x00
address = 0x0
values = context[slave_id].getValues(register, address, count=1)
values = [v + 1 for v in values]
log.debug("new values: " + str(values))
context[slave_id].setValues(register, address, values)
#---------------------------------------------------------------------------#
# initialize 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)
#---------------------------------------------------------------------------#
# initialize the server information
#---------------------------------------------------------------------------#
identity = ModbusDeviceIdentification()
identity.VendorName = 'ZXL'
identity.ProductCode = 'PLC'
identity.VendorUrl = 'https://github.com/zxlin/Modbus-PLC-Simulator'
identity.ProductName = 'PLC-Sim'
identity.ModelName = 'Modbus-Server'
identity.MajorMinorRevision = '1.0'
#---------------------------------------------------------------------------#
# Start running the server
#---------------------------------------------------------------------------#
# time = 5
# loop = LoopingCall(f=updating_writer, a=(context,))
# loop.start(time, now=False)
StartTcpServer(context, identity=identity, address=(server_address, server_port))