I’m encountering a very strange problem using g++ 4.1.2. I have a very basic program that opens a serial port and writes data to it. The port is opened & setup using following commands:
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
struct termios port_settings;
cfsetispeed(&port_settings, B115200);
cfsetospeed(&port_settings, B115200);
port_settings.c_cflag &= ~PARENB;
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &port_settings);
I have an array of char consisting of ASCII constants to be sent over the serial link. Data is written using a straightforward write(fd, &serial_out, 1), serial_out being the character to be written. This data is then put onto a 16×2 LCD.
Now here’s the strange thing. As long as I compile with -O0 this all works great. However, if I compile with anything higher (1, 2, 3 or s), all the lowercase characters appear as uppercase on the LCD.
Has anyone got any idea what might be causing this? I might be overlooking something simple, but I really have no clue.
This is because you are basically setting the terminal parameters to random values.
To get more meaningful behavior, first interrogate the serial port’s settings and then change the items as you see fit: