Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int parseRF(char *input);
- void printStruct(struct data_main input);
- void removeChar(char *str, char garbage);
- struct data_main my_data;
- int main (void) {
- char data1[] = "#-75,16,[382,503,2700],[178,132,10,2],[178,662,10,2],[585,109,10,0]";
- char data2[] = "#-75,16,[382,503,2700],[178,132,10,2],[178,662,10,2]"; // missing g2
- char data3[] = "#-75,16,382,503,2700],[178,132,10,2],[178,662,10,2],585109,10,0]"; // broken syntax
- char data4[] = "NoRX";
- if (parseRF(data1)){
- printf("Packet worked!\n");
- } else {
- printf("Packet read failed, data corrupted\n");
- }
- printStruct(my_data);
- return 0;
- }
- int parseRF(char *input) {
- char *pt;
- struct data_main temp; // create a buffer struct
- if (input[0] != '#') { // exit if we dont have a preceeding #
- return false;
- }
- else {
- removeChar(input,'#'); // remove leading # from token
- }
- pt = strtok(input,","); // split up via ','
- int i = 0;
- while (i < 17) {
- if (pt == NULL){
- break; // Is this ok? to leave when one broken val?
- }
- if (i == 2 || i == 5 || i == 9 || i == 13) {
- if (strcmp(&pt[0],"[") != 1) { // exit if we dont have a preceeding [
- return false;
- } else {
- removeChar(pt,'[');
- }
- } else if (i == 4 || i == 8 || i == 12 || i == 16) {
- if ( strlen(pt) > 0 && pt[strlen(pt)-1] == ']' ) // remove trailing ], data is broken if there is none so return false
- {
- removeChar(pt,']');
- } else {
- return false;
- }
- }
- __int16 tempInt = atoi(pt);
- switch(i) {
- case 0:
- temp.rssi = tempInt;
- break;
- case 1:
- temp.index = tempInt;
- break;
- case 2:
- temp.robot_xpos = tempInt;
- break;
- case 3:
- temp.robot_ypos = tempInt;
- break;
- case 4:
- temp.robot_orientation = tempInt;
- break;
- case 5:
- temp.g0_xpos = tempInt;
- break;
- case 6:
- temp.g0_ypos = tempInt;
- break;
- case 7:
- temp.g0_speed = tempInt;
- break;
- case 8:
- temp.g0_direction = tempInt;
- break;
- case 9:
- temp.g1_xpos = tempInt;
- break;
- case 10:
- temp.g1_ypos = tempInt;
- break;
- case 11:
- temp.g1_speed = tempInt;
- break;
- case 12:
- temp.g1_direction = tempInt;
- break;
- case 13:
- temp.g2_xpos = tempInt;
- break;
- case 14:
- temp.g2_ypos = tempInt;
- break;
- case 15:
- temp.g2_speed = tempInt;
- break;
- case 16:
- temp.g2_direction = tempInt;
- break;
- }
- pt = strtok(NULL,","); // increment counter and token
- i++;
- }
- my_data = temp; // copy data to main array
- return true;
- }
- // remove a character from a given string
- void removeChar(char *str, char garbage) {
- char *src, *dst;
- for (src = dst = str; *src != '\0'; src++) {
- *dst = *src;
- if (*dst != garbage) dst++;
- }
- *dst = '\0';
- }
- void printStruct(struct data_main input) {
- printf("%d\n", input.rssi);
- printf("%d\n", input.index);
- printf("%d\n", input.robot_xpos);
- printf("%d\n", input.robot_ypos);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement