Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue mbed
main.cpp
00001 //////////////////////////////////////// 00002 // Serial Packet - MbedJsonValue // 00003 // Arkadiraf@gmail.com - 15/06/2017 // 00004 //////////////////////////////////////// 00005 /* 00006 Construct packet from uart and parse with json format 00007 json:<PACKET>\r\n 00008 test with 00009 json:{"my_array": ["demo_string", 10], "my_boolean": true}\r\n 00010 */ 00011 00012 /* 00013 Board : Nucleo STM32F446RE 00014 */ 00015 00016 /* 00017 Pinout: 00018 PC - Serial 2 00019 PA_2 (Tx) --> STLINK 00020 PA_3 (Rx) --> STLINK 00021 */ 00022 00023 00024 /////////////// 00025 // Libraries // 00026 /////////////// 00027 #include "mbed.h" 00028 #include "MbedJSONValue.h" 00029 //#include <string> 00030 00031 //#include "BufferedSerial.h" // uart handler library 00032 /////////////// 00033 // #defines // 00034 /////////////// 00035 00036 #define DEBUG_MOD1 00037 #define MSG_BUFFER_SIZE 512 00038 #define HEADER_SIZE 5 00039 #define FOOTER_SIZE 2 00040 00041 ///////////// 00042 // Objects // 00043 ///////////// 00044 00045 // uart 00046 Serial pc(USBTX, USBRX); 00047 00048 // digital output 00049 DigitalOut led(PA_5); 00050 /////////////// 00051 // variables // 00052 /////////////// 00053 00054 // json buffer 00055 char json[MSG_BUFFER_SIZE]; 00056 00057 // packet variables 00058 struct packetMSG_struct { 00059 // recieve message variables 00060 uint8_t header[HEADER_SIZE]; 00061 uint8_t footer[FOOTER_SIZE]; 00062 uint8_t syncIndex; // sync index for header / footer 00063 uint8_t syncFlag; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer 00064 // buffer 00065 uint16_t bufferIndex; // buffer index 00066 uint8_t buffer[MSG_BUFFER_SIZE]; 00067 } ; 00068 packetMSG_struct packetMSG; 00069 /////////////// 00070 // Functions // 00071 /////////////// 00072 00073 // Serial Event function 00074 void rxCallback(void); 00075 00076 // initialize packet struct 00077 void initPacket(void); 00078 00079 // Packet Parser 00080 void parsePacket(void); 00081 //////////////////////// 00082 // Main Code Setup : // 00083 //////////////////////// 00084 int main() 00085 { 00086 // init packet: 00087 initPacket(); 00088 // init uart 00089 pc.baud(57600); 00090 // attach serial event interrupt 00091 pc.attach(&rxCallback, Serial::RxIrq); 00092 00093 #ifdef DEBUG_MOD1 00094 pc.printf("UART Test \r\n"); 00095 #endif 00096 /////////////////////// 00097 // Main Code Loop : // 00098 /////////////////////// 00099 while(1) { 00100 //sleep(); 00101 }// end main loop 00102 }// end main 00103 00104 00105 00106 /////////////// 00107 // Functions // 00108 /////////////// 00109 00110 // Serial Event function 00111 void rxCallback(void) 00112 { 00113 while (pc.readable()) { 00114 // read icoming 00115 led = !led; 00116 uint8_t in_byte = pc.getc(); 00117 #ifdef DEBUG_MOD1 00118 pc.putc(in_byte); 00119 #endif 00120 // detect start message , end message 00121 switch (packetMSG.syncFlag) { 00122 // waiting for header 00123 case 0: { 00124 if (packetMSG.header[packetMSG.syncIndex] == in_byte) { 00125 packetMSG.syncIndex++; 00126 if (packetMSG.syncIndex == HEADER_SIZE) { // finish header SYNC 00127 packetMSG.syncFlag = 1; // start collecting data, wait for footer 00128 packetMSG.bufferIndex = 0; 00129 packetMSG.syncIndex=0; 00130 } 00131 } else { // reinit sync 00132 packetMSG.syncIndex=0; 00133 } 00134 //pc.printf("case 0 , %d \r\n",packetMSG.syncIndex); 00135 break; 00136 } 00137 // waiting for footer 00138 case 1: { 00139 // add byte to buffer 00140 packetMSG.buffer[packetMSG.bufferIndex] = in_byte; 00141 packetMSG.bufferIndex++; 00142 if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow 00143 // reset buffer 00144 packetMSG.bufferIndex = 0; 00145 packetMSG.syncIndex = 0; 00146 packetMSG.syncFlag = 0; 00147 } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved 00148 packetMSG.syncIndex++; 00149 packetMSG.syncFlag=2; // move to verify footer 00150 } 00151 //pc.printf("case 2 , %d \r\n",packetMSG.syncIndex); 00152 break; 00153 } 00154 // verify footer 00155 case 2: { 00156 // add byte to buffer 00157 packetMSG.buffer[packetMSG.bufferIndex] = in_byte; 00158 packetMSG.bufferIndex++; 00159 if (packetMSG.bufferIndex >= MSG_BUFFER_SIZE) { // buffer overflow 00160 // reset buffer 00161 packetMSG.bufferIndex = 0; 00162 packetMSG.syncIndex = 0; 00163 packetMSG.syncFlag = 0; 00164 } else if (packetMSG.footer[packetMSG.syncIndex] == in_byte) { // footer char recieved 00165 packetMSG.syncIndex++; 00166 if (packetMSG.syncIndex == FOOTER_SIZE) { // finish footer SYNC 00167 packetMSG.syncFlag = 3; 00168 // copy packet to json buffer 00169 memcpy (&json, &packetMSG.buffer, packetMSG.bufferIndex); 00170 json[packetMSG.bufferIndex]=NULL; // end with NULL to indicate end of string 00171 // copy packet to json buffer with sprintf 00172 //sprintf(json, "%.*s", packetMSG.bufferIndex, packetMSG.buffer ); 00173 // send msg to parse. 00174 parsePacket(); 00175 // reset buffer 00176 packetMSG.bufferIndex = 0; 00177 packetMSG.syncIndex = 0; 00178 packetMSG.syncFlag = 0; 00179 } 00180 } else { // footer broke restart wait for footer 00181 packetMSG.syncFlag=1; 00182 // verify that it didnt broke on first footer char 00183 if (packetMSG.footer[0] == in_byte) { 00184 packetMSG.syncIndex=1; 00185 } else { 00186 packetMSG.syncIndex=0; 00187 } 00188 } 00189 break; 00190 } 00191 default: { 00192 pc.printf("Sonmething went wrong \r\n"); 00193 break; 00194 } 00195 } // end switch 00196 }// end uart readable 00197 } // end rxCallback 00198 00199 00200 // initialize packet struct 00201 void initPacket(void) 00202 { 00203 // init variables to default: 00204 packetMSG.header[0] = 'j'; 00205 packetMSG.header[1] = 's'; 00206 packetMSG.header[2] = 'o'; 00207 packetMSG.header[3] = 'n'; 00208 packetMSG.header[4] = ':'; 00209 00210 packetMSG.footer[0]= 13; // /r 00211 packetMSG.footer[1]= 10; // /n 00212 00213 packetMSG.syncIndex=0; // sync index for header / footer 00214 packetMSG.syncFlag=0; // 0 - waiting for header, 1 - waiting for footer, 2 - verify footer, 3 - finish footer send to parser, flash buffer 00215 packetMSG.bufferIndex=0; // buffer index 00216 } 00217 00218 // Packet Parser 00219 void parsePacket(void) 00220 { 00221 #ifdef DEBUG_MOD1 00222 // write buffer to screen 00223 //pc.printf("%d, %.*s", packetMSG.bufferIndex ,packetMSG.bufferIndex, packetMSG.buffer ); 00224 //pc.printf("%s", json); 00225 #endif 00226 00227 MbedJSONValue demo; 00228 00229 //const char * json = "{\"my_array\": [\"demo_string\", 10], \"my_boolean\": true}"; 00230 // json:{"my_array": ["demo_string", 10], "my_boolean": true} 00231 //parse the previous string and fill the object demo 00232 parse(demo, json); 00233 00234 // string my_str; 00235 // int my_int; 00236 // bool my_bool; 00237 // 00238 // my_str = demo["my_array"][0].get<std::string>(); 00239 // my_int = demo["my_array"][1].get<int>(); 00240 // my_bool = demo["my_boolean"].get<bool>(); 00241 // 00242 // printf("my_str: %s\r\n", my_str.c_str()); 00243 // printf("my_int: %d\r\n", my_int); 00244 // printf("my_bool: %s\r\n", my_bool ? "true" : "false"); 00245 00246 }
Generated on Thu Jul 14 2022 12:57:27 by
1.7.2