void loop() {
if (Serial3.available()) {
int inByte = Serial3.read();
// Serial.write(inByte);
Serial.println(inByte);
Serial3.flush();
}
else
{Serial.println("-1");}
delay(200);
}
When I connect the compass TX to TX3 I get nothing (Due prints "-1").
When I connect the compass TX to RX3 I get this strange pattern of numbers:
13
10
50
57
53
13
10
50
57
51
13
10
50
57
50
13
I have used this code with and without the flush command and the same thing occurs. I have also tried different baud rates with no success.
I know the serial ports work because I have successfully used them to read from a gps unit on my Due. Thus, I figure the problem must be with my connection of the compass to the board.
I would have thought that the compass was broken or damaged, but I have gotten it to print correct values with one particular set up... If I connect the compass TX to the TX0 port on the Due (and the Rx to the RX0), accurate positions print in real time. However, because of the importance of this serial port to other functions of the board, I obviously do not with to use it.
I am baffled by this situation, and if anyone can help me to understand what may be going wrong with my compass or my serial ports I would appreciate it very very much.
void loop() {
if (Serial3.available()) {
int inByte = Serial3.read();
// Serial.write(inByte);
Serial.println(inByte);
Serial3.flush(); // why are you trying to flush the tx buffer of Serial3?
/* if you want to clear the receive buffer try
while(Serial3.available()) Serial3.read();
*/
}
else
{Serial.println("-1");}
delay(200);
}
When I connect the compass TX to TX3 I get nothing (Due prints "-1"). // male -> male doesn't fit
When I connect the compass TX to RX3 I get this strange pattern of numbers: // male -> female :)
13 = Carriage Return or \r
10 = line feed or \n
50 = '2'
57 = '9'
53 = '5'
13 = Carriage Return or \r
the command
Serial.println(inByte);
is interpreted by the Arduino Compiler as a request to print the value of inByte, I think you actually want to print the displayable character that is represented by the value of inByte. To have the compiler display the character represented by the number in inByte use:
Serial.println((char)inByte); // cast inByte as a char
// or instead of defining inByte as an int, define it as a char
char inByte = Serial3.read();
Serial.println(inByte); // this will print the Character represented by the value of inByte because we
/* defined inByte as a char instead of an int. */
try this substituting this code for your loop:
static unsigned long timeout=0;
void loop(){
while(Serial3.available()){
char inChar = Serial3.read();
Serial.print(inChar);
timeout = millis(); // time last character received
}
if(millis()-timeout>10000){
/* if it has been over 10 seconds since last character received print a warning message
*/
Serial.print("\n-1\n");
timeout=millis(); // start another timeout interval
}
}