Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么print()会影响com1的读取方式?_Python_Serial Port_Pyserial - Fatal编程技术网

Python 为什么print()会影响com1的读取方式?

Python 为什么print()会影响com1的读取方式?,python,serial-port,pyserial,Python,Serial Port,Pyserial,我正在为一个旧的电子设备编写一个使用RS232串行接口的接口。我遇到了一个似乎无法解决的问题。这是原始代码 def readMyPort(): global readPort while True: #always loop if readPort: # only do something of the port is active dataString = b'' while (mySerialPort.inW

我正在为一个旧的电子设备编写一个使用RS232串行接口的接口。我遇到了一个似乎无法解决的问题。这是原始代码

def readMyPort():
    global readPort
    while True:   #always loop
        if readPort: # only do something of the port is active
            dataString = b''
            while (mySerialPort.inWaiting()>0):
                data = mySerialPort.read(1)
                if (data == b'') or (data == b'\r') or (data == b'\n'):
                    if (dataString != b''):
                        myOutput.insert('1.0', (dataString + b'\n').decode())
                        dataString = b''
                else:
                    dataString += data
我面临的问题是,该工具发送了一个由12个字符组成的字符串来响应命令,而我似乎只捕获了最后4个字符,而且字符串中没有“”、“\r”或“\n”。为了排除故障,我添加了一个print(),如下所示。神奇的是,我开始获取所有的数据

def readMyPort():
    global readPort
    while True:   #always loop
        if readPort: # only do something of the port is active
            dataString = b''
            while (mySerialPort.inWaiting()>0):
                data = mySerialPort.read(1)
                print(data)   #<-------- the added line
                if (data == b'') or (data == b'\r') or (data == b'\n'):
                    if (dataString != b''):
                        myOutput.insert('1.0', (dataString + b'\n').decode())
                        dataString = b''
                else:
                    dataString += data
def readMyPort():
全局读取端口
虽然为True:#始终循环
如果readPort:#仅在端口处于活动状态时执行某些操作
数据字符串=b''
while(mySerialPort.inWaiting()>0):
data=mySerialPort.read(1)
打印(数据)#操作
打印(数据)
需要一些时间(相对较大)才能将内容打印到控制台。因此,通过添加行
print(data)
,您只需在循环中添加一些延迟。你可以用
print(data)
代替
time.sleep(0.1或任何较小的值)
来验证这个理论,并检查问题是否有望解决


我认为延迟是有帮助的,因为没有它,
mySerialPort.inWaiting()
有时会在事务实际完成之前变为
0
(接收缓冲区为空)。你的仪器不能简单地输出那么快的数据。在代码的最终版本中,您可以添加
time.sleep()
而不是
print(data)

根据Konstantin的回答,串行端口似乎需要一些时间来调整缓冲区和计数器,因此在循环中添加一个小的延迟解决了我的问题。1毫秒是不够的,但10毫秒是不够的。最后的代码是:

def readMyPort():
    global readPort
    while True:   #always loop
        if readPort: # only do something if the port is active
            dataString = b''
            while (mySerialPort.inWaiting()>0):  #if something is in the buffer
                data = mySerialPort.read(1)
                if (data == b'') or (data == b'\r') or (data == b'\n'):
                    if (dataString != b''):  #don't output empty strings
                        myOutput.insert('1.0', (dataString + b'\n').decode())
                        dataString = b''
                else:
                    dataString += data
                    time.sleep(0.01)   #slow the loop down a bit 

我仍然想知道是否有更优雅的解决方案。

谢谢你的建议。我以前确实尝试过增加延迟,但也许我没有给它足够的时间。