码迷,mamicode.com
首页 > 编程语言 > 详细

Pyserial 实现串口 base on python3

时间:2019-05-18 09:17:02      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:create   else   div   tool   通信   list   python   erro   nbsp   

应用Pyserial 包实现串口通信类:

其主要接口如下:

1. Serial_Create  创建和使能串口

2. Serial_WriteAndReadBin 写读二进制数据流

3. Serial_WriteAndReadString 写读字符串

4. Serial_WriteString 写字符串

5. Serial_WriteBin 写二进制数据

6. Serial_Read 读字符串数据

 

#coding:utf-8
#!/usr/bin/python3
import serial
import serial.tools.list_ports
import threading
import time

__all__ = ["CSerial"]

class CSerial:
    def __init__(self, Port, Baud, TimeOut):
        self.l_serial = None
        self.alive = False
        self.waitEnd = None
        self.port = Port
        self.baud = Baud
        self.timeOut = TimeOut
        self.readString = ""
        self.readBin = None
        self.receiveReady = False
        self.thread_SerialRead = None

    def Serial_Create(self):
        try:
            self.l_serial = serial.Serial(self.port, self.baud, timeout=self.timeOut)
        except Exception as e:
            print("serial port error:", e)

    def Serial_WriteAndReadBin(self,bin):
        WaitingCnt = 0
        self.alive = True
        self.l_serial.write(bin)
        while self.alive:
            time.sleep(0.1)   #1 second
            if self.l_serial.in_waiting:
                self.readBin = self.l_serial.read(self.l_serial.in_waiting)
                self.alive = False
                return self.readBin
            else:
                 WaitingCnt += 1
                 if WaitingCnt > 3:
                     return None

        return None

    def Serial_WriteAndReadString(self,string):
        WaitingCnt = 0
        self.readString = ""
        self.alive = True
        self.l_serial.write(string.encode("gbk"))
        while self.alive:
            time.sleep(0.1)  #1 second
            if self.l_serial.in_waiting:
                self.readString = self.l_serial.read(self.l_serial.in_waiting).decode("gbk")
                self.alive = False
                return self.readString
            else:
                 WaitingCnt += 1
                 if WaitingCnt > 3:
                     return None

        return None

    def Serial_ReadData(self):
        while self.alive:
            if self.l_serial.in_waiting and self.receiveReady == False:
                self.readData = self.l_serial.read(self.l_serial.in_waiting)
                self.receiveReady = True

    def Serial_WriteString(self,string):
         length = self.l_serial.write(string.encode("gbk"))
         return length

    def Serial_WriteBin(self,bin):
         length = self.l_serial.write(bin)
         return length

    def Serial_Read(self):
        if self.l_serial.in_waiting:
            STRGLO = self.l_serial.read(self.l_serial.in_waiting).decode("gbk")
            print(STRGLO)

    def Serial_Close(self):
        self.alive = False
        if self.l_serial.isOpen():
            self.l_serial.close()

#test example
if __name__ == ‘__main__‘:
    test = CSerial("COM41",9600,100)
    test.Serial_Create()
    print(test.Serial_WriteAndReadString("hello, I am a serial port!\r\n"))

  

Pyserial 实现串口 base on python3

标签:create   else   div   tool   通信   list   python   erro   nbsp   

原文地址:https://www.cnblogs.com/mftang2018/p/10884543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!