美文网首页
获取串口内容的实践

获取串口内容的实践

作者: 小蜗牛的成长 | 来源:发表于2018-02-06 15:13 被阅读0次

准备

安装pyserial模块,即pip install pyserial

pyserial使用

导入模块 import serial
初始化 serial.Serial()实例化一个串口对象

ser = serial.Serial(  
port=None,               # 串口名称
baudrate=9600,           # 波特率
bytesize=EIGHTBITS,      # 数据位  
parity=PARITY_NONE,      # 校验位
stopbits=STOPBITS_ONE,   # 停止位
timeout=None,            # 设置超时值, None永远等待、0不阻塞,立刻返回、某个值是会阻塞
xonxoff=0,               # enable software flow control  
rtscts=0,                # enable RTS/CTS flow control  
interCharTimeout=None    # Inter-character timeout, None to disable  
) 

以上因串口不同设置值会有差异,例如我使用的串口板参数:baudrate=512000,bytesize=8,stopbits=1,特别注意:波特率值不要设置错误,否则一直乱码
串口名称可通过导入serial.tools.list_ports模块,使用serial.tools.list_ports.comports()获得所有串口
例如我设计的类部分代码,获取当前usb串口:

@property
        def port(self):
                return self.__port

        @port.setter
        def port(self,port):
                if port:
                        self.__port=port
                else:
                        for i in serial.tools.list_ports.comports():
                                if "USB-to-Serial" in i.description:
                                        self.port=i.device
常见实例方法

改变已打开端口的参数,如

 self.ser .baudrate =self.baudrate
 self.ser .timeout = self.timeout
 self.ser .bytesize=self.bytesize

获取实例对象的内容及状态等

open()                  # 打开端口
close()                 # 立即关闭端口  
isOpen()                #获取串口的状态
inWaiting()             # 返回缓冲器字节的长度
write(s)                # 把字符串s写到该端口  
read(n)                 # 读取指定字节长度的字符串
flushInput()            # 清除输入缓存区,放弃所有内容
flushOutput()           # 清除输出缓冲区,放弃输出  
sendBreak()             # 发送中断条件  
setRTS(level=1)         # set RTS line to specified logic level  
setDTR(level=1)         # set DTR line to specified logic level  
getCTS()                # return the state of the CTS line  
getDSR()                # return the state of the DSR line  
getRI()                 # return the state of the RI line  
getCD()                 # return the state of the CD line

串口实例方法可参考:http://blog.csdn.net/dainiao01/article/details/5885122

相关文章

  • 获取串口内容的实践

    准备 安装pyserial模块,即pip install pyserial pyserial使用 导入模块 imp...

  • 串口编程

    串口连接界面 主要控件 打开串口 自动获取串口列表并显示 初始化串口 写入卡片 插入打卡记录并显示打卡成功

  • 串口

    串口使用和CSerial类 windows下打开串口COM10的坑 从注册表获取所有的串口[Windows] wi...

  • 监控本地电脑串口数据(CommMonitor)

    获取本地电脑串口数据 1、工具:CommMonitor(串口监视精灵)下载地址:http://www.pc0359...

  • 考勤系统功能实现——打卡

    1.打卡界面 2.相关代码 自动获取串口列表 向卡中写入数据 打开或关闭串口 窗口关闭时,要关闭串口 “读取卡片”...

  • QT-串口

    //获取串口信息列表 QList serialPortInfo=QSerialPortInfo::avail...

  • 2018-08-31 uart通信

    usart通信 第一步 串口通行设置 中断服务函数 最终实现功能: 是在串口助手上获取数据打印数据。

  • nodejs 获取串口数据

    由于项目要求,项目为(B/S)架构 ,需要在页面上读取串口数据,于是就需要nodejs了 使用 nodejs 的一...

  • 2018-07-09

    考勤系统打卡功能实现 1、制作打卡后台服务界面 2、功能实现代码 (1)自动获取串口 (2)设置串口参数2.PNG...

  • 9月21日

    今天讲的全新内容 串口通信 什么是串口和并口 主要讲解是串口通信寄存器的配置 并且初步实现了通信功能 下午复习上午...

网友评论

      本文标题:获取串口内容的实践

      本文链接:https://www.haomeiwen.com/subject/yakjzxtx.html