I am using the code from Example 2 in this post: Serial Input Basics - updated to read data coming via serial. I'm using an ESP32 using Arduino framework on PlatformIO.
The code below is only allowing me to use keyboard input but I would like to send commands from a Qt program using QSerialPort to write("hello").
Any suggestions on how to modify the code?
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void recvWithEndMarker();
void showNewData();
void setup() {
Serial.begin(9600);
Serial.println("");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.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;
}
}