def setUp(self):
     """ Initializes the test environment """
     self.context = RemoteSlaveContext(client=None)  # for the log statment
     self.initialize(
         ["../tools/reference/diagslave", "-m", "tcp", "-p", "12345"])
     self.client = ModbusTcpClient(port=12345)
     self.context = RemoteSlaveContext(client=self.client)
    def testRemoteSlaveSetValues(self):
        ''' Test setting values against a remote slave context '''
        client  = mock()
        client.write_coils = lambda a,b: WriteMultipleCoilsResponse()

        context = RemoteSlaveContext(client)
        result  = context.setValues(1, 0, [1])
        self.assertTrue(True)
Beispiel #3
0
    def testRemoteSlaveSetValues(self):
        ''' Test setting values against a remote slave context '''
        client = mock()
        client.write_coils = lambda a, b: WriteMultipleCoilsResponse()

        context = RemoteSlaveContext(client)
        result = context.setValues(1, 0, [1])
        self.assertTrue(True)
    def testRemoteSlaveValidateValues(self):
        ''' Test validating against a remote slave context '''
        client  = mock()
        client.read_coils = lambda a,b: ReadCoilsResponse([1]*10)
        client.read_input_registers = lambda a,b: ReadInputRegistersResponse([10]*10)
        client.read_holding_registers = lambda a,b: ExceptionResponse(0x15)

        context = RemoteSlaveContext(client)
        result  = context.validate(1, 0, 10)
        self.assertTrue(result)

        result  = context.validate(4, 0, 10)
        self.assertTrue(result)

        result  = context.validate(3, 0, 10)
        self.assertFalse(result)
    def testRemoteSlaveGetValues(self):
        ''' Test getting values from a remote slave context '''
        client  = mock()
        client.read_coils = lambda a,b: ReadCoilsResponse([1]*10)
        client.read_input_registers = lambda a,b: ReadInputRegistersResponse([10]*10)
        client.read_holding_registers = lambda a,b: ExceptionResponse(0x15)

        context = RemoteSlaveContext(client)
        result  = context.getValues(1, 0, 10)
        self.assertEqual(result, [1]*10)

        result  = context.getValues(4, 0, 10)
        self.assertEqual(result, [10]*10)

        result  = context.getValues(3, 0, 10)
        self.assertNotEqual(result, [10]*10)
Beispiel #6
0
    def testRemoteSlaveValidateValues(self):
        ''' Test validating against a remote slave context '''
        client = mock()
        client.read_coils = lambda a, b: ReadCoilsResponse([1] * 10)
        client.read_input_registers = lambda a, b: ReadInputRegistersResponse(
            [10] * 10)
        client.read_holding_registers = lambda a, b: ExceptionResponse(0x15)

        context = RemoteSlaveContext(client)
        result = context.validate(1, 0, 10)
        self.assertTrue(result)

        result = context.validate(4, 0, 10)
        self.assertTrue(result)

        result = context.validate(3, 0, 10)
        self.assertFalse(result)
Beispiel #7
0
    def testRemoteSlaveGetValues(self):
        ''' Test getting values from a remote slave context '''
        client = mock()
        client.read_coils = lambda a, b: ReadCoilsResponse([1] * 10)
        client.read_input_registers = lambda a, b: ReadInputRegistersResponse(
            [10] * 10)
        client.read_holding_registers = lambda a, b: ExceptionResponse(0x15)

        context = RemoteSlaveContext(client)
        result = context.getValues(1, 0, 10)
        self.assertEqual(result, [1] * 10)

        result = context.getValues(4, 0, 10)
        self.assertEqual(result, [10] * 10)

        result = context.getValues(3, 0, 10)
        self.assertNotEqual(result, [10] * 10)
Beispiel #8
0
def run_serial_forwarder():
    # ----------------------------------------------------------------------- #
    # initialize the datastore(serial client)
    # ----------------------------------------------------------------------- #
    client = ModbusClient(method='ascii', port='/dev/pts/14')
    store = RemoteSlaveContext(client)
    context = ModbusServerContext(slaves=store, single=True)

    # ----------------------------------------------------------------------- #
    # run the server you want
    # ----------------------------------------------------------------------- #
    StartServer(context)
Beispiel #9
0
def run_serial_forwarder(modbusip):
    # ----------------------------------------------------------------------- #
    # initialize the datastore(serial client)
    # ----------------------------------------------------------------------- #
    client = ModbusClient(method='binary', port='/dev/ttyUSB0')
    store = RemoteSlaveContext(client)
    context = ModbusServerContext(slaves=store, single=True)

    # ----------------------------------------------------------------------- #
    # run the server you want
    # ----------------------------------------------------------------------- #
    StartServer(context, address=(modbusip, 502))
Beispiel #10
0
def run_serial_forwarder():
    # ----------------------------------------------------------------------- #
    # initialize the datastore(serial client)
    # ----------------------------------------------------------------------- #
    client = ModbusClient(method='rtu', port='/dev/ptyp0')
    store = RemoteSlaveContext(client)
    context = ModbusServerContext(slaves=store, single=True)

    # ----------------------------------------------------------------------- #
    # run the server you want
    # ----------------------------------------------------------------------- #
    StartServer(context, address=("localhost", 5020))
Beispiel #11
0
def run_serial_forwarder():
    # ----------------------------------------------------------------------- #
    # initialize the datastore(serial client)
    # Note this would send the requests on the serial client with address = 0

    # ----------------------------------------------------------------------- #
    client = ModbusClient(method='rtu', port='/tmp/ptyp0')
    # If required to communicate with a specified client use unit=<unit_id>
    # in RemoteSlaveContext
    # For e.g to forward the requests to slave with unit address 1 use
    # store = RemoteSlaveContext(client, unit=1)
    store = RemoteSlaveContext(client)
    context = ModbusServerContext(slaves=store, single=True)

    # ----------------------------------------------------------------------- #
    # run the server you want
    # ----------------------------------------------------------------------- #
    StartServer(context, address=("localhost", 5020))
 def testRemoteSlaveContext(self):
     ''' Test a modbus remote slave context '''
     context = RemoteSlaveContext(None)
     self.assertNotEqual(str(context), None)
     self.assertRaises(NotImplementedException, lambda: context.reset())
Beispiel #13
0
 def testRemoteSlaveContext(self):
     ''' Test a modbus remote slave context '''
     context = RemoteSlaveContext(None)
     self.assertNotEqual(str(context), None)
     self.assertRaises(NotImplementedException, lambda: context.reset())
Beispiel #14
0
the data context (basically anything can become a modbus device).
'''
#---------------------------------------------------------------------------# 
# import the various server implementations
#---------------------------------------------------------------------------# 
from pymodbus.server.sync import StartTcpServer as StartServer
from pymodbus.client.sync import ModbusSerialClient as ModbusClient

from pymodbus.datastore.remote import RemoteSlaveContext
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext

#---------------------------------------------------------------------------# 
# configure the service logging
#---------------------------------------------------------------------------# 
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)

#---------------------------------------------------------------------------# 
# initialize the datastore(serial client)
#---------------------------------------------------------------------------# 
client = ModbusClient(method='ascii', port='/dev/pts/14')
store = RemoteSlaveContext(client)
context = ModbusServerContext(slaves=store, single=True)

#---------------------------------------------------------------------------# 
# run the server you want
#---------------------------------------------------------------------------# 
StartServer(context)