Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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->cport_nr, connect->bdrate, connect->mode) == 1)
     sleep(1);

 // set connected flag to signal completion of connection to main()
 connect->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->MSB to MSB->LSB
   memset(buf, 0, sizeof buf); // clear buffer
   buf[0] = (g >> 24) & 0xFF;
   buf[1] = (g >> 16) & 0xFF;
   buf[2] = (g >> 8) & 0xFF;
   buf[3] = g & 0xFF;

   if(RS232_SendBuf(connect->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() >= 4) {
    digitalWrite(13, 1);
    for (int i = 0; i < 4; i++) {
      temp[i] = Serial.read();
    }
    result += temp[0] << 24;
    result += temp[1] << 16;
    result += temp[2] << 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

share|improve this question
    
Is there more Arduino code? It does not look complete. – Dave X Mar 5 at 2:51
    
the serial data is sent by RPi but not received by Arduino. - how do you know it is being sent? Maybe not having the serial monitor on the Pi open affects the way it is sending data to the USB port. – Nick Gammon Mar 5 at 4:49
    
@nick-gammon i can see my outgoing message in C which means there's no error encountered while sending the data. – Doe Joe Mar 5 at 6:04
    
@dave-x the complete code is thousands of lines long and only this method is used in loop to listen to what i am sending it – Doe Joe Mar 5 at 6:05
1  
temp = Serial.read(); surely not. Do you mean: temp [i] = Serial.read();? - it's hard to debug code that isn't really what you are using. – Nick Gammon Mar 5 at 6:19

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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