Hi there!
My question is related with receiving integers via serial port and store them in six different arrays using UNO. The integers can change between 0 and 180 (they are used to control angular positions of six servo motors). The six integer' arrays must save incoming integers which are transmit by VB6 application via USB serial port. Every array stores the angular positions of only one servo motor. The servo's positions can be varying between 10 (at least) and reach up to 30, so every of these 6 arrays must have up to 30 elements.
My problem is, that every time the number of the servo motors positions can be different. For instance, I can have 15 combinations of different positions, i.e. 6 different integers between 0 and 180 in the first element of the arrays, then next combination of 6 different integers in the second elements, and so on until the 15-th arrays element.
Using VB6 app, and some tutorials like this, and this, that I have found here, helped me to create the following code (My code is based on the second tutorial. The first is not applicable to me, because takes great amount of UNO's dynamic memory in my case):
int servo01SP[30], servo02SP[30], servo03SP[30], servo04SP[30], servo05SP[30], servo06SP[30]; // for storing positions/steps
int speedDelay = 10;
int index = 0;
int dataIn;
const byte numChars = 30;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
//============
if (m == 10) {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
strcpy(messageFromPC, strtokIndx); // copy it to messageFromPC
servo01SP[0] = messageFromPC;
for (int i = 1; i < 30; i++) {
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
// integerFromPC = atoi(strtokIndx); // convert this part to an integer
servo01SP[i] = atoi(strtokIndx);
// servo01SP[i] = integerFromPC;
delay (20);
}
// strtokIndx = strtok(NULL, ",");
// integerFromPC2 = atoi(strtokIndx); // convert this part to a float
// strtokIndx = strtok(NULL, ",");
// integerFromPC3 = atoi(strtokIndx); // convert this part to a float
}
void showParsedData() {
// Serial.print("String ");
// Serial.println(messageFromPC);
for (int i = 0; i < 30; i++) {
Serial.println(servo01SP[i]);
delay (20);
}
}
I can send via serial port strings, formatted like this "<95,95,-5,-5,-5,-5,-5,-5,79,79,79,79,79,99,99>" by VB6 app. But UNO return the string data like this:
10
9 <- ?!?
256 <-?!?
95 - OK
-5 - OK
-5 - OK
-5 - OK
-5 - OK
-5 - OK
-5 - OK
79 - OK
79 - OK
7 - OK
79 - OK
79 - OK
99 - OK
99 - OK
0
0
0
0
0
My questions are, related with Arduino code:
- How can I reliably parse the different incoming integers and store all them into given array?
- How can I distinguish the different incoming strings - which integer sequence in which of the six arrays must be stored?
Thank you in advance!