It happens to get an outdated response from the hardware serial and i cannot understand how and why.
I'm using arduino mega 2596 and the hardware serial 1 is connected to to a hc05 that acts as a master and it is connected to an ELM327 bybluetooth.
so, i have the folowing sketch and it's based on the arduino examples
String inputString = ""; // a String to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
Serial.begin(9600);
Serial1.begin(115200);
inputString.reserve(200);
}
void loop() {
if (Serial.available()) { // If anything comes in Serial (USB),
Serial1.write(Serial.read()); // read it and send it out Serial1 (pins 0 & 1)
serialEvent();
}
// if (Serial1.available()) { // If anything comes in Serial1 (pins 0 & 1)
// Serial.write(Serial1.read()); // read it and send it out Serial (USB)
// }
// print the string when a newline arrives:
if (stringComplete == true) {
Serial.println("complete string printing:");
Serial.println(inputString);
Serial.println("done!");
// clear the string:
inputString = "";
stringComplete = false;
Serial.println("string cleared?");
Serial.println(inputString);
}
}
void serialEvent() {
while (Serial1.available()) {
// get the new byte:
char inChar = (char)Serial1.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '>') {
stringComplete = true;
}
}
}
The thing is that:
for the first command i send by serial i get nothing, NO FEEDBACK
for the second command/message i'm sending by serial is the actual response that i was supposed to get for the first command i sent, wich is this:
complete string printing:
ATI
ELM327 v1.5
>
done!
string cleared?
for the third one is the response i was supposed to get for the second one and so on...
Why are you calling serialEvent()? The Arduino calls that useless function only when there is serial data to be read. You are calling it regardless of whether there is data, or not, to read.
void serialEvent() {
while (Serial1.available()) {
Why are you dealing with Serial1 in serialEvent()?
char inChar = (char)Serial1.read();
Do you REALLY think that the explicit cast will do something more than the implicit cast would do? If so, just what do think the difference(s) are?
Serial1.write(Serial.read()); // read it and send it out Serial1 (pins 0 & 1)
Which Arduino are you using? What is connected to the pins 0 and 1?
i used your second example and it works perfect. Thank you!
// Example 2 - Receive with an end-marker
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
Serial1.begin(115200);
}
void loop() {
recvWithEndMarker();
showNewData();
if (Serial.available()) { // If anything comes in Serial (USB),
Serial1.write(Serial.read()); // read it and send it out Serial1 (pins 0 & 1)
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '>';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
//Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
This was just a test to help me get used with the responses I get from the ELM327, now I need it to include it to my Blynk sketch to process the data received from ELM327 and later from a GPS module.
So:
In the Serial Monitor, after i send 5 commands, as a response i receive one row of data and i can see the feedback like this (BOTH NL and CR):
<Arduino is ready>
ATIELM327 v1.5
AT SP 3OK
ATTP3OK
0105BUS INIT: ...OK41 05 38
010541 05 39
But, if i copy paste it in a text editor i can see them like this:
<Arduino is ready>
[b]ATI[/b]
ELM327 v1.5
[b]AT SP 3[/b]
OK
[b]ATTP3[/b]
OK
[b]0105[/b]
BUS INIT: ...OK
41 05 38
[b]0105[/b]
41 05 39
Everything in bold is the actual feedack from ELM327 and i can disable it, but i think is usefull.
I am a bit confused.Can you help me understand? How can i start reading the data? should i count a character right where is suposed to be the hidden newline?
What you are trying to do is really serial_comm_101 and is done by many on a regular basis without issue. To get a handle on this you need to follow Robin's advice in #3. Communicating with the outside world is a fundamental requirement in using MCUs and needs to be understood. We will help you to understand, but please don't ask for canned answers as this won't accelerate the learning process.
rotarucosminleonard:
So:
In the Serial Monitor, after i send 5 commands, as a response i receive one row of data and i can see the feedback like this (BOTH NL and CR):
Your description is not sufficiently clear.
Do you mean that you get one line of reply in response to one command?
If so, stick with a single command until you understand the process.
But, if i copy paste it in a text editor i can see them like this:
What have you copied that text from?
What text editor are you using?
How can i start reading the data?
In what way do you want to read (or interpret) the data?