【问题标题】:Losing bytes over Serial communication from Raspberry PI 3 to Arduino UNO从 Raspberry PI 3 到 Arduino UNO 的串行通信丢失字节
【发布时间】:2019-05-11 11:30:16
【问题描述】:

所以我在 Uni 有这个 OpenCV 项目,我们使用 Raspberry Pi 和 Arduino 以及一个带有 144 个 LED 和一个网络摄像头的 LED 灯带构建了一个流光溢彩系统。

我基本上通过网络摄像头、OpenCV 和 Python 从电视屏幕的边框读取 144 个 RGB 值,然后通过 USB 将位置(索引从 0 到 143)发送到 Arduino。 然后 Arduino 设置相应的 LED,就是这样。

我的问题是,在从 Raspberry Pi 到 Arduino 的途中,一些发送的字节消失了。

我尝试了不同的波特率。准确地说是 9600 和 57600。

这是负责的 Python 代码

def send():
    threading.Timer(5,send).start()
        values = []
    for pnt in cts:
        values.append(blur[pnt[1],pnt[0]])

    for idx, val in enumerate(values):       
        ser.write(struct.pack('>BBBB',idx,val[2],val[1],val[0]))
        print("{}    {}    {}    {}".format(idx,val[2],val[1],val[0]))
        print(struct.pack('>BBBB',idx,val[2],val[1],val[0]))

以及打印语句的输出:

0    128    188    216
b'\x00\x80\xbc\xd8'
1    136    198    224
b'\x01\x88\xc6\xe0'
2    150    202    226
b'\x02\x96\xca\xe2'
3    151    207    230
b'\x03\x97\xcf\xe6'
4    149    217    233
b'\x04\x95\xd9\xe9'
5    159    219    233
b'\x05\x9f\xdb\xe9'
6    160    215    236
b'\x06\xa0\xd7\xec'
7    161    224    236
b'\x07\xa1\xe0\xec'
8    163    219    232
b'\x08\xa3\xdb\xe8'

...

这是负责的 Arduino 代码


        while(Serial.available() >= 4){
        for (int i = 0; i < 4; i++){
          incoming[i] = Serial.read();
        }
        bytePos = incoming[0];
        byteR = incoming[1];
        byteG = incoming[2];
        byteB = incoming[3];

        Serial.println(bytePos);
        Serial.println(byteR);
        Serial.println(byteG);
        Serial.println(byteB);
        Serial.println();


      }
      strip.setPixelColor(bytePos, (byte) byteR*normalized, (byte) 
      byteG*normalized, (byte) byteB*normalized);    

以及通过 Serial.println 从 Arduino 发回的内容

0
128
188
216

1
136
198
224

2
150
202
226

3
151
207
230

4
149
217
233

5
159
219
233

6
160
8
163

219
232
9
164

222
235
10
164

从包 6 中可以看出,字节正在丢失。这并不一致。有时字节会在第一个或第二个包中丢失。 LED 的设置既不对应于我从 Arduino 返回的值,也不对应于我从 RasPi 发送的值,所以我知道双向都丢失了字节。

我的 Arduino 上还有第二种方法,我一次设置整个 LED-Strip,只发送一个三字节包,效果很好。

是否试图通过串行一次发送 144 个 4 字节数据包? 或者这应该是可能的,在这种情况下我应该检查 USB 电缆吗? 还是我的 Python 代码是垃圾?

提前感谢您的帮助。

保罗

【问题讨论】:

    标签: python arduino raspberry-pi serial-port serial-communication


    【解决方案1】:

    似乎我的 Arduino UNO 上的 USB 端口已损坏。

    我尝试了不同的电缆,但得到了相同的结果。 然而,切换到 Arduino Micro 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多