I'm using WiringPi's library to read information from my Arduino on my Raspberry Pi 3b. The RPi is connected to the Arduino as so:

RPi <-----> Arduino

8 (TX) <-> 17 (RX2)

10 (RX) <-> 16 (TX2)

I'm doing a very simple test to make sure it works. From my Arduino, I have the following code:

void setup() 

void loop() 
  Serial2.write("Just a test");

Now, for my C code on the Pi:

  msgLength = serialDataAvail(fd);
  printf("%i \n",msgLength);
  printf("%s \n", ArduinoMsg);

I removed everything else so it's easier to read and gets straight to the point.

Here's My Issue:

serialDataAvail is suppose to return the number of bytes it has to read BUT it always returns 0. No matter what. The thing is, it still outputs values, as can be seen in the image below: Output from running the code ./Receive is just the name of my C code. I have Transmit and Receive and am only having issues with Receive now.

My Thoughts:

Since it keeps outputting Putty every two tries, I believe maybe the SSH program is somehow effecting the serial communication.

I have searched around to find why the function serialDataAvail() is giving me a 0 when there is clearly something in the buffer but could not find any answers.


I experimented with minicom -b 9600 -o -D /dev/ttyAMA0 and I am receiving the characters from the Arduino clearly. Which means there is an issue with my code. When I figure it out, I will post it for those who run into similar issues in the future.


1 Answer 1


I figured out the issue. Posting in hopes that it will help somebody in the future

  1. I flushed the UART system to get rid of the garbage from before.
  2. I needed to add a delay before serialDataAvail(fd); was called to allow the buffer time to receive its characters.
  3. I needed to add a loop to all the code to recheck and see. I probably should have used a for loop to limit the amount of times it checks
  4. I don't know if it was needed per say but it definitely helped I changed the ArduinoMsg variable from a char to a uint8_t

It ended up looking like this:



  while(!(msgLength = serialDataAvail(fd)))
        printf ("%i \n", msgLength);
  printf ("%i \n", msgLength);

  ArduinoMsg[msgLength] = 0;
  printf ("%s",ArduinoMsg);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.