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
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:49temp = 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