Offline
Newbie
Karma: 0
Posts: 32
|
 |
« on: February 06, 2013, 05:33:46 am » |
Hi there, I'm interfacing a LCD with a consumer, prompting to enter some text via serial. I have some default text (easy enough), but I will like to limit input to 8 character strings. What should I do: - Define input as char text[8].
- Define input as String.
In either case, how to limit Serial.read() to read only 8 chars? (If loop with string.lenght == 8 )? or is there a more efficient way?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35519
Seattle, WA USA
|
 |
« Reply #1 on: February 06, 2013, 05:37:40 am » |
What should I do: Neither. 8 characters need a 9 element array, so there is room for the terminating NULL. In either case, how to limit Serial.read() to read only 8 chars? That depends on what you intend to do if there is more than 8 characters sent. I would read and store the first 8, and read and discard the rest until the end-of-packet marker arrived.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #2 on: February 06, 2013, 05:45:55 am » |
What should I do: Neither. 8 characters need a 9 element array, so there is room for the terminating NULL. Ok, got it... Forgot about the NULL. Are you suggesting me to go char text[9] instead of string? In either case, how to limit Serial.read() to read only 8 chars? That depends on what you intend to do if there is more than 8 characters sent. I would read and store the first 8, and read and discard the rest until the end-of-packet marker arrived. I really want users to think through and only input 8 characters. If there is more than 8 chars, warn user and ask again. Only 'save' (change variable) if the input is 3<=text>=8, then LCD print some info with the user input!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35519
Seattle, WA USA
|
 |
« Reply #3 on: February 06, 2013, 06:41:49 am » |
Are you suggesting me to go char text[9] instead of string? A string is a NULL terminated array of chars. A String, on the other hand, is a horrid beast that has no place on the Arduino. I really want users to think through and only input 8 characters. If there is more than 8 chars, warn user and ask again. Only 'save' (change variable) if the input is 3<=text>=8, then LCD print some info with the user input! How will you know when they are done with input? Where is the input coming from? You can count the characters received, and store the first 8 in the array. If the end-of-packet marker arrives before you get to 9, then use the data. Otherwise, reject it. It ain't rocket surgery.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #4 on: February 06, 2013, 06:57:05 am » |
I was thinking in something like this:
tmpName = Serial.read()
if (strlen($tmpName) < 2 || strlen($tmpName) > 8 ) Serial.println("The input is not valid!, please retry (min: 3 chars, max 8 chars)"); else char Name[9] = tmpName
But I don't know how to work around strlen()
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35519
Seattle, WA USA
|
 |
« Reply #5 on: February 06, 2013, 07:05:19 am » |
But I don't know how to work around strlen() I don't understand why you think you need to. That example won't work, for several reasons. First, Serial.read() returns ONE character, not a string. Second, there is no variable defined called $tmpName, which is not a valid name in C++. Third, you can't assign an array (if that is what the undefined tmpName is supposed to be) to another array that way.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #6 on: February 06, 2013, 07:09:21 am » |
OK, I get It... I was trying to do the 3rd.
I wanted to add a string or an array of characters after user inputs the text and hits "send"
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35519
Seattle, WA USA
|
 |
« Reply #7 on: February 06, 2013, 07:24:56 am » |
I wanted to add a string or an array of characters after user inputs the text and hits "send" strcat() does that.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #8 on: February 06, 2013, 07:30:54 am » |
OK, but how can I limit strcat() to 8 chars?
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 31
Posts: 1190
|
 |
« Reply #9 on: February 06, 2013, 07:37:18 am » |
You could overload the function to add in another parameter, or put a null in the source at the position you want it to stop copying at. If needed, store the 8th character in a temp, replace with null, when done just put the temp char back.
|
|
|
|
|
Logged
|
|
|
|
|
Denmark
Offline
God Member
Karma: 19
Posts: 683
|
 |
« Reply #10 on: February 06, 2013, 07:48:46 am » |
You could read and count the characters until you reach the terminator, while you store the characters in a character array. The terminator could be Carriage return, or other special you choose If the count is >8, warn the user, ans start over.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #11 on: February 06, 2013, 08:02:32 am » |
You could read and count the characters until you reach the terminator, while you store the characters in a character array. The terminator could be Carriage return, or other special you choose If the count is >8, warn the user, ans start over.
This sounds great, though I'm not sure how to implement the If. I have this so far: char Name[9]; // My Data array char inChar; // Where to store the character read byte index = 0; // Index into array; where to store the character
if(index < 8){ inChar = Serial.read(); Name[index] = inChar; // Store it index++; // Increment where to write next Name[index] = '\r'; // Carriage to terminate the string } else{ Serial.println("Invalid Input, please enter 3 to 8 characters"); }
Will that work?
|
|
|
|
|
Logged
|
|
|
|
|
Denmark
Offline
God Member
Karma: 19
Posts: 683
|
 |
« Reply #12 on: February 06, 2013, 08:27:47 am » |
That was something like that I was thinking, although I think that the below test Sketch is more likely what you want. You need to clear the Name array in case of wrong number of entrys, and end the Array with the null terminator. There might be some errors in the sketch, but it explains better than words what I meant. char Name[9]; // My Data array char inChar; // Where to store the character read byte index = 0; // Index into array; where to store the character
void setup(){
Serial.begin(9600);
}
void loop(){ if (Serial.available()>0){ inChar = Serial.read(); Name[index] = inChar; // Store it if (index>8){ Serial.println("Invalid Input, please enter 3 to 8 characters"); } if (int(Name[index])==13){ //If Carriage return has been reached Serial.println(Name); index=0; }
index++; // Increment where to write next }
}
|
|
|
|
« Last Edit: February 06, 2013, 08:29:52 am by Erni »
|
Logged
|
|
|
|
|
Netherlands
Offline
Newbie
Karma: 1
Posts: 36
|
 |
« Reply #13 on: February 06, 2013, 08:49:21 am » |
The Stream class has a readBytesUntil( ... ) function; have a look at it.
kind regards,
Jos
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #14 on: February 06, 2013, 08:57:02 am » |
The Stream class has a readBytesUntil( ... ) function; have a look at it.
EUREKA!!! jajaja thanks!
|
|
|
|
|
Logged
|
|
|
|
|
|