I'm wishing to get away from Strings, use char arrays. Thought I was doing simple practice & familiarization - but that was 10 hrs ago....
code & annoying output attached. Using MEGA2560 w/ IDE on a PC.
Question1: Reference manual says "Serial.available()" returns number of bytes available to read, but I always get a return value of 1. What am I missing?
Real question is below: HUH! what am I missing...
part A - entered "cows" on serial monitor and output was just as expected except for the "chars available to read = 1", nevertheless got an output from reading the array "inStrings[]" char by char of "cows". Great.
part B - entered "cowboys" but got NO output from reading "inStrings[]" char-by-char. Not Great. Then entered "bears" & got no output.
part C - then entered "bear" & got GOOD OUTPUT.
part TheEnd - "inStrings[]" is 10 slots but never get good output if there are more than 4 printable chars in it. At least that's what I think. Have done this 9 ways to Sunday before I bothered y'all.
Free beer in Dallas for any useful insights....and THANKS.
Chuck
This is simple code. Prompt to enter anything on serial monitor & put up to 9 chars in the array "inString[]" then put a NULL at their end. Reset the input location to 0 and do it again.
// name is "sketch_Cstrings2"
// 12/01/20214
// on MEGA2560
// at home in CUBBY
#include <stdio.h>
#include <string.h>
char pgmName[] = "sketch_Cstrings2";
int i = 0,j = 0,k = 0,loopCnt = 0;
int charInCnt = 0;
int num1 = 0, num2 = 0, num3 = 0;
const int maxInLen = 10;
volatile char inString[maxInLen];
const int maxOutLen = 50;
volatile char outString[maxOutLen];
char inChar;
void setup() {
Serial.begin(115200);
while (!Serial) {;}
Serial.print("pgmName: ");
Serial.println(pgmName);
for(i=0; i<(maxInLen-1); i++) inString[i] = ' ';
inString[i] = '\0';
for(i=0; i<(maxOutLen-1); i++)outString[i] = ' ';
outString[i] = '\0';
Serial.println("GOING TO LOOP.");
}// END setup()
void loop(){
loopCnt++;
inChar = ' ';
charInCnt = 0;
if(k == 0){
Serial.println("enter a topic then press ENTER");
k = 1;
}//END if k
while(Serial.available()>0){
if(k == 1){
num1 = Serial.available();
Serial.print("chars to print? = "); Serial.println(num1);
k = 2;
}
inChar = Serial.read();
if(isPrintable(inChar) && (charInCnt <= maxInLen - 2)){
inString[charInCnt] = inChar;
charInCnt++;
Serial.println(inChar);
}
else{//END if printable
inString[charInCnt] = '\0';
if(charInCnt >= maxInLen-1) Serial.println("inString is FULL!");
Serial.println("next line is inString[]");
for(i=0; i<charInCnt; i++) {Serial.write(inString[i]);}
Serial.println();
k = 0;
}
}//END while avbl
}// END loop()
Serial monitor Output:
pgmName: sketch_Cstrings2
GOING TO LOOP.
enter a topic then press ENTER
chars to print? = 1
c
o
w
s
next line is inString[]
cows
enter a topic then press ENTER
chars to print? = 1
c
o
w
b
o
y
s
next line is inString[]
enter a topic then press ENTER
chars to print? = 1
b
e
a
r
s
next line is inString[]
enter a topic then press ENTER
chars to print? = 1
b
e
a
r
next line is inString[]
bear
enter a topic then press ENTER