forked from agtic/chronos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
working-sync-server.py
51 lines (43 loc) · 1.96 KB
/
working-sync-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
from pymodbus.server.sync import StartSerialServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer
import logging
import sys
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
#---------------------------------------------------------------------------#
# 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/riptideio/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'
def setup_server():
datablock = ModbusSequentialDataBlock(address=6, values=[1]*16)
inpblock = ModbusSequentialDataBlock(address=3, values=[1]*16)
slave_1 = ModbusSlaveContext(
di = datablock,
co = datablock,
hr = datablock,
ir = inpblock,
)
store = slave_1
context = ModbusServerContext(slaves=store, single=True)
#---------------------------------------------------------------------------#
# run the server you want
#---------------------------------------------------------------------------#
# RTU:
print ("address: 0x00, value=" + str(context[1].getValues(3, 0)))
print ("address: 0x01, value=" + str(context[1].getValues(3, 1)))
return context
if __name__ == "__main__":
ctx = setup_server()
server = StartSerialServer(context=ctx, framer=ModbusRtuFramer, identity=identity, port=sys.argv[1], timeout=2, baudrate=9600, startbit=1, databits=1, stopbit=2, bytesize=8, parity='N')