I will be sending strings ( ex: "CODE;25") from VB.NET to arduino and i need to split that string in half so the arduino serial monitor will return something like this:
h1- CODE
h2- 25
I've done some research but im still a bit confused, im new to arduino and this is a little overwhelming to me, but ive managed to split defined strings/chars (ex ; char sz[] = "CODE;25", then i can use the semicolon as the delimiter and split the words, but im a bit lost on how to do that with the incoming serial strings.
You can read the string into a char array as a buffer and when you've read the whole string, use strtok to split it at the semicolon. How do you know when you've read the whole string though - does your VB code send a newline or other end of string marker?
Since you're writing the VB code (I assume), consider making your packets look like this:
<CODE;25>
Then you've got a start of packet marker and one for the end. Then you can parse the data as you receive it:
throw chars away until you see '<'
read chars into h1 array until you see ';'
read chars into h2 array until you see '>'
Don't forget to null terminate the arrays and to check that you're not overflowing h1 or h2.
If more than one set of codes is coming in as a single stream, wildbill's solution is a good idea with the packet markers. If only one set comes in at a time, then you can use strchr() to partition the incoming string:
void setup() {
// put your setup code here, to run once:
char incomingMsg[20]; // size as needed
char codeBuff[10];
char number[5];
char *index;
int charsRead;
Serial.begin(115200); // Make sure yours matches
while (true) {
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', incomingMsg, sizeof(incomingMsg) - 1);
incomingMsg[charsRead] = '\0'; // Make it a string
Serial. print(" incomingMsg = "); // Debug statement
Serial.println(incomingMsg);
index = strchr(incomingMsg, ';');
*index = '\0'; //Make a string
strcpy(codeBuff, incomingMsg);
strcpy(number, index + 1);
Serial.print("Code = "); // Debug statements
Serial.print(codeBuff);
Serial. print(" number = ");
Serial.println(number);
break;
}
}
}
void loop() {
}
If you type in "code;25", the codeBuff should equal code and number should equal 25
wildbill:
You can read the string into a char array as a buffer and when you've read the whole string, use strtok to split it at the semicolon. How do you know when you've read the whole string though - does your VB code send a newline or other end of string marker?
Since you're writing the VB code (I assume), consider making your packets look like this:
<CODE;25>
Then you've got a start of packet marker and one for the end. Then you can parse the data as you receive it:
throw chars away until you see '<'
read chars into h1 array until you see ';'
read chars into h2 array until you see '>'
Don't forget to null terminate the arrays and to check that you're not overflowing h1 or h2.
To answer your question, yes i send newline to know that i´ve read the entire string! and yes i'm writing my own code (vb) to control a fingerprint sensor! and thank you so much for the tip! I managed to split the string by sending it into a char array then using strktok to split at the semicolon, its doing exactly what i wanted !
I have to do this preferably without using markers as start and end of string but i won't throw this idea away.
Now all i need is to send each half CODE and 25 to the respective h1 and h2, im going to read the " Serial Input Basics" before asking any more questions, you have been a great help!
econjack:
If more than one set of codes is coming in as a single stream, wildbill's solution is a good idea with the packet markers. If only one set comes in at a time, then you can use strchr() to partition the incoming string:
void setup() {
// put your setup code here, to run once:
char incomingMsg[20]; // size as needed
char codeBuff[10];
char number[5];
char *index;
int charsRead;
Serial.begin(115200); // Make sure yours matches
while (true) {
if (Serial.available() > 0) {
charsRead = Serial.readBytesUntil('\n', incomingMsg, sizeof(incomingMsg) - 1);
incomingMsg[charsRead] = '\0'; // Make it a string