2

我正在尝试使用pySerial通过 USB-RS232 转换器与设备通信。

我的第一个测试是放弃通信,只是“发明”数据点,以测试通信类与程序其余部分的集成。

def run(self):
    import random
    while True:
        self.callback(random.ranint(MIN, MAX))

工作得很好。现在我想测试“短路”通信。也就是说,短引脚 2 和 3(没有流量控制)并接收我正在传输的内容。

这适用于minicom,但不适用于我的代码:

def run(self):
    while True:
        self.ser.write('a')
        print self.ser.read(size=1)

读取和写入超时设置为 0。

timeout = None:永远等待
timeout = 0:非阻塞模式(读取时立即返回)
timeout = x:将超时设置为 x 秒(允许浮动)

在此处输入图像描述

我的程序在调用后挂起write()。我错过了什么?

4

1 回答 1

0

以下是该库测试中的一些代码:

def test2_Loopback(self):
"""timeout: each sent character should return (binary test).
this is also a test for the binary capability of a port."""
    for block in segments(bytes_0to255):
        length = len(block)
        self.s.write(block)
        # there might be a small delay until the character is ready (especially on win32)
        time.sleep(0.05)
        self.failUnlessEqual(self.s.inWaiting(), length, "expected exactly %d character for inWainting()" % length)
        self.failUnlessEqual(self.s.read(length), block)#, "expected a %r which was written before" % block)
    self.failUnlessEqual(self.s.read(1), data(''), "expected empty buffer after all sent chars are read")

这里。看来我缺少的是中间的评论。

编辑:锯末下面的评论以更简洁的方式解决了这个问题。改用那个。


现在是真正的解决方案。

事实证明,我的问题非常愚蠢。我在做:

  1. 打开端口没有超时或读写
  2. Runt 的无限循环write(),然后read()
  3. 用小刀或回形针将 RX 和 TX 短路
  4. 等等,挠我的头,为什么一切都冻结了

发生了什么:写入成功,但数据丢失(因为 RX 和 TX 尚未加入)。然后 read() 冻结了,等待听到什么。

首先将引脚短路,然后运行测试来解决它。

于 2013-09-11T11:29:44.483 回答