Ho scaricato questo codice da internet ma a me sembra non funzionare.
Dal pc invio !100*
Sullo sniffer seriale vedo !1100*
Ma arduino non processa il messaggio.
Praticamente sembra che non entri mai dopo il while !!
Uso arduino mega con max232 e ricevo sulla porta1 e visualizzo i messaggi sulla porta0
Grazie per l'aiuto
Alverman
Questo il codice:
/*===========================
TITLE: Serial Echo Code v1.1
AUTHOR: Fayaz Kadir
DESCRIPTION: If you send a string starting with "!" and ending with "*", the Arduino responds back with "$Y" for valid data string, or $N for invalid data string. You can modify this code for your use.
COMPATIBLE BOARD: Arduino Mega (but any other Arduino should work fine)
============================*/
char string[25];
int i=0;
int string_len;
int inByte;
int last_inByte;
void setup() {
Serial1.begin(9600); //INTIALISING THE SERIAL PORT
Serial.begin(9600); //INTIALISING THE SERIAL PORT
}
void loop() // LOOP FUNCTION
{
inByte = Serial1.read();
string_len=0;
if (inByte == '!') read_serial(); // If Start of line ("!") is found, call read_serial() function
}
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE
{
Serial.println ("SOL FOUND and reading"); // THE SOL (Start of Line) found notice.
Serial.print("READ : !\n"); //Saying that it has read "!" on the serial port
while (inByte != '*') // As long as EOL not found, keep reading
if (Serial1.available() > 0) // if new data is available
{
inByte = Serial1.read(); // Read new byte
Serial.print("READ : "); // Display the new byte
string[string_len] = inByte; // Save the data in a character array
Serial.println(string[string_len]); // Print the characters that was recieved
string_len++;}
else if (Serial1.available() == 0)
{Serial.println("EOL not available, data string invalid"); // If EOL not in the string
Serial1.println("$N"); //FAIL SIGNATURE
break;
}
if (inByte == '*')
{
Serial.println ("eol FOUND, and full string was"); // Echoes Success Message
Serial1.println ("$Y"); //SUCCESS SIGNATURE
for (i=0;i<(string_len-1);i++) Serial.print(string[i]);
Serial.print ("\n");
}
}