|
@@ -0,0 +1,35 @@
|
|
|
+import time
|
|
|
+from serial import Serial
|
|
|
+import colorama
|
|
|
+from colorama import Fore, Style
|
|
|
+
|
|
|
+class Modbus:
|
|
|
+ def __init__(self, tty: str, brate: int, address: str):
|
|
|
+ self.serial = Serial(port=tty, baudrate=brate, timeout=0.05, parity='N', xonxoff=False)
|
|
|
+ self.address = address
|
|
|
+
|
|
|
+ def test_send(self, data: bytes):
|
|
|
+ while True:
|
|
|
+ self.serial.write(data)
|
|
|
+ time.sleep(1)
|
|
|
+
|
|
|
+ def send_recv(self, data, number):
|
|
|
+ self.serial.write(data)
|
|
|
+
|
|
|
+ # while True:
|
|
|
+ # self.serial.write(data)
|
|
|
+ # recv = self.serial.read_all()
|
|
|
+ # print(recv)
|
|
|
+ # print(recv.decode('utf_8'))
|
|
|
+ # time.sleep(1)
|
|
|
+
|
|
|
+def main():
|
|
|
+ colorama.init()
|
|
|
+ dev = Modbus('COM22', 115200, 1)
|
|
|
+ # dev.test_send(bytes('hello world\r\n', encoding="utf_8"))
|
|
|
+ # tx_data = 'hello world\r\n'
|
|
|
+ tx_data = 'abc'
|
|
|
+ dev.send_recv(bytes(tx_data, encoding="utf_8"), len(tx_data))
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ main()
|