-1

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

  • Is there more Arduino code? It does not look complete. – Dave X Mar 5 '16 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 '16 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 '16 at 6:04
  • 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 '16 at 6:19
  • 1
    @NickGammon error during posting! edited in original post. theres no way i can tell if it "actually departs from the Pi" when the library method doesn't return -1 which indicates an error. The only way to test is to see the light blink in Arduino itself but in our case the light didn't blink. will continue testing again – Doe Joe Mar 5 '16 at 7:57
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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