简体   繁体   English

Python:serial.readline()-如何定义从\\ n到\\ n \\ n的EOL

[英]Python: serial.readline() - how to define the EOL from \n to \n\n

I'm realy sorry for asking another question here in a day. 非常抱歉一天之后在这里提出另一个问题。

The new problem in detail: I connected an Laser Range Finder from HOKUYO onto my RaspBerryPi. 详细的新问题是:我将HOKUYO的激光测距仪连接到我的RaspBerryPi上。

Connection etc works find, thanks to the serial.py 感谢serial.py,可以找到连接等

My only Problem ist, wenn I'm sending a command, I get an echo and a timestamp + \\n back. 我唯一的问题专家,我正在发送命令,收到回显和时间戳+ \\ n返回。

The data in the buffer looks like this: 缓冲区中的数据如下所示:

MD000007200001\n
2Dh1\n
\n\n

After this, the sensor transmits the measurement, which locks like 之后,传感器发送测量值,如

MD000007200001\n
2Dh1\n
0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C\n
0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C\n
0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C\n
0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C\n
.....
...
0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C\n
\n\n

to read these data, at the moment I use readall(). 读取这些数据,此刻我使用readall()。 Also tried readlines(). 还尝试了readlines()。 In both cases I got the problem, that have to wait until the timeout, which was set to 1. This takes too much time for a realtime application and the fact, that this sensor can measure every 120ms. 在这两种情况下,我都遇到了问题,那就是必须等待超时,该超时被设置为1。这对于实时应用来说花费了太多时间,而且这个传感器每120ms可以测量一次。 If I set the timeout to 0 I often miss some data and everything collapses, because I need the whole dataset for my caluclation. 如果将超时设置为0,我经常会丢失一些数据,并且一切都会崩溃,因为我需要整个数据集进行计算。

I also read, that there was an option to set the EOL of readline like readline(eof='\\n\\n') but with Python 3.x this won't work. 我还读到,有一个选项可以将readline的EOL设置为readline(eof ='\\ n \\ n'),但对于Python 3.x则不起作用。

There seems to be a 2nd Option, writing my own readline-function. 似乎有第二种选择,编写我自己的readline函数。

But I'm an absolute beginner in python. 但是我绝对是python的初学者。 so I don't know where I should start. 所以我不知道我应该从哪里开始。

Propably there are some additional options. 可能还有其他一些选择。

Best regards, A. 最好的问候,A.

Adapting the answer at pySerial 2.6: specify end-of-line in readline() (which also offers alternatives), one could write a function such as: 调整pySerial 2.6的答案:在readline()中指定行尾 (它也提供了替代方法),可以编写如下函数:

def readline(a_serial, eol=b'\n\n'):
    leneol = len(eol)
    line = bytearray()
    while True:
        c = a_serial.read(1)
        if c:
            line += c
            if line[-leneol:] == eol:
                break
        else:
            break
    return bytes(line)

a_serial must be a serial.Serial instance built with the proper parameters, of course -- eg, the default timeout of None could cause this to block indefinitely if the required eol marker is not forthcoming. a_serial必须是serial.Serial实例,并使用适当的参数构建-例如,如果未提供所需的eol标记,则默认timeoutNone可能导致该实例无限期地阻塞。 This does not appear to be a problem for the OP if I read the question correctly, but, it is something to be aware of in general cases. 如果我正确阅读了问题,这对于OP来说似乎不是问题,但是在一般情况下这要注意的。

You should set the timeout to 0.12 (or whatever you'd like to make it "realtime") and use readall() . 您应该将超时设置为0.12 (或将其设置为“实时”的任何值),并使用readall() Then, you have a number of choices: 然后,您有多种选择:

  1. If you want both \\n and \\n\\n to count as a delimiter, call replace("\\n\\n", "\\n") on the data from readall() and divide it up into lines yourself by calling split("\\n") . 如果您希望\\n\\n\\n都作为分隔符,请对readall()的数据调用replace("\\n\\n", "\\n") readall()并自己通过调用split("\\n") readall()将其分成几行split("\\n")
  2. If you want only \\n\\n to count as a delimiter, just call split("\\n\\n") on the data from readall() . 如果只希望\\n\\n用作分隔符,则只需对readall()的数据调用split("\\n\\n") readall()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM