Hi, In the below Code, I am receiving the Data bytes from the serial port using Serial Monitor. Comparing that Serial received data with the Constant Data.
if ( IncomingBytes[0] == 'a')
I am sending the char "a" from the Serial monitor to the Serial Port by printing " Firststagestring". But after the comparison
if ( IncomingBytes[0] == 'a')
," Secondstagestring" is not printed in the serial monitor. What is the mistake in the below code? I have attached the Serial monitor image for your reference. What is the Data Format from the Serial monitor in arduino. Is it hex or Ascii or dec? Kindly help to clear this.
Serial.print("I read from Serial:");
Serial.println(Serial.read());
the 'a' character/byte is read from the serial input buffer, printed, then lost. When you do this next:
IncomingBytes[0] = Serial.read();
if ( IncomingBytes[0] == 'a')
you read a new byte (if any) coming from serial, and that's not the same as the previous one because the "Serial.read()" function pops out the next byte from the input buffer.
Please note an empty read() (i.e. if there are no more bytes in the buffer, due to the fact you don't check "Serial.available()" before the second one) returns -1 so the "if" statement will compare -1 with 97, and being different the code won't be executed.
You just need to do a single read. Like this:
IncomingBytes[0] = Serial.read();
Serial.print("I read from Serial:");
Serial.println(IncomingBytes[0]);
Serial.println("Firststagestring");
if ( IncomingBytes[0] == 'a')
Why this endless loop? It won't ever get out from there, you need to reset the MCU to resume it.
You can't with Serial Monitor because it's for ASCII characters, not binary. You could just try kinda fake mapping a specific character (one you're pretty sure it can't be part of a message, let's say '*') to 0xFF, something like:
IncomingBytes[0] = Serial.read();
if (IncomingBytes[0] == '*') IncomingBytes[0] = 0xFF;
Serial.print("I read from Serial:");
Serial.println(IncomingBytes[0], HEX);
That's just for testing purposes. Please note I used "HEX" parameter for Serial.println() here, because incoming data is binary, I suggest you to keep it.
What is the source of that serial data? Why don't you use another serial (e.g. using SoftwareSerial) and free the onboard serial-USB to use it for debugging/monitoring?
Otherwise you should either use another serial terminal emulator being able to somehow send binary data or write a small PC program to send binary data you need to test with.
This way, you can use "Alt-numkey" to send binary, like "Alt-255" to send 255 or 0xFF, this sketch is my test sketch:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println();Serial.println();
Serial.println("Started. Type in your data (Alt+numkey for binary)");
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()) {
byte c = Serial.read();
if (c != '\r' && c != '\n') Serial.println(c, HEX);
}
}
and this is the output when I type Alt-255, Alt-254 and alt-128:
No need to share any exe file (besides, I won't ever do that for obvious security reasons), MobaXterm is a free software, you can download it here for free and use it for personal purposes: