zoukankan      html  css  js  c++  java
  • linux python 串口

    #!/usr/bin/env python

    # encoding: utf-8
    '''
    # @Time : 21-2-20
    # @Author : geoffery
    # @Site :
    # @File : serialCom.py
    # @Software: PyCharm

    '''

    import serial

    class usbSerial(object):
    def __init__(self, port, bps, time):
    try:
    self.portx = port
    self.bps = bps
    # 超时设置,None:永远等待操作,0为立即返回请求结果,其他值为等待超时时间(单位为秒)
    self.timex = time
    self.ser = serial.Serial(self.portx, self.bps, timeout=self.timex)
    print("串口详情参数:", self.ser)
    except Exception as e:
    print("---异常---:", e)

    def send(self, data):
    try:
    self.d = bytes.fromhex(data)
    self.result = self.ser.write(self.d)
    print("写总字节数:", self.result)
    except Exception as e:
    print("---异常---:", e)

    def read(self, num):
    try:
    return self.ser.read(num).hex()
    except Exception as e:
    print("---异常---:", e)

    def close(self):
    try:
    self.ser.close()
    except Exception as e:
    print("---异常---:", e)


    if __name__ == "__main__":
    uS = usbSerial("/dev/ttyUSB0", 9600, 1)
    uS.send('01 05 00 00 00 00 00 00')
    a = uS.read(8)
    print(a)
    uS.close()
  • 相关阅读:
    设计模式七大原则之单一职责原则
    机器学习入门与进阶
    Django之路
    Python编程之路
    Python练习1
    Docker入门与进阶
    运维相关
    Node.js(一)
    位运算
    双指针算法
  • 原文地址:https://www.cnblogs.com/hddkman/p/14481902.html
Copyright © 2011-2022 走看看