0

I've been trying to establish serial (UART) communication between a Raspberry Pi Model B Revision 2.0 (checked the model like described on this page) and Arduino Mega 2560. I made a service on the Pi that writes to UART and then expects a message and a coworker programmed the Arduino with an echo program. While they were communicating, I had trouble receiving data, meaning that it was clustered in 8 byte pieces and I had to introduce a timeout for waiting between them (I was actually as much as available and calling select()for the next cluster but it turned to be 8bytes a cluster, except for maybe the last one. As explained in a question I found on this site, the programmer is the one to take care of the protocol and can not rely that the whole message will be ready to read at once (that is logical). However, when I just connected Pi's TXD and RXD pins, no matter how much bytes I tried sending, it sends them in one go (I've gone up to a bit more than 256, that's more than enough for my purposes). I also have around 50 milliseconds of duration difference, measured directly from within the program, using gettimeofday() function.

So, could anybody clear things for me:

  1. Why is this happening?
  2. Is this difference in behaviour expected?
  3. Is there a potential problem in either of the devices (if that can even be concluded from the given information).

Of course, any additional information is welcome, in case I forgot asking something that is deemed important.

NMilev
  • 77
  • 2
  • 11

1 Answers1

0

Why is this happening?

I tried some time back communicating Arduino-Arduino and Arduino-Pi. I faced some problems with UART communication. However, you might want to keep same Baud rate on both the devices. With Pi, you might need to trigger an event if you receive data from Arduino. On the other side, if you code runs longer, then you might lose some data i.e. your Arduino code is running something else while Pi sends data over UART.

Is this difference in behaviour expected?

Yes. Arduino is a microcontroller based device while Pi is microprocessor based (runs on OS)

Is there a potential problem in either of the devices (if that can even be concluded from the given information).

I don't think there could be any hardware problem unless it is not functioning at all.

Also, because of this issues, I switched from UART communication to SPI communication. This solved my problem completely.

Community
  • 1
  • 1
stuartnox
  • 649
  • 1
  • 6
  • 15
  • 1
    Baud rates are the same (they have to be, AFAIK), as well as all the settings regarding packing the byte (parity, stop). Actually, tried it a couple days ago and it all seems to be working completely fine now. The echo service is now ~70 ms for ~260 characters, which is a tad slower than the theoretical number of bytes that should be received but it's okay. The only thing that's changed is the wires I use to connect the devices. I redid them and it's working properly. Actually have no idea why this is happening. UART is required by the employer, so I can't make a change. – NMilev Sep 05 '16 at 08:05