Hi,
This is my first time using the RX/TX pins on the Arduino and Teensy to try to read data coming from the Teensy. The code is hacky but only because I’m trying to test the water .
I’m encountering a case where the Arduino will print out messages that I’ve typed into the Arduino’s Serial Monitor but it does not display messages that should be sent over the Teensy’s TX pin. I checked the teensy’s serial monitor and the message the teensy’s suppose to send over to the arduino via its TX pin is printed correctly into the teensy’s serial monitor.
Since I’m seeing messages in serial monitor for both devices, could it be that serial communication is occurring between each respective devices and their USB ports back to my computer, to be displayed on my serial monitor and not to each other?
Hardware setup is pretty simple:
- Arduino connected to COM4
- Teensy 3.1 connected to COM5
- Arduino’s TX/RX connected to Teensy’s RX/TX, respectively.
The Teensy sends a message every second, indefinitely:
void setup() {
Serial.begin(9600); // Teensy Serial obj. always communicates at 12Mbps
}
void loop() {
Serial.write("x<Hello>asdf");
delay(1000);
}
The Arduino Uno checks its Serial Buffer and stores strings enclosed by the “<”, “>” characters e.g., “…” gets stored into a character array as “helloworld\0”.
#define LED_PIN 13
#define BUF_SIZE 100
char msg_Buf[BUF_SIZE];
void initPins(){
pinMode(LED_PIN, OUTPUT);
}
void clearMsgBuf(){
int i;
for(i=0; i<BUF_SIZE; i++)
msg_Buf[i] = '\0';
}
int parseSerial(){
char startMarker = '<';
char endMarker = '>';
char rc;
boolean startRead = 0;
int index = 0;
if(Serial.available() > 0){ //If there's stuff in serial
//Find start of msg using startMarker; if startMarker not found
rc = Serial.read();
while((rc != startMarker) && (Serial.available()>0)){ rc = Serial.read();}
if(rc == startMarker){ startRead = 1;}
else{ return 0;}
//Start reading until '>' is encountered
while((Serial.available()>0) && (startRead==1)){
rc = Serial.read(); //Reads byte from Serial buffer
if(rc != endMarker){
msg_Buf[index] = rc;
index++;
}else if(rc == endMarker){
startRead = 0; //Stop reading
}
}
msg_Buf[index] = '\0'; //Null terminate the msg array
return 1;
}
return 0; //Nothing in serial.
}//End parseSerial()
void setup(){
initPins();
Serial.begin(9600);
Serial.println("==Arduino ready to receive Serial Input==");
}
void loop(){
parseSerial();
Serial.println(msg_Buf);
clearMsgBuf();
delay(500);
Serial.println("...");
}