使用Python readline()通过USB从Arduino Uno获取多个传感器数据
我有一个树莓pi 3B+通过USB电缆连接到Arduino Uno上,我正在接收多个传感器数据。我正试图找出用python将它们分开的最佳方法。比如,如果我得到如下数据: 27.3 0 一, 它需要知道第一行是温度,第二行是运动检测,第三行是噪声检测等等 想法1) 让所有传感器以相同的顺序同时更新。在循环中对其进行编码,使第一行readline()保存为温度变量,第二行保存为运动变量,依此类推,直到它循环回来 想法2) 在每个传感器数据前面放置某种标志,让python读取每一行并决定如何处理它。Like temperature将像t27.3一样输出,python中的代码将看到前面的t,并知道如何获取其余数据并将其更新到服务器。不过,我不确定这段代码会是什么样子使用Python readline()通过USB从Arduino Uno获取多个传感器数据,python,arduino,raspberry-pi,Python,Arduino,Raspberry Pi,我有一个树莓pi 3B+通过USB电缆连接到Arduino Uno上,我正在接收多个传感器数据。我正试图找出用python将它们分开的最佳方法。比如,如果我得到如下数据: 27.3 0 一, 它需要知道第一行是温度,第二行是运动检测,第三行是噪声检测等等 想法1) 让所有传感器以相同的顺序同时更新。在循环中对其进行编码,使第一行readline()保存为温度变量,第二行保存为运动变量,依此类推,直到它循环回来 想法2) 在每个传感器数据前面放置某种标志,让python读取每一行并决定如何处理它。
哪种方法更好(最好更快)?如果是第二个,你能告诉我一些解析字符串的代码吗?我已经有一段时间没有这样做了,我已经完全忘记了。谢谢 我的建议是使用一种数据格式,它是未来的证明,所以让我们使用一个简单的JSON。
它的标准化,phyton内置了支持,在Arfuino上,我们可以手动编写代码,而无需大量的libs
因此,这里有两个代码片段可以随时满足您的需要。请随意调整,并阅读代码中的注释,以了解有关所用函数的更多信息
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import json
ser = serial.Serial('/dev/ttyACM0',9600)
buffer = ""
while True:
try:
buffer = ser.readline()
print buffer
data = json.loads(buffer)
print data["temperature"]
temperature = data["temperature"]
print temperature
print data["motion "]
motion = data["motion "]
print motion
print data["noise"]
noise = data["noise"]
print noise
buffer = ""
print " "
except json.JSONDecodeError:
print "Error : try to parse an incomplete message"
在Arduino端,我们使用一个全局字符数组来构建JSON
char jsonData[256] = {'\0'}; /* Defined globally choose size large enough */
char numBuffer[16] = {'\0'}; /* Defined globally used for convering numbers to string */
// here we build the JSON
strcpy (jsonData, "{\"temperature\":\"" );
// If its an int use this command
// itoa(temperature, numBuffer, 10); // convert int DATA from sensors etc.
// For float we use this command
// dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
dtostrf(temperature, 3, 2, numBuffer); // -> xx.xx
strcat(jsonData, numBuffer);
strcat (jsonData, "\",\"motion\":\"" );
itoa(motion, numBuffer, 10); // convert int DATA from sensors etc.
strcat(jsonData, numBuffer);
strcat (jsonData, "\",\"noise\":\"" );
itoa(noise, numBuffer, 10); // convert int DATA from sensors etc.
strcat(jsonData, numBuffer);
strcat (jsonData, "\"}");
// The JSON data looks like: {"temperature":"21.50","motion":"113","noise":"321"}
Serial.println(jsonData);
别忘了在两台机器(在软件中)上设置相同的串行速度,并从一侧启动,以确保JSON的完整传输。我的建议是使用一种数据格式,它是未来的证明,因此让我们使用一种简单的JSON。
它的标准化,phyton内置了支持,在Arfuino上,我们可以手动编写代码,而无需大量的libs
因此,这里有两个代码片段可以随时满足您的需要。请随意调整,并阅读代码中的注释,以了解有关所用函数的更多信息
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import json
ser = serial.Serial('/dev/ttyACM0',9600)
buffer = ""
while True:
try:
buffer = ser.readline()
print buffer
data = json.loads(buffer)
print data["temperature"]
temperature = data["temperature"]
print temperature
print data["motion "]
motion = data["motion "]
print motion
print data["noise"]
noise = data["noise"]
print noise
buffer = ""
print " "
except json.JSONDecodeError:
print "Error : try to parse an incomplete message"
在Arduino端,我们使用一个全局字符数组来构建JSON
char jsonData[256] = {'\0'}; /* Defined globally choose size large enough */
char numBuffer[16] = {'\0'}; /* Defined globally used for convering numbers to string */
// here we build the JSON
strcpy (jsonData, "{\"temperature\":\"" );
// If its an int use this command
// itoa(temperature, numBuffer, 10); // convert int DATA from sensors etc.
// For float we use this command
// dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
dtostrf(temperature, 3, 2, numBuffer); // -> xx.xx
strcat(jsonData, numBuffer);
strcat (jsonData, "\",\"motion\":\"" );
itoa(motion, numBuffer, 10); // convert int DATA from sensors etc.
strcat(jsonData, numBuffer);
strcat (jsonData, "\",\"noise\":\"" );
itoa(noise, numBuffer, 10); // convert int DATA from sensors etc.
strcat(jsonData, numBuffer);
strcat (jsonData, "\"}");
// The JSON data looks like: {"temperature":"21.50","motion":"113","noise":"321"}
Serial.println(jsonData);
不要忘记在两台机器(在SW中)上设置相同的串行速度,并从一侧启动,以确保JSON的完全传输,两种方式都是可能的,速度不应存在任何问题。其余的可以在网上找到,这两种方法都是可能的,速度不应该是任何问题。其余的可以在网上轻松找到