diff --git a/virtualcoke.py b/virtualcoke.py index 6e3640c1098d735677efee1d3b8550b9d0456b5b..3a1596c7fa337eec96b3e0c18e063b9ccf279954 100755 --- a/virtualcoke.py +++ b/virtualcoke.py @@ -374,3 +374,127 @@ Mark Tearle, October 2014 if __name__ == "__main__": App = VirtualCokeApp() App.run() + + + +##!/usr/bin/env python +#''' +#Pymodbus Asynchronous Server Example +#-------------------------------------------------------------------------- +# +#The asynchronous server is a high performance implementation using the +#twisted library as its backend. This allows it to scale to many thousands +#of nodes which can be helpful for testing monitoring software. +#''' +##---------------------------------------------------------------------------# +## import the various server implementations +##---------------------------------------------------------------------------# +#from pymodbus.server.async import StartTcpServer +#from pymodbus.server.async import StartUdpServer +#from pymodbus.server.async import StartSerialServer +# +#from pymodbus.datastore import ModbusSequentialDataBlock, ModbusSparseDataBlock +#from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext +# +##---------------------------------------------------------------------------# +## configure the service logging +##---------------------------------------------------------------------------# +#import logging +#logging.basicConfig() +#log = logging.getLogger() +#log.setLevel(logging.DEBUG) +# +##---------------------------------------------------------------------------# +## initialize your data store +##---------------------------------------------------------------------------# +## The datastores only respond to the addresses that they are initialized to. +## Therefore, if you initialize a DataBlock to addresses of 0x00 to 0xFF, a +## request to 0x100 will respond with an invalid address exception. This is +## because many devices exhibit this kind of behavior (but not all):: +## +## block = ModbusSequentialDataBlock(0x00, [0]*0xff) +## +## Continuting, you can choose to use a sequential or a sparse DataBlock in +## your data context. The difference is that the sequential has no gaps in +## the data while the sparse can. Once again, there are devices that exhibit +## both forms of behavior:: +## +## block = ModbusSparseDataBlock({0x00: 0, 0x05: 1}) +## block = ModbusSequentialDataBlock(0x00, [0]*5) +## +## Alternately, you can use the factory methods to initialize the DataBlocks +## or simply do not pass them to have them initialized to 0x00 on the full +## address range:: +## +## store = ModbusSlaveContext(di = ModbusSequentialDataBlock.create()) +## store = ModbusSlaveContext() +## +## Finally, you are allowed to use the same DataBlock reference for every +## table or you you may use a seperate DataBlock for each table. This depends +## if you would like functions to be able to access and modify the same data +## or not:: +## +## block = ModbusSequentialDataBlock(0x00, [0]*0xff) +## store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block) +##---------------------------------------------------------------------------# +# +##---------------------------------------------------------------------------# +## create your custom data block with callbacks +##---------------------------------------------------------------------------# +#class CallbackDataBlock(ModbusSparseDataBlock): +# ''' A datablock that stores the new value in memory +# and passes the operation to a message queue for further +# processing. +# ''' +# +# def __init__(self, values): +# self.toggle = 1 +# super(CallbackDataBlock, self).__init__(values) +# +# +# def getValues(self, address, count=1): +# ''' Returns the requested values from the datastore +# +# :param address: The starting address +# :param count: The number of values to retrieve +# :returns: The requested values from a:a+c +# ''' +# log.debug("CBD getValues %d:%d" % (address, count)) +# +# if address < 1024: +# return [1] +# +# self.toggle = self.toggle+1 +# +# log.debug("CBD getValues toggle %d" % (self.toggle)) +# if self.toggle % 3 == 0: +# return [1] +# +# if self.toggle % 3 == 1: +# return [1] +# +# if self.toggle % 3 == 2: +# return [0] +# +# +# return [1] +# +# +# +#store = ModbusSlaveContext( +# #di = ModbusSequentialDataBlock(0, [17]*100), +# #co = ModbusSequentialDataBlock(0, [17]*100), +# #hr = ModbusSequentialDataBlock(0, [17]*100), +# #ir = ModbusSequentialDataBlock(0, [17]*100)) +# di = CallbackDataBlock([0]*100), +# co = CallbackDataBlock([0]*65536), +# hr = CallbackDataBlock([0]*100), +# ir = CallbackDataBlock([0]*100)) +#context = ModbusServerContext(slaves=store, single=True) +# +##---------------------------------------------------------------------------# +## run the server you want +##---------------------------------------------------------------------------# +#StartTcpServer(context) +##StartUdpServer(context) +##StartSerialServer(context, port='/dev/pts/13')