3
An example of creating a fully implemented modbus server
4
with read/write data as well as user configurable base data
8
from optparse import OptionParser
9
from twisted.internet import reactor
11
from pymodbus.server.async import StartTcpServer
12
from pymodbus.datastore import ModbusServerContext,ModbusSlaveContext
14
#--------------------------------------------------------------------------#
16
#--------------------------------------------------------------------------#
20
server_log = logging.getLogger("pymodbus.server")
21
protocol_log = logging.getLogger("pymodbus.protocol")
23
#---------------------------------------------------------------------------#
24
# Extra Global Functions
25
#---------------------------------------------------------------------------#
26
# These are extra helper functions that don't belong in a class
27
#---------------------------------------------------------------------------#
30
''' Simple test to see if we are running as root '''
31
return True # removed for the time being as it isn't portable
32
#return getpass.getuser() == "root"
34
#--------------------------------------------------------------------------#
36
#--------------------------------------------------------------------------#
37
class ConfigurationException(Exception):
38
''' Exception for configuration error '''
40
def __init__(self, string):
41
''' Initializes the ConfigurationException instance
43
:param string: The message to append to the exception
45
Exception.__init__(self, string)
49
''' Builds a representation of the object
51
:returns: A string representation of the object
53
return 'Configuration Error: %s' % self.string
57
Class used to parse configuration file and create and modbus
60
The format of the configuration file is actually just a
61
python pickle, which is a compressed memory dump from
65
def __init__(self, config):
67
Trys to load a configuration file, lets the file not
68
found exception fall through
70
:param config: The pickled datastore
73
self.file = open(config, "r")
75
raise ConfigurationException("File not found %s" % config)
78
''' Parses the config file and creates a server context
80
handle = pickle.load(self.file)
81
try: # test for existance, or bomb
87
raise ConfigurationException("Invalid Configuration")
88
slave = ModbusSlaveContext(d=dsd, c=csd, h=hsd, i=isd)
89
return ModbusServerContext(slaves=slave)
91
#--------------------------------------------------------------------------#
93
#--------------------------------------------------------------------------#
95
''' Server launcher '''
96
parser = OptionParser()
97
parser.add_option("-c", "--conf",
98
help="The configuration file to load",
100
parser.add_option("-D", "--debug",
101
help="Turn on to enable tracing",
102
action="store_true", dest="debug", default=False)
103
(opt, arg) = parser.parse_args()
105
# enable debugging information
108
server_log.setLevel(logging.DEBUG)
109
protocol_log.setLevel(logging.DEBUG)
111
print "Logging is not supported on this system"
113
# parse configuration file and run
115
conf = Configuration(opt.file)
116
StartTcpServer(context=conf.parse())
117
except ConfigurationException, err:
121
#---------------------------------------------------------------------------#
123
#---------------------------------------------------------------------------#
124
if __name__ == "__main__":
127
else: print "This script must be run as root!"