-
Notifications
You must be signed in to change notification settings - Fork 0
/
modbus_wrapper_server.py
206 lines (171 loc) · 7.82 KB
/
modbus_wrapper_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
###########################################################################
# This software is graciously provided by HumaRobotics
# under the BSD License on
# github: https://github.com/Humarobotics/modbus_wrapper
# HumaRobotics is a trademark of Generation Robots.
# www.humarobotics.com
#
# Copyright (c) 2015, Generation Robots.
# All rights reserved.
# www.generationrobots.com
#
# This wrapper package is based on the pymodbus library developed by:
# Galen Collins
# github: https://github.com/bashwork/pymodbus
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the FreeBSD Project.
from post_threading import Post
from signals import Signal
import time
import sys
sys.path.append('/home/pi/pymodbus')
try:
from pymodbus.server.sync import ModbusSocketFramer, ModbusTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
except Exception,e:
print "-- INSTALL DEPENDENCIES -- "
print "sudo apt-get install python-pymodbus"
print "or from source:"
print "git clone [email protected]:bashwork/pymodbus.git"
print "cd pymodbus"
print "sudo python setup.py install"
print "and"
print "sudo apt-get install python-pyasn1 python-twisted-conch"
print e
exit()
ADDRESS_WRITE_START = 0
ADDRESS_READ_START = 0
class CustomHoldingRegister(ModbusSequentialDataBlock):
def __init__(self,address,value):
"""
Creates a custom holding register to add a publisher and subscriber to the modbus server
:param address: The starting address of the holding register
:type address: int
:param values: The initial values for the holding registers
:type values: list[int]
:param sub_topic: ROS topic name for the subscriber that updates the modbus registers
:type sub_topic: string
:param pub_topic: ROS topic name for the publisher that publishes a message, once there is something written to the writeable modbus registers
:type pub_topic: string
"""
super(CustomHoldingRegister,self).__init__(address,value)
self.reading_start = ADDRESS_READ_START
self.writing_start = ADDRESS_WRITE_START
def setValues(self, address, value):
"""
Sets the requested values to the holding registers
:param address: The starting address
:type address: int
:param values: The new values to be set
:type values: list[int]
"""
#print "address",address,"value",value
ModbusSequentialDataBlock.setValues(self,address, value)
def __updateWriteableRegisters(self,msg):
if len(msg.data) > self.reading_start+1: # how does this one work?
print "Message to long. Shorten it or it will be ignored"
self.setValues(self.writing_start, list(msg.data))
class ModbusWrapperServer():
def __init__(self,port=1234):
"""
Creates a Modbus TCP Server object
.. note:: The default port for modbus is 502. This modbus server uses port 1234 by default, otherwise superuser rights are required.
.. note:: Use "startServer" to start the listener.
:param port: Port for the modbus TCP server
:type port: int
"""
# chr = CustomHoldingRegister(ADDRESS_WRITE_START, [16]*100)
# cdi = CustomHoldingRegister(ADDRESS_WRITE_START, [16]*100)
self.onShutdown=False;
self.state_changed = Signal()
cco = CustomHoldingRegister(ADDRESS_WRITE_START, [16]*100)
self.store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(ADDRESS_WRITE_START, [16]*100),
co = cco,
hr = ModbusSequentialDataBlock(ADDRESS_WRITE_START, [16]*100),
ir = ModbusSequentialDataBlock(ADDRESS_WRITE_START, [16]*100))
self.context = ModbusServerContext(slaves=self.store, single=True)
self.identity = ModbusDeviceIdentification()
self.identity.VendorName = 'Pymodbus'
self.identity.ProductCode = 'PM'
self.identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
self.identity.ProductName = 'Pymodbus Server'
self.identity.ModelName = 'Pymodbus Server'
self.identity.MajorMinorRevision = '1.0'
self.store.setValues(2,0,[0]*1)
self.post = Post(self)
framer = ModbusSocketFramer
self.server = ModbusTcpServer(self.context, framer, self.identity, address=("0.0.0.0", port))
def startServer(self):
"""
Non blocking call to start the server
"""
self.post._startServer()
def _startServer(self):
print "Modbus server started"
self.server.serve_forever()
def stopServer(self):
"""
Closes the server
"""
self.onShutdown = True
self.server.server_close()
self.server.shutdown()
def waitForCoilOutput(self,address,timeout=2):
now = time.time()
while not self.onShutdown:
values = self.store.getValues(1,address, 1)
if values[0] is True:
return True
else:
if timeout > 0 and now + timeout > time.time():
time.sleep(1/50)
else:
return False
def stateChangeListener(self,address):
self.post.__stateChangeListener(address)
def __stateChangeListener(self,address):
old_state = 0;
while not self.onShutdown:
value = self.store.getValues(1,address, 1)[0]
if old_state != value:
old_state = value
self.state_changed(address,value)
#print "state has changed"
time.sleep(1/50)
def setDigitalInput(self,address,values):
if not values is list:
values = [values]
self.store.setValues(2,address,values)
def triggerInput(self,address,timeout=0.5):
self.setDigitalInput(address, True)
self.post.resetInput(address, timeout)
def resetInput(self,address,timeout):
time.sleep(timeout)
self.setDigitalInput(address, False)