Reception of multiple characters over UART without interrupting the program. Uses 2 input buffers.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 3:e316c5e314e1
- Parent:
- 2:416bbbc23e82
--- a/main.cpp Mon Apr 20 21:41:02 2015 +0000 +++ b/main.cpp Tue May 12 09:31:21 2015 +0000 @@ -3,7 +3,7 @@ * @author Petr Dousa * @version V1.00 * @date 22-March-2015 -* @brief Every 1 second push to serial message and read from serial integer number +* @brief Every single second push to serial message and read from serial * Serial speed is set to 115200. ***********************************************************************************/ @@ -19,7 +19,7 @@ int serialGetBuffer(char * & data); /* Variables ---------------------------------------------------------------------*/ -// help variables for incoming data +// additional variables for incoming data char serial_buffer[buffer_size]; // buffer to save incoming data char serial_buffer2[buffer_size]; // second buffer to save incoming data int serial_buffer_where=0; // index array for buffer @@ -35,7 +35,7 @@ /******************************************************************************* * Function Name : serialRx. * Description : Incoming interruption fom serial. Fill two buffers with -* incoming data and prepare it to serialGetBuffer. +* incoming data and prepare it to serialGetBuffer. * Input : None. * Output : None. * Return : None. @@ -45,19 +45,19 @@ while(pc.readable()) { // read all data from serial char character=pc.getc(); // get a char form serial if(((int)character==10 || (int)character==13) && serial_end_line) { // search for end line /r or /n - serial_end_line=true; // end was find in previous char, so skip it + serial_end_line=true; // end was find in the previous charascter, skip continue; } else { serial_end_line=false; // clear serial_end_line flag } - if(serial_buffer_where!=buffer_fill && !(serial_buffer2_where != 0 && serial_buffer2_where!=buffer_fill)/* && ( serial_buffer_where!=0 || (serial_buffer2_where !=0 && serial_buffer_where==0 ))*/) { + if(serial_buffer_where!=buffer_fill && !(serial_buffer2_where != 0 && serial_buffer2_where!=buffer_fill)) { serial_buffer[serial_buffer_where++]=character; - if(serial_buffer_where==buffer_size) { // check if incoming data are smaller than buffe size + if(serial_buffer_where==buffer_size) { // check if incoming data are smaller than buffer size serial_buffer[buffer_size-1]='\0'; // posibility to lost data, if the incoming data are too big serial_buffer_where=buffer_fill; // set index array to indicate buffer fill continue; } - if(character==13 || character==10) { // if end of line (\r \n) is indicate, prepare the buffer to serialGetBuffer + if(character==13 || character==10) { // if end of line (\r \n) is indicated, prepare the buffer to serialGetBuffer serial_buffer[serial_buffer_where-1]='\0'; // set end of buffer with 0 serial_buffer_where=buffer_fill; // set index array to indicate buffer fill serial_end_line=true; // end of line was find, set serial_end_line flag @@ -74,7 +74,7 @@ serial_buffer2_where=buffer_fill; serial_end_line=true; } - } /*else { // buffer are fill and data are lost + } /*else { // the buffers are full and thedata are lost while(!pc.writeable()); pc.printf("Code is too slow!\n"); //pc.printf("I get: \"%c\", DEC: %d\n",character,(int)character); @@ -124,15 +124,15 @@ int j = 0; int k = 0; - //array for incomng data to user + //array for data that are coming to user char * prijata_data; - pc.baud(115200); // set serial speed to 115200 - pc.attach(&serialRx,Serial::RxIrq); // attach serialRx to incoming interruption + pc.baud(115200); // set serial speed to 115200 + pc.attach(&serialRx,Serial::RxIrq); // attach serialRx to reception interrupt pc.printf("Hello World !\n"); while(1) { - wait_ms(10); // wait 1 second + wait_ms(10); // wait 1 second if(serialGetBuffer(prijata_data)) { // get char array from serial sscanf (prijata_data,"%d",&j); // sscanf for searching integer pc.printf("From PC:%d.\n" ,j); // write to serial incoming integer @@ -141,7 +141,7 @@ k++; if (k>=100) { - pc.printf("This program runs since %d seconds.\n", i++); // write to serial some string + pc.printf("This program runs since %d seconds.\n", i++); // write some string to serial myled = !myled; // blink LED