简体   繁体   English

将数据从arduino导入到python

[英]Importing data from arduino to python

I am trying to import data from my arduino to python so that I can then use it in my program but I cannot find how to send the data without importing b'\\r\\n' as well. 我试图将数据从arduino导入到python,以便可以在程序中使用它,但是我也找不到如何在不导入b'\\ r \\ n'的情况下发送数据。 This is an example of what I am currently importing: 这是我当前正在导入的示例:

b'197,2302,50,198,\\r\\n' b'197,2302,50,198,\\ r \\ n'

# The numbers in the middle are data from four distance sensors #中间的数字是来自四个距离传感器的数据

I have asked this question a few days ago. 我几天前问过这个问题。 Maybe this thread helps you: pyserial communication with arduino (for motor-control) 也许该线程对您有帮助: 与arduino进行pyserial通信(用于电机控制)

Just add this lines to read from serial and delete \\r\\n from your code: 只需添加以下行即可从序列中读取并从代码中删除\\ r \\ n:

get = ser.readline()[:-2]

You didn't show how to send the data from arduino side, so I will answer dealing with the data you got; 您没有显示如何从arduino端发送数据,所以我将回答如何处理您得到的数据; strip trailing space using bytes.rstrip : 使用bytes.rstrip删除尾随空间:

>>> b'197,2302,50,198,\r\n'.rstrip()
b'197,2302,50,198,'

Your Arduino is probably sending the data with b'\\r\\n' in it. 您的Arduino可能正在发送带有b'\\ r \\ n'的数据。

You can get the numbers out with this regular expression like this: 您可以使用以下正则表达式得出数字:

>>> import re
>>> re.findall('[0-9]+', "b'197,2302,50,198,\r\n'")
['197', '2302', '50', '198']

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

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