python - 使用python从RS 232端口读取

标签 python serial-port

<分区>

我想用 python 编写一个程序来从 RS 232 端口读取数据。 我的笔记本电脑没有这个端口。 任何人都可以推荐任何好的模拟器和一个从 RS 232 端口读取的示例 python 程序。

最佳答案

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

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)

关于python - 使用python从RS 232端口读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23534506/

相关文章:

python - scipy中的距离变换算法

python - 何时刷新 Pyserial 缓冲区?

python - ./python : error while loading shared libraries: libssl. so.1.1: 无法打开共享对象文件: 没有这样的文件或目录

python - django-rest-framework : restrict RelatedField queryset according to request

python - Flask 运行 request.method 默认为 'POST' 而不是 'GET'

c - 在读取期间完全清除调制解调器模块串行缓冲区

c# - SerialPort.GetPortNames 多次返回同一个端口

c# - 如何解锁COM口

java - 如何将 Eclipse Indigo 与 Java 通信 API 结合使用

python - 添加重复项时从集合中获取原始元素