I have to test output from a program send to the Arduino.
There is no documentation available how the strings send are terminated.
So i wrote a simple program to show and check these.
/*
* Displays text sent over the serial port (e.g. from the Serial Monitor) on
* an attached LCD.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20,20,4); // set the LCD address to 0x20 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
Serial.begin(9600);
}
void loop()
{
char ch;
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
// lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
ch = Serial.read();
if(ch == 0x10) lcd.print("\r");
else if(ch == 0x12) lcd.print("\c");
else
lcd.write(ch);
Serial.write(ch);
}
}
}
But i get the above mentioned arror. How can this be as i am only checking for it and then print the representation of it.
There is no escape character \c
It looks like you took the hex for decimal 12 which is c and put a \ before it.
Copy the page linked to above into a place you can get to quickly, it is useful.
ascii codes and their escape sequences
ASCII Name Description C Escape Sequence
Found the error.
Using else if(ch == 0x12) lcd.print("\c"); i wanted to print the received character.
But i should have been else if(ch == 0x12) lcd.print("/c");
lcd.print("\c") would actualy print the escape character
PaulS:
Why are you looking for 0x12 at all? The usual terminating characters are 0x10 and 0x13.
As i dont know what the program sends to the output.
But i got a new problem.
void loop()
{
char ch;
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
ch = Serial.read();
if(ch == 0x10) lcd.print("/r");
else if(ch == 0x12) lcd.print("/f");
else if(ch == 0x13) lcd.print("/c");
else
lcd.write(ch);
}
}
}
Now the LCD shows the non printable character but not the if result.
Is the IF ELSE contruction correct?
PaulS:
Why are you looking for 0x12 at all? The usual terminating characters are 0x10 and 0x13.
As i dont know what the program sends to the output.
But i got a new problem.
void loop()
{
char ch;
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
ch = Serial.read();
if(ch == 0x10) lcd.print("/r");
else if(ch == 0x12) lcd.print("/f");
else if(ch == 0x13) lcd.print("/c");
else
lcd.write(ch);
}
}
}
Now the LCD shows the non printable character but not the if result.
Is the IF ELSE contruction correct?
Just read the serial input and print the characters as HEX. Then you will know what you get.
I'm not a big fan of cascading if statements...I find them hard to read. Anytime something is "hard to read", I look for an easier way to read and debug the code. For me, this is much easier to read:
switch (ch)
{
case 0x10:
lcd.print("\r");
break;
case 0x12:
lcd.print("/c");
break;
default:
lcd.write(ch);
break;
}
I know it's a matter of preference, but if you can use a switch to replace a cascading if, it's almost always easier to read.
Why print a /c when a carriage return arrives? Why send binary data to the LCD at all?
As i wrote before its a test to determinate what a application on my PC is sending to the COM-port
So if there is a CR i would like to know, also for the LF and FF.
Sending binary data to the LCD comes from a example, didnt change it.
Changing the IF ELSE statment to SWITCH doesnt solve the problem.
Still the nonprintable character is shown.
Maybe i should search for a USB-COM port sniffer but i would like to see it realy happening in my Arduino
That's why you should send the hex of the characters to the LCD.
The hex will show everything while the control characters will not.
What you do now is just fooling around.