Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 Arduino Uno Raspberry Pi串行通信双读数_Python_Arduino_Raspberry Pi_Arduino Uno_Pyserial - Fatal编程技术网

Python Arduino Uno Raspberry Pi串行通信双读数

Python Arduino Uno Raspberry Pi串行通信双读数,python,arduino,raspberry-pi,arduino-uno,pyserial,Python,Arduino,Raspberry Pi,Arduino Uno,Pyserial,我使用Arduino Uno将光传感器的模拟数据转换为数字,并通过USB电缆将数据发送给raspberry pi。然而,当我运行代码时,我从一个10位传感器中读取类似1923的值 这是arduino代码 int a = A0; int meas = 0; void setup() { Serial.begin(9600); } void loop() { meas = analogRead(a); if(Serial.readString() == "1"){ //Check that Rasp

我使用Arduino Uno将光传感器的模拟数据转换为数字,并通过USB电缆将数据发送给raspberry pi。然而,当我运行代码时,我从一个10位传感器中读取类似1923的值

这是arduino代码

int a = A0;
int meas = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
meas = analogRead(a);
if(Serial.readString() == "1"){ //Check that Raspberry wants data or not
Serial.println(meas);     
}
}
下面是Raspberry Pi中的Python代码

import serial
from datetime import datetime
now = datetime.now()

ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write("1".encode())
s = ser.readline()

file = open("dataset.txt", "a")
file.write(now.strftime("%Y-%m-%d %H:%M") + " Sensor Value:" + str(s)+ "\n")
file.close()
下面是每5分钟运行一次代码后的示例输出

14:08 Sensor Value:6
14:10 Sensor Value:8
14:15 Sensor Value:8
14:20 Sensor Value:10
14:25 Sensor Value:6
14:30 Sensor Value:9
14:35 Sensor Value:6
14:40 Sensor Value:7
14:45 Sensor Value:5
14:50 Sensor Value:5
14:55 Sensor Value:12
15:00 Sensor Value:1
15:05 Sensor Value:1
15:10 Sensor Value:10
15:15 Sensor Value:12
15:20 Sensor Value:14
15:25 Sensor Value:1922
15:30 Sensor Value:2211
15:35 Sensor Value:11
15:39 Sensor Value:6
15:40 Sensor Value:7
15:45 Sensor Value:8
15:50 Sensor Value:10
15:55 Sensor Value:1
16:00 Sensor Value:
16:05 Sensor Value:11
我想去掉这些1和1922之类的东西,它们当然是毫无意义的数据

PS:传感器在山顶上,我正在使用远程连接检查数据和操作代码


我该怎么做?谢谢您的时间。

您可以看看校准,下面是一段示例代码


我认为马克·塞切尔是对的。您正在从过去的测量中获取数据

就个人而言,我会实现一个更健壮的协议,但由于您的应用程序非常基本,您可以尝试一种更简单的方法,这就是他所建议的

通过在python程序中请求和读取之间添加一个小延迟,可以很容易地解决这个问题。像这样的东西就足够了:

from time import sleep
...
ser.write("1".encode())
sleep(0.05);
s = ser.readline()
同时,我不喜欢你在arduino处理阅读的方式。如果您总是发送单字符命令,我建议使用以下方法:

void loop() {
    meas = analogRead(a);
    if (Serial.available())
    {
        if (Serial.read() == '1')
        {
            Serial.println(meas);
        }
    }
}

这不会阻止循环的执行(如果您计划扩展功能,这会很方便)

但传感器位于山顶的盒子中。代码用于校准传感器发出的值。您需要一种方法来限制值的范围。我认为这是由异步通信引起的,您知道如何解决这个问题吗?再多的错误校准也不会使
analogRead
返回的值超过1023。校准无法解决问题。正如您在这里提到的错误校准,这意味着误解,校准不在设备上,而是确定并指定代码中的有效值范围@EkinEkinEkin,你试过在arduino.cc下的传感器论坛上发帖吗。他们还会要求提供图表和详细的传感器信息。在读取Arduino之前,请尝试测试
serial.available()
,在Pi上读取
readline()
之前等待200毫秒。我尝试了你在Python代码中所说的内容,但现在我从Arduino那里什么也得不到。覆盆子Pi@EkinEkinEkin您是更改了arduino代码还是只更改了python?您正在运行哪个版本的python?我更改了这两个版本,并且正在使用python2@EkinEkinEkin你能分享一下你所说的“有时工作,工作不正常”的意思吗?你收到了什么?你没有收到什么?您正在经历的行为是什么?为什么不是您期望的行为?@EkinEkinEkin好的,打开串行端口时尝试使用超时选项(
ser=serial.serial('/dev/ttyACM0',9600,timeout=1)
)。如果仍然无法工作,请尝试使用串行监视器检查与arduino的通信是否正常,以及您是否收到了预期的结果