def testExpectedLengthTooShort(self):
        """check that an error is raised if expected_length is too low"""
        self.slave1.set_values("hr0-100", 0, range(100))
        ok = True

        def check_length_hook(args):
            (master, response) = args
            LOGGER.debug("expected: %d - actual: %d",
                         check_length_hook.expected_length, len(response))
            check_length_hook.test.assertEqual(
                check_length_hook.expected_length, len(response))

        check_length_hook.test = self
        hooks.install_hook("modbus_rtu.RtuMaster.after_recv",
                           check_length_hook)

        for x in (5, 204):
            try:
                check_length_hook.expected_length = x
                self.master.execute(1,
                                    modbus_tk.defines.READ_HOLDING_REGISTERS,
                                    0,
                                    100,
                                    expected_length=x)
            except:
                pass
            else:
                ok = False
        hooks.uninstall_hook("modbus_rtu.RtuMaster.after_recv",
                             check_length_hook)

        self.assert_(ok)
Esempio n. 2
0
 def _do_uninstall_hook(self, args):
     """
     uninstall a function as a hook.
     If no function is given, uninstall all functions
     """
     hook_name = args[1]
     try:
         hooks.uninstall_hook(hook_name)
     except KeyError as exception:
         LOGGER.error(str(exception))
Esempio n. 3
0
 def _do_uninstall_hook(self, args):
     """
     uninstall a function as a hook.
     If no function is given, uninstall all functions
     """
     hook_name = args[1]
     try:
         hooks.uninstall_hook(hook_name)
     except KeyError as exception:
         LOGGER.error(str(exception))
Esempio n. 4
0
 def testExpectedLengthTooShort(self):
     """check that an error is raised if expected_length is too low"""
     self.slave1.set_values("hr0-100", 0, range(100))
     ok = True
     def check_length_hook(args):
         (master, response) = args
         LOGGER.debug("expected: %d - actual: %d", check_length_hook.expected_length, len(response))
         check_length_hook.test.assertEqual(check_length_hook.expected_length, len(response))
                 
     check_length_hook.test = self
     hooks.install_hook("modbus_rtu.RtuMaster.after_recv", check_length_hook)
     
     for x in (5, 204):
         try:
             check_length_hook.expected_length = x
             self.master.execute(1, modbus_tk.defines.READ_HOLDING_REGISTERS, 0, 100, expected_length=x)
         except: 
             pass
         else:
             ok = False
     hooks.uninstall_hook("modbus_rtu.RtuMaster.after_recv", check_length_hook)
         
     self.assert_(ok)