【问题标题】:Serial communication from raspi to multiple arduino's从 raspi 到多个 arduino 的串行通信
【发布时间】:2022-10-08 01:13:34
【问题描述】:

我有一个可能是愚蠢的问题,但我似乎无法找到一个好的解决方案。

我想要什么: 2 arduino 收集模拟和数字信号,他们将其保存为一个字符串(每个单独),当被询问时,他们通过串行将数据发送到 Raspberry Pi。树莓派发送一个信号(和时钟数据),以便能够匹配所有内容(下一阶段)。

问题: 如何编写下降脚本:1收集所有数据并将其保存(作为字符串还是应该转到数组?)在arduino中,其次如何从Raspi收集数据。

我得到了什么: 阿杜诺:

char dataStr[50];
void setup() {
    Serial.begin(9600);
    while (!Serial) {   
        ; 
    } 
void loop(){
    //read sensors
    //attach value to datastring with strcat(dataStr, Sensorvalue);
    dataStr= "1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;" //this part already works code A0 is arduino 1 and code A01 is arduino 2
    if (Serial.available() > 0) {
        String data = Serial.readStringUntil('\n');
        Serial.print( data); //clock signal coming from raspi
        Serial.println(dataStr); 
        dataStr[0] = 0;
    }

树莓派:

#!/usr/bin/env python3
import serial
import time
from datetime import datetime
now = datetime.now()

if __name__ == '__main__':
ser00 = serial.Serial('/dev/ttyACM0', 9600, timeout=10)
    ser00.reset_input_buffer()
    
    ser01 = serial.Serial('/dev/ttyACM1', 9600, timeout=10)
    ser01.reset_input_buffer()

    while True:
        
        line = ser00.readline().decode('utf-8').rstrip()
        if (line != ""):           
          d1 = now.strftime("%d/%m/%Y %H:%M:%S ")
          ser00.write(d1.encode('utf-8'))
          print(line)
            
        line = ser01.readline().decode('utf-8').rstrip()
        if (line != ""):
          d1 = now.strftime("%d/%m/%Y %H:%M:%S ")
          ser00.write(d1.encode('utf-8'))
          print(line)

如您所见,我现在设法进行通信,向 arduino 发送时间并接收时间 + 数据字符串。但是这两个 arduino 不会以相同的速度工作,或者不会以相同的速度收集数据(例如,每分钟测量一次封闭环境的温度,但要尽可能快地测量加速度以获得最大值)。一些背景信息,我正在尝试监视我的拖车和其中的动物。并获得有关驱动器状况的精美打印输出。

现在我得到这个回应:

> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A0-TRPI-Empty, S;
> 22/02/2022 10:46:14 1, 2, 3, 4, 5, 6, 7, 8, A01-TRPI-Empty, S;

我肯定会犯 100 个新手错误,我是 python 新手。但是,如果您设法跳过那些简单的新手代码,您能否帮助我理解我应该如何创建所需的通信。我应该研究的几个方向以及我错过的任何明显错误都非常受欢迎!

提前致谢!!

一些额外的信息;我最终将需要超过 2 个 arduino,因为我将添加多个摄像头。我正在使用串行,因为无论如何我都在使用电线(我不会管理多个电池)所以我认为这是最干净的解决方案(加上长度最大为 +-5m,所以我无法在没有额外硬件的情况下使用 I2C)。 Arduino 通过 USB 连接到覆盆子,目前使用的是 Arduino Uno,但完成后会切换到更小的东西。我正在使用树莓派 3B+。

【问题讨论】:

    标签: arrays string arduino raspberry-pi3


    【解决方案1】:

    您可以在 python3 中使用 pyMultiSerial 模块。

    pip3 install pyMultiSerial
    

    在此处查看示例文件夹中提供的示例:https://github.com/SunitRaut/pyMultiSerial/tree/main/Examples

    该模块将使您可以轻松实现所需的应用程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多