Hi,
I started this new topic but first of all I apologize for a, I suppose, boring question.
As many of us, I'm trying to collect data from a serial data source (e.g. a GPS, but also other sources) and to do something with this data (e.g. store the data in a file on a SD).
When the serial data source is the USB and I use the serial monitor of IDE everything works using the best-known code "serialEvent" like:
//***********************************************************************
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// blocco di debug per vedere sescrive ogni carattere sul file e se blink IL LED 13
f.print(inChar);
f.flush();
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(100);
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
This means that the function serialEvent() is called properly when something arrives in the serial port; in this piece of code there are also some "blink" to see if the function is really called.
But, when I detach the USB cabel and attach the wires of the serial source to pin 0,1 and GND (TX, RX and Ground) and restart the program, it seems that nothing arrives from the serial port and the serialEvent() is never called.
This following simplified test code proof the fact, as no blnk can be seen even if he blink instruction is now the first instrucion of serialEvent()
int incomingByte = 0; // for incoming serial data
String inputString="";
boolean stringComplete=false;
//***********************************************************************
void serialEvent() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
// digitalWrite(13, HIGH);
// delay(200);
// digitalWrite(13, LOW);
if (inChar == '\n') {
stringComplete = true;
}
}
}
//******************************************************************
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
inputString.reserve(200);
}
//***********************************************************************
void loop() {
}
The serial data source works properly, this has been checke attaching the wires directly to USB wires and checking the dada via terminal server.