简体   繁体   中英

Losing bytes over Serial communication from Raspberry PI 3 to Arduino UNO

So I have this OpenCV Project at Uni where we build an Ambilight System using a Raspberry Pi and an Arduino, as well as an LED Strip with 144 LEDs and a webcam.

I basically read 144 RGB values from the Borders of the TV-Screen via the Webcam, OpenCV and Python and send them, including the positions (indexes from 0 to 143) over Serial via USB to the Arduino. The Arduino then sets the corresponding LED and that's it.

My Problem is that on the way from Raspberry Pi to the Arduino, some of the sent bytes dissapear.

I've tried different baud rates. 9600 and 57600 to be exact.

here's the responsible Python code

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]))

and what the print statements outputs:

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'

...

and here's the responsible Arduino code


        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);    

and what gets send back from the Arduino via the Serial.println

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

As seen on package 6, bytes are getting lost. This isn't consistent. Sometimes bytes are lost in the first or second package. The LEDs are getting set corresponding to neither the values I get back from the Arduino nor the values I send from the RasPi, so I know that bytes are lost on both ways.

I also have a second method on my Arduino, where I set the whole LED-Strip at once, sending only one three-byte-package, which works just fine.

Is trying to send 144 4-Byte-packages at once over Serial simply to much? Or should this be Possible, in which case I should check the USB-Cable? Or is my Python code garbage?

Thanks for any help in advance.

Paul

Seems like the USB-Port on my Arduino UNO was damaged.

I tried different cables but got the same result. However, switching to an Arduino Micro solved the Problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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