Problem receiving data from RPi to Arduino via serial Our team has been trying to send data over from RPi to Arduino but there are some weird things happening:

If we open the serial monitor of arduino on RPi itself, the serial data is sent by RPi and received by Arduino successfully.

However, if we do not open the serial monitor of arduino on RPi, the serial data is sent by RPi but not received by Arduino.

The communication between them is via USB cable.

Below are the codes implemented on RPi (using RS232 Linux library in C):

void *rs232t_connect(void *arg)
{
 struct RS232Connection *connect = (struct RS232Connection*)arg;

 // connect to USB port ttyACM0
 fprintf(output, "Waiting for connection on Comport ttyACM0 (24)...\n");
 while(RS232_OpenComport(connect-&gt;cport_nr, connect-&gt;bdrate, connect-&gt;mode) == 1)
     sleep(1);

 // set connected flag to signal completion of connection to main()
 connect-&gt;connected = true;
 fprintf(output, "Connection accepted.");

 return 0;
}




void *rs232t_send(void *arg)
{
 struct RS232Connection *connect = (struct RS232Connection*)arg;
 unsigned char buf[4];
 int32_t g;
 bool reconnect = false, resend = false;

 while(1)
 {
   // sample
   g = 10;

   // convert from LSB-&gt;MSB to MSB-&gt;LSB
   memset(buf, 0, sizeof buf); // clear buffer
   buf[0] = (g &gt;&gt; 24) &amp; 0xFF;
   buf[1] = (g &gt;&gt; 16) &amp; 0xFF;
   buf[2] = (g &gt;&gt; 8) &amp; 0xFF;
   buf[3] = g &amp; 0xFF;

   if(RS232_SendBuf(connect-&gt;cport_nr, buf, sizeof(int)) == -1)
   {
     // Print error msg if sending failed
     fprintf(output, "Disconnected\n");
   }
   else
   {
     fprintf(output, "Outgoing Message: %d\n", g); // DEBUG
     g = 0;
   }
 }
 return 0;
}


Arduino Code:

long readSerialInt() {
  long result = 0;
  long temp[4];
  if (Serial.available() &gt;= 4) {
    digitalWrite(13, 1);
    for (int i = 0; i &lt; 4; i++) {
      temp[i] = Serial.read();
    }
    result += temp[0] &lt;&lt; 24;
    result += temp[1] &lt;&lt; 16;
    result += temp[2] &lt;&lt; 8;
    result += temp[3];
    return result;
  }
  else {
     return 0;
  }
}


Any idea why this is happening? Its weird that we need to open the serial monitor of Arduino IDE on RPi before the message gets sent out.

We are sending data over to Arduino. However Arduino does not detect the messages we are sending to it (it didnt perform any action/light up any LED).

Thank you
The problem may be with your code, especially the function for opening the port. Try this in Python 3 and see if it works.

import serial, time

ser = serial.Serial('/dev/tty/ACM0', 9600, timeout=1)

while ser.isOpen():
    try:
        ser.write(b'Hello')
        time.sleep(0.1)
        print('Received: ')
        while ser.inWaiting():
            print(str(ser.read(), encoding='ascii'), end='')
        print()
    except KeyboardInterrupt:
        ser.close()


Arduino side:

void setup(){
  Serial.begin(9600);
}

void loop(){
  while (Serial.available())
    Serial.write(Serial.read())
}


Crude, but it is a lot more straightforward than your program and will help you isolate the problem.