首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python从RS232端口读取

使用python从RS232端口读取
EN

Stack Overflow用户
提问于 2014-05-08 14:39:14
回答 1查看 9.5K关注 0票数 4

我想写一个程序在python读取RS232端口。我的笔记本电脑没有这个端口。有没有人能推荐一个好的模拟器和一个从RS232端口读取数据的python示例程序?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-12 14:13:40

我找到解决方案了。我使用了一个虚拟串行端口模拟器来创建虚拟的2个端口,并将这些端口配对。然后,我使用pyserial python库对端口进行读写。用于从端口读取的示例代码

代码语言:javascript
复制
import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='COM2',
    baudrate=9600,
    timeout=1,
    parity=serial.PARITY_ODD,
    stopbits=serial.STOPBITS_TWO,
    bytesize=serial.SEVENBITS
)
ser.isOpen()
# Reading the data from the serial port. This will be running in an infinite loop.

while 1 :
        # get keyboard input
        bytesToRead = ser.inWaiting()
        data = ser.read(bytesToRead)
        time.sleep(1)
        print(data)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23534506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档