Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def testWrongInputValue(self):
for LsbFirst in [False, True]:
# Range 0-65535
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 77, -1, LsbFirst)
if _runTestsForNewVersion: # For compatibility with Python2.6
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 77000, 0, LsbFirst) # Gives DeprecationWarning instead of ValueError for Python 2.6
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 65536, 0, LsbFirst)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 77, 4, LsbFirst)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, -1, 0, LsbFirst)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, -77, 1, LsbFirst)
# Range -32768 to 32767
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 77, -1, LsbFirst, True)
if _runTestsForNewVersion: # For compatibility with Python2.6
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, -77000, 0, LsbFirst, True) # Gives DeprecationWarning instead of ValueError for Python 2.6
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, -32769, 0, LsbFirst, True)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 32768, 0, LsbFirst, True)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 77000, 0, LsbFirst, True)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, 77, 4, LsbFirst, True)
self.assertRaises(ValueError, minimalmodbus._numToTwoByteString, -77, 4, LsbFirst, True)
def testKnownValues(self):
for inputvalue, numberOfDecimals, LsbFirst, signed, knownstring in self.knownValues:
resultstring = minimalmodbus._numToTwoByteString(inputvalue, numberOfDecimals, LsbFirst, signed)
self.assertEqual(resultstring, knownstring)
def testSanity(self):
for value, numberOfDecimals, LsbFirst, signed, bytestring in self.knownValues:
if not LsbFirst:
resultvalue = minimalmodbus._twoByteStringToNum( \
minimalmodbus._numToTwoByteString(value, numberOfDecimals, LsbFirst, signed), \
numberOfDecimals, signed )
self.assertEqual(resultvalue, value)
if ALSO_TIME_CONSUMING_TESTS:
for value in range(0x10000):
resultvalue = minimalmodbus._twoByteStringToNum( minimalmodbus._numToTwoByteString(value) )
self.assertEqual(resultvalue, value)
def testWrongResponseWritedata(self):
self.assertRaises(ValueError, minimalmodbus._checkResponseWriteData, '\x00\x2d\x00\x58', '\x00\x59')
self.assertRaises(ValueError, minimalmodbus._checkResponseWriteData, '\x00\x2d\x00\x58', minimalmodbus._numToTwoByteString(89))
self.assertRaises(ValueError, minimalmodbus._checkResponseWriteData, '\x00\x47\xff\x00', '\xff\x01')
def testCorrectResponseWritedata(self):
minimalmodbus._checkResponseWriteData('\x00\x2d\x00\x58', '\x00\x58')
minimalmodbus._checkResponseWriteData('\x00\x2d\x00\x58', minimalmodbus._numToTwoByteString(88))
minimalmodbus._checkResponseWriteData('\x00\x47\xff\x00', '\xff\x00')
minimalmodbus._checkResponseWriteData('\x00\x47\xff\x00', minimalmodbus._numToTwoByteString(65280))
minimalmodbus._checkResponseWriteData('\x00\x2d\x00\x58ABCDEFGHIJKLMNOP', '\x00\x58')
# We read 10 registers per detector
# FIXME. This may exceed the maximum number of registers that can be read
data = self.read_registers(2999, len(detector_numbers) * 10)
detector_levels = {}
for detector_number in detector_numbers:
# Calculate the register shift for this detector. There are 10 registers
# per detector, in order.
reg_shift = 10 * (detector_number - 1)
# Get the range and read the level
detector_range = self.detector_configuration(detector_number).range
# The level is the 0th register
level_register = data[reg_shift]
bytestring = _numToTwoByteString(level_register)
level = _twoByteStringToNum(bytestring, numberOfDecimals=3, signed=True)\
* detector_range
# Status is the 1st register (0-based)
status = data[1 + reg_shift]
status_out = []
if status == 0:
status_out.append('OK')
else:
# Format the number into bitstring and reverse it with [::-1]
bit_string = '{:016b}'.format(status)[::-1]
for bit_position, status_message in DETECTOR_STATUS.items():
if bit_string[bit_position] == '1':
status_out.append(status_message)
# inhibit is the 4th register (0-based)