modbus.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import time
  2. from serial import Serial
  3. import colorama
  4. from colorama import Fore, Style
  5. from typing import Sequence
  6. DEFAULT_MB_CRC_TABLE = (
  7. 00000, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 1920,
  8. 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 52545, 3840, 53185,
  9. 52865, 3648, 2560, 51905, 52097, 2880, 51457, 2496, 2176, 51265, 55297,
  10. 6_336, 6528, 55617, 6912, 56257, 55937, 6720, 7680, 57025, 57217, 8000,
  11. 56577, 7616, 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760,
  12. 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 61441, 12480,
  13. 12672, 61761, 13056, 62401, 62081, 12864, 13824, 63169, 63361, 14144, 62721,
  14. 13760, 13440, 62529, 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089,
  15. 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 10240, 59585, 59777,
  16. 10560, 60161, 11200, 10880, 59969, 60929, 11968, 12160, 61249, 11520, 60865,
  17. 60545, 11328, 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704,
  18. 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 24960, 41281,
  19. 25344, 41921, 41601, 25152, 26112, 42689, 42881, 26432, 42241, 26048, 25728,
  20. 42049, 27648, 44225, 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328,
  21. 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 47489, 31040, 47873,
  22. 31680, 31360, 47681, 48641, 32448, 32640, 48961, 32000, 48577, 48257, 31808,
  23. 46081, 29888, 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 45953,
  24. 29504, 45313, 29120, 28800, 45121, 20480, 37057, 37249, 20800, 37633, 21440,
  25. 21120, 37441, 38401, 22208, 22400, 38721, 21760, 38337, 38017, 21568, 39937,
  26. 23744, 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 39809, 23360,
  27. 39169, 22976, 22656, 38977, 34817, 18624, 18816, 35137, 19200, 35777, 35457,
  28. 19008, 19968, 36545, 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985,
  29. 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 17280, 33601, 16640,
  30. 33217, 32897, 16448)
  31. class ModbusMixin():
  32. def print_hex(self, text: str, data: bytes):
  33. print(text, *tuple(map(lambda x: '0x{:02X}'.format(x), (i for i in data))))
  34. class Modbus(ModbusMixin):
  35. # MB_CRC_TABLE: Sequence[int] = DEFAULT_MB_CRC_TABLE
  36. MB_TIMEOUT: float = 0.05
  37. MB_CRC_TABLE: [int] = DEFAULT_MB_CRC_TABLE
  38. MB_DEBUG: bool = True
  39. def __init__(self, tty: str, brate: int, address: str):
  40. self.serial = Serial(port=tty, baudrate=brate, timeout=0.05, parity='N', xonxoff=False)
  41. self.address = address
  42. @classmethod
  43. def test(cls):
  44. print(type(cls.MB_CRC_TABLE))
  45. @classmethod
  46. def crc(cls, data: bytes) -> bytes:
  47. crc = 0xFFFF
  48. crc_table = cls.MB_CRC_TABLE
  49. for char in data:
  50. crc = (crc >> 8) ^ crc_table[(crc ^ char) & 0xFF]
  51. return crc.to_bytes(2, 'little')
  52. def raw_communicate(self, data: bytes, predicted_length:int = -1) -> bytes:
  53. if self.MB_DEBUG:
  54. self.print_hex('Request:', data)
  55. self.serial.write(data)
  56. response_bytes = list()
  57. start_time = time.time()
  58. while True:
  59. b = self.serial.read(1)
  60. if len(b):
  61. response_bytes.append(b)
  62. elif time.time() - start_time > self.MB_TIMEOUT:
  63. break
  64. if len(response_bytes) == predicted_length:
  65. break
  66. if self.MB_DEBUG:
  67. self.print_hex('Responce:', response_bytes)
  68. def test_send(self, data: bytes):
  69. while True:
  70. self.serial.write(data)
  71. time.sleep(1)
  72. def send_recv(self, data, number):
  73. self.serial.write(data)
  74. # while True:
  75. # self.serial.write(data)
  76. # recv = self.serial.read_all()
  77. # print(recv)
  78. # print(recv.decode('utf_8'))
  79. # time.sleep(1)
  80. def main():
  81. colorama.init()
  82. st = 'Hello world'
  83. # st.encode('utf-8')
  84. print(st.encode('utf-8'))
  85. # dev = Modbus('COM22', 115200, 1)
  86. # dev.test_send(bytes('hello world\r\n', encoding="utf_8"))
  87. # tx_data = 'hello world\r\n'
  88. # tx_data = 'abc'
  89. # dev.send_recv(bytes(tx_data, encoding="utf_8"), len(tx_data))
  90. # Modbus.test()
  91. # print(i.to_bytes(2, 'big'))
  92. # print(Modbus.crc(i.to_bytes(1, 'big')))
  93. # print(Modbus.crc(b'\x00\x01'))
  94. # crc = Modbus.crc(b'\x00\x01')
  95. # dev.raw_communicate(b'\x00\x01')
  96. # print(ModbusMixin.print_hex(crc))
  97. # print(Modbus.crc(b'\xd0\x00'))
  98. if __name__ == '__main__':
  99. main()