Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. int parseRF(char *input);
  2. void printStruct(struct data_main input);
  3. void removeChar(char *str, char garbage);
  4.  
  5. struct data_main my_data;
  6.  
  7. int main (void) {
  8.  
  9. char data1[] = "#-75,16,[382,503,2700],[178,132,10,2],[178,662,10,2],[585,109,10,0]";
  10.  
  11. char data2[] = "#-75,16,[382,503,2700],[178,132,10,2],[178,662,10,2]"; // missing g2
  12.  
  13. char data3[] = "#-75,16,382,503,2700],[178,132,10,2],[178,662,10,2],585109,10,0]"; // broken syntax
  14.  
  15. char data4[] = "NoRX";
  16.  
  17. if (parseRF(data1)){
  18. printf("Packet worked!\n");
  19. } else {
  20. printf("Packet read failed, data corrupted\n");
  21. }
  22.  
  23. printStruct(my_data);
  24.  
  25. return 0;
  26. }
  27.  
  28. int parseRF(char *input) {
  29. char *pt;
  30. struct data_main temp; // create a buffer struct
  31.  
  32. if (input[0] != '#') { // exit if we dont have a preceeding #
  33. return false;
  34. }
  35. else {
  36. removeChar(input,'#'); // remove leading # from token
  37. }
  38.  
  39. pt = strtok(input,","); // split up via ','
  40.  
  41.  
  42.  
  43. int i = 0;
  44. while (i < 17) {
  45. if (pt == NULL){
  46. break; // Is this ok? to leave when one broken val?
  47. }
  48.  
  49. if (i == 2 || i == 5 || i == 9 || i == 13) {
  50. if (strcmp(&pt[0],"[") != 1) { // exit if we dont have a preceeding [
  51. return false;
  52. } else {
  53. removeChar(pt,'[');
  54. }
  55. } else if (i == 4 || i == 8 || i == 12 || i == 16) {
  56. if ( strlen(pt) > 0 && pt[strlen(pt)-1] == ']' ) // remove trailing ], data is broken if there is none so return false
  57. {
  58. removeChar(pt,']');
  59. } else {
  60. return false;
  61. }
  62. }
  63.  
  64. __int16 tempInt = atoi(pt);
  65.  
  66. switch(i) {
  67.  
  68. case 0:
  69. temp.rssi = tempInt;
  70. break;
  71. case 1:
  72. temp.index = tempInt;
  73. break;
  74. case 2:
  75. temp.robot_xpos = tempInt;
  76. break;
  77. case 3:
  78. temp.robot_ypos = tempInt;
  79. break;
  80. case 4:
  81. temp.robot_orientation = tempInt;
  82. break;
  83. case 5:
  84. temp.g0_xpos = tempInt;
  85. break;
  86. case 6:
  87. temp.g0_ypos = tempInt;
  88. break;
  89. case 7:
  90. temp.g0_speed = tempInt;
  91. break;
  92. case 8:
  93. temp.g0_direction = tempInt;
  94. break;
  95. case 9:
  96. temp.g1_xpos = tempInt;
  97. break;
  98. case 10:
  99. temp.g1_ypos = tempInt;
  100. break;
  101. case 11:
  102. temp.g1_speed = tempInt;
  103. break;
  104. case 12:
  105. temp.g1_direction = tempInt;
  106. break;
  107. case 13:
  108. temp.g2_xpos = tempInt;
  109. break;
  110. case 14:
  111. temp.g2_ypos = tempInt;
  112. break;
  113. case 15:
  114. temp.g2_speed = tempInt;
  115. break;
  116. case 16:
  117. temp.g2_direction = tempInt;
  118. break;
  119. }
  120.  
  121. pt = strtok(NULL,","); // increment counter and token
  122. i++;
  123. }
  124. my_data = temp; // copy data to main array
  125. return true;
  126. }
  127.  
  128. // remove a character from a given string
  129. void removeChar(char *str, char garbage) {
  130. char *src, *dst;
  131. for (src = dst = str; *src != '\0'; src++) {
  132. *dst = *src;
  133. if (*dst != garbage) dst++;
  134. }
  135. *dst = '\0';
  136. }
  137.  
  138. void printStruct(struct data_main input) {
  139. printf("%d\n", input.rssi);
  140. printf("%d\n", input.index);
  141. printf("%d\n", input.robot_xpos);
  142. printf("%d\n", input.robot_ypos);
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement