Go Down

Topic: Reading large serial inputs (Read 112 times) previous topic - next topic

ON3WVS

Sep 14, 2019, 04:59 pm Last Edit: Sep 14, 2019, 05:15 pm by ON3WVS
Hello,

I would like to read some serial data into my arduino, and therefore I've found on this website the "Serial Input Basics" tutorial. There I used the code for receiving several characters, and this works very good.

However, if my received data is larger than 255 characters, then I only see the first 255. Is there a way that I can read more than 255? Here is the code which I've used:

Code: [Select]

const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data

boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvWithEndMarker();
 showNewData();
}

void recvWithEndMarker() {
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 
 // if (Serial.available() > 0) {
           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;
 }
}


blh64

your variables 'numChars' and 'ndx' are of type byte which means they can only hold 0-255.  You will have to make them int or unsigned int if you want to hold more.

ON3WVS

your variables 'numChars' and 'ndx' are of type byte which means they can only hold 0-255.  You will have to make them int or unsigned int if you want to hold more.
receivedChars is also a Char type, so it can also not contain more than 255 characters, correct? Can I make this a String then?

Robin2

#3
Sep 14, 2019, 06:15 pm Last Edit: Sep 14, 2019, 06:16 pm by Robin2
receivedChars is also a Char type, so it can also not contain more than 255 characters, correct? Can I make this a String then?
No that's incorrect thinking. You are mixing up the type of an item with the number of those items.

The array receivedChars can hold as many characters as there is space in the Arduino SRAM. My tutorial assumes you will be receiving alphanumeric characters. If you need to receive bytes then just declare the array as a byte array. Obviously it would also make sense to change the name to receivedBytes but that is not essential - the compiler does not care what name you use.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ON3WVS

No that's incorrect thinking. You are mixing up the type of an item with the number of those items.

The array receivedChars can hold as many characters as there is space in the Arduino SRAM. My tutorial assumes you will be receiving alphanumeric characters. If you need to receive bytes then just declare the array as a byte array. Obviously it would also make sense to change the name to receivedBytes but that is not essential - the compiler does not care what name you use.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time.

...R
Thanks Robin! It worked great! I'm a newbie at this but learning every day. Thx!

GolamMostafa

If you need to receive bytes then just declare the array as a byte array. Obviously it would also make sense to change the name to receivedBytes but that is not essential - the compiler does not care what name you use.
But, a human reader does care; therefore, an identifier should be a meaningful name and very much related with the context.

Robin2

But, a human reader does care; therefore, an identifier should be a meaningful name and very much related with the context.
Agree 100%

...R
Two or three hours spent thinking and reading documentation solves most programming problems.


Go Up