Skip to content

Commit

Permalink
updating documentation and being pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
bashwork committed Jul 24, 2012
1 parent 65554bc commit f96541d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 39 deletions.
22 changes: 18 additions & 4 deletions examples/common/asynchronous-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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
Expand Down Expand Up @@ -79,10 +80,23 @@
ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------#
# 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/bashwork/pymobdbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'

#---------------------------------------------------------------------------#
# run the server you want
#---------------------------------------------------------------------------#
StartTcpServer(context)
#StartUdpServer(context)
#StartSerialServer(context, port='/dev/pts/3', framer=ModbusRtuFramer)
#StartSerialServer(context, port='/dev/pts/3', framer=ModbusAsciiFramer)
StartTcpServer(context, identity=identity, address=("localhost", 5020))
#StartUdpServer(context, identity=identity, address=("localhost", 502))
#StartSerialServer(context, identity=identity, port='/dev/pts/3', framer=ModbusRtuFramer)
#StartSerialServer(context, identity=identity, port='/dev/pts/3', framer=ModbusAsciiFramer)
20 changes: 17 additions & 3 deletions examples/common/synchronous-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pymodbus.server.sync import StartUdpServer
from pymodbus.server.sync import StartSerialServer

from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

Expand Down Expand Up @@ -79,9 +80,22 @@
ir = ModbusSequentialDataBlock(0, [17]*100))
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------#
# 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/bashwork/pymobdbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.0'

#---------------------------------------------------------------------------#
# run the server you want
#---------------------------------------------------------------------------#
#StartTcpServer(context)
#StartUdpServer(context)
StartSerialServer(context, port='/dev/pts/3', timeout=1)
StartTcpServer(context, identity=identity, address=("localhost", 502))
#StartUdpServer(context, identity=identity, address=("localhost", 502))
#StartSerialServer(context, identity=identity, port='/dev/pts/3', timeout=1)
32 changes: 17 additions & 15 deletions pymodbus/server/async.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,38 +187,40 @@ def _send(self, message, addr):
#---------------------------------------------------------------------------#
# Starting Factories
#---------------------------------------------------------------------------#
def StartTcpServer(context, identity=None, server_address=None):
def StartTcpServer(context, identity=None, address=None):
''' Helper method to start the Modbus Async TCP server
:param context: The server data context
:param identify: The server identity to use (default empty)
:param server_address: An optional (interface,port) to bind to.
:param address: An optional (interface, port) to bind to.
'''
from twisted.internet import reactor
if not server_address:
server_address = ("", Defaults.Port)
_logger.info("Starting Modbus TCP Server on %s:%s" % server_address)
framer = ModbusSocketFramer

address = address or ("", Defaults.Port)
framer = ModbusSocketFramer
factory = ModbusServerFactory(context, framer, identity)
InstallManagementConsole({'factory': factory})
reactor.listenTCP(server_address[1], factory, interface=server_address[0])

_logger.info("Starting Modbus TCP Server on %s:%s" % address)
reactor.listenTCP(address[1], factory, interface=address[0])
reactor.run()


def StartUdpServer(context, identity=None, server_address=None):
def StartUdpServer(context, identity=None, address=None):
''' Helper method to start the Modbus Async Udp server
:param context: The server data context
:param identify: The server identity to use (default empty)
:param server_address: An optional (interface,port) to bind to.
:param address: An optional (interface, port) to bind to.
'''
from twisted.internet import reactor
if not server_address:
server_address = ("", Defaults.Port)
_logger.info("Starting Modbus UDP Server on %s:%s" % server_address)
framer = ModbusSocketFramer
server = ModbusUdpProtocol(context, framer, identity)
reactor.listenUDP(server_address[1], server, interface=server_address[0])

address = address or ("", Defaults.Port)
framer = ModbusSocketFramer
server = ModbusUdpProtocol(context, framer, identity)

_logger.info("Starting Modbus UDP Server on %s:%s" % address)
reactor.listenUDP(address[1], server, interface=address[0])
reactor.run()


Expand Down
32 changes: 15 additions & 17 deletions pymodbus/server/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ class ModbusTcpServer(SocketServer.ThreadingTCPServer):
server context instance.
'''

def __init__(self, context, framer=None, identity=None,
server_address=None):
def __init__(self, context, framer=None, identity=None, address=None):
''' Overloaded initializer for the socket server
If the identify structure is not passed in, the ModbusControlBlock
Expand All @@ -211,20 +210,20 @@ def __init__(self, context, framer=None, identity=None,
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure
:param server_address: An optional (interface,port) to bind to.
:param address: An optional (interface, port) to bind to.
'''
self.threads = []
self.decoder = ServerDecoder()
self.framer = framer or ModbusSocketFramer
self.context = context or ModbusServerContext()
self.control = ModbusControlBlock()
self.address = address or ("", Defaults.Port)

if isinstance(identity, ModbusDeviceIdentification):
self.control.Identity.update(identity)

SocketServer.ThreadingTCPServer.__init__(self,
server_address or ("", Defaults.Port),
ModbusConnectedRequestHandler)
self.address, ModbusConnectedRequestHandler)

def process_request(self, request, client):
''' Callback for connecting a new client thread
Expand Down Expand Up @@ -253,8 +252,7 @@ class ModbusUdpServer(SocketServer.ThreadingUDPServer):
server context instance.
'''

def __init__(self, context, framer=None, identity=None,
server_address=None):
def __init__(self, context, framer=None, identity=None, address=None):
''' Overloaded initializer for the socket server
If the identify structure is not passed in, the ModbusControlBlock
Expand All @@ -263,20 +261,20 @@ def __init__(self, context, framer=None, identity=None,
:param context: The ModbusServerContext datastore
:param framer: The framer strategy to use
:param identity: An optional identify structure
:param server_address: An optional (interface,port) to bind to.
:param address: An optional (interface, port) to bind to.
'''
self.threads = []
self.decoder = ServerDecoder()
self.framer = framer or ModbusSocketFramer
self.context = context or ModbusServerContext()
self.control = ModbusControlBlock()
self.address = address or ("", Defaults.Port)

if isinstance(identity, ModbusDeviceIdentification):
self.control.Identity.update(identity)

SocketServer.ThreadingUDPServer.__init__(
self, server_address or ("", Defaults.Port),
ModbusDisconnectedRequestHandler)
SocketServer.ThreadingUDPServer.__init__(self,
self.address, ModbusDisconnectedRequestHandler)

def process_request(self, request, client):
''' Callback for connecting a new client thread
Expand Down Expand Up @@ -391,27 +389,27 @@ def server_close(self):
#---------------------------------------------------------------------------#
# Creation Factories
#---------------------------------------------------------------------------#
def StartTcpServer(context=None, identity=None, server_address=None):
def StartTcpServer(context=None, identity=None, address=None):
''' A factory to start and run a tcp modbus server
:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param server_address: An optional (interface,port) to bind to.
:param address: An optional (interface, port) to bind to.
'''
framer = ModbusSocketFramer
server = ModbusTcpServer(context, framer, identity, server_address)
server = ModbusTcpServer(context, framer, identity, address)
server.serve_forever()


def StartUdpServer(context=None, identity=None, server_address=None):
def StartUdpServer(context=None, identity=None, address=None):
''' A factory to start and run a udp modbus server
:param context: The ModbusServerContext datastore
:param identity: An optional identify structure
:param server_address: An optional (interface,port) to bind to.
:param address: An optional (interface, port) to bind to.
'''
framer = ModbusSocketFramer
server = ModbusUdpServer(context, framer, identity, server_address)
server = ModbusUdpServer(context, framer, identity, address)
server.serve_forever()


Expand Down

0 comments on commit f96541d

Please sign in to comment.