Using a Yun, the following code does not appear to receive any data from my Serial device... For testing, the serial device is a PC using SerialMon, connected using a USB-RS232 adapter, a DFRobot RS232-TTL serial adapter, and that has its RX connected to Pin 2, and TX connected to pin 3 on arduino.
I have tested the following code on my Arduino Uno, and it works fine. When I send the following ASCII msg to the Uno from SerialMon, I get a response of the parsed data:
ASCII msg sent to Uno from SerialMon:
11:35:58 015.4 0238.5 0026.4 0061.7 000001 0015.4
Response from Uno via Serial():
Read data
Read data
Read data
Read data
11:35:58
wSpd:15.4
wDir:239
temp:26.4
humi:62
end of data
However, when I use the same code and setup on my Yun, I do not get any response from it when sending the above ASCII msg, just "Read Data" repeating every 5 secs. Something is going on here and the SoftwareSerial on the Yun is not receiving data... Hopefully a second set of eyes or some experience can identify the issue. Not making much sense to me.
Arduino Yun Serial() ouput:
Read data
Read data
Read data
Read data
Read data
Read data
Read data
...
Arduino Sketch used on both Uno and Yun:
#include <SoftwareSerial.h>
SoftwareSerial RS232(2,3);//RX,TX
void setup() {
// initialize serial:
RS232.begin(9600);
Serial.begin(9600);
}
void loop() {
delay(5000);
String time;
float wSpd;
float wDir;
float temp;
float humi;
String data;
int j = 0; //data parameter index
Serial.println("Read data");
while (RS232.available() > 0) {
char c = RS232.read();
if (c == ' '){ //space-seperated data
switch (j){
case 0:
time = data;
time.trim();
Serial.println(time);
break;
case 1:
wSpd = toFloat(data);
Serial.print("wSpd:");
Serial.println(wSpd,1);
break;
case 2:
wDir = toFloat(data);
Serial.print("wDir:");
Serial.println(wDir,0);
break;
case 3:
temp = toFloat(data);
Serial.print("temp:");
Serial.println(temp,1);
break;
case 4:
humi = toFloat(data);
Serial.print("humi:");
Serial.println(humi,0);
break;
default:
Serial.println("end of data");
//RS232.flush();
}
j ++;
data = "";
}
else{
data += c;
}
if (RS232.available() == 0){
Serial.println("All Data Read");
}
}
}
float toFloat(String input){
char buffer[8];
input.toCharArray(buffer,sizeof(buffer));
return atof(buffer);
}
Next, to reduce possibilities, I removed the RS232-TTL adapter from the system and used the Uno to send serial data to the Yun with this sketch. I hooked up the TX from the Uno to pin 2 of the Yun. The following code was uploaded to the Uno:
void setup() {
Serial.begin(9600);
}
void loop() {
delay(3000);
Serial.println("11:35:58 015.4 0238.5 0026.4 0061.7 000001 0015.4");
}
This also did not provide any response from the Yun. Am I doing something wrong? Are there any issues with SoftwareSerial library and Yun? Is my Yun messed up? Thanks for the help.