Serial 串口通信

Windows串口名称:

import serial
import serial.tools.list_ports

port_list = list(serial.tools.list_ports.comports())
if len(port_list) <= 0:
    print("The Serial port can't find!")
else:
    print(len(port_list))
    for i in list(port_list):
        print(i[1])
        print(i)

Ubuntu串口名称:

dmesg | grep ttyS*  # 如需加权限chomd 666 

 继电器:

    def usb_off(self):
        serial_power = serial.Serial("COM8", "9600")
        if not serial_power:
            print('try again')
            serial_power = serial.Serial("COM8", "9600")
        if not serial_power:
            print('connect serial %s failed' % "9600")
        # 断1
        d = bytes.fromhex("01 05 00 00 FF 00 8C 3A")
        serial_power.write(d)
        serial_power.close()
        time.sleep(2)

    def usb_on(self):
        serial_power = serial.Serial("COM8", "9600")
        if not serial_power:
            print('try again')
            serial_power = serial.Serial("COM8", "9600")
        if not serial_power:
            print('connect serial %s failed' % "9600")
        # 连接
        d = bytes.fromhex("01 05 00 00 00 00 CD CA")
        serial_power.write(d)
        serial_power.close()
        time.sleep(2)

参考资料:https://blog.csdn.net/xuzhexing/article/details/91043579/

posted @ 2021-03-26 10:35  是超级可爱的小姚童鞋  阅读(224)  评论(0编辑  收藏  举报