Interactive serial interface for sending commands to an embedded system using serial communications. This will be helpful if you want to send commands to serial port and be able to correct the wrong text entered at the terminal
main.cpp@4:183056edc0f4, 2017-09-20 (annotated)
- Committer:
- mjm2016
- Date:
- Wed Sep 20 06:18:04 2017 +0000
- Revision:
- 4:183056edc0f4
- Parent:
- 3:12840abcee40
- Child:
- 5:bfc6c756c8e9
Bug fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjm2016 | 0:97014197b0d4 | 1 | #include "mbed.h" |
mjm2016 | 0:97014197b0d4 | 2 | /* |
mjm2016 | 0:97014197b0d4 | 3 | COPYWRITE Mariwan Jalal |
mjm2016 | 0:97014197b0d4 | 4 | This function will take a string from the terminal and save it in a buffer. |
mjm2016 | 0:97014197b0d4 | 5 | The string is editable during the time user input the string. |
mjm2016 | 0:97014197b0d4 | 6 | This will be useful if any one want to have an interactive interface for an application. |
mjm2016 | 2:880559109781 | 7 | USE IT AT YOUR OWN RESPONSIBILITY .. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND |
mjm2016 | 0:97014197b0d4 | 8 | |
mjm2016 | 0:97014197b0d4 | 9 | */ |
mjm2016 | 0:97014197b0d4 | 10 | |
mjm2016 | 0:97014197b0d4 | 11 | #define MAX_LENGTH_OF_THE_STRING 256 //Change this line to get longer string. |
mjm2016 | 0:97014197b0d4 | 12 | #define BAUD_RATE_ 9600 // Serial Terminal baudrate |
mjm2016 | 0:97014197b0d4 | 13 | Serial pc(USBTX, USBRX); |
mjm2016 | 0:97014197b0d4 | 14 | |
mjm2016 | 1:e71563467f24 | 15 | void GetStringFromSerial(char (& stringBuf) [MAX_LENGTH_OF_THE_STRING]) |
mjm2016 | 0:97014197b0d4 | 16 | { |
mjm2016 | 0:97014197b0d4 | 17 | pc.printf("Echoes back to the screen anything you type\n"); |
mjm2016 | 0:97014197b0d4 | 18 | int i=0; |
mjm2016 | 0:97014197b0d4 | 19 | while(i<MAX_LENGTH_OF_THE_STRING) { |
mjm2016 | 0:97014197b0d4 | 20 | stringBuf[i]=pc.getc(); |
mjm2016 | 0:97014197b0d4 | 21 | if(stringBuf[i]=='\n') { |
mjm2016 | 0:97014197b0d4 | 22 | stringBuf[i]='\0'; |
mjm2016 | 3:12840abcee40 | 23 | if (i>0){ |
mjm2016 | 3:12840abcee40 | 24 | i--; |
mjm2016 | 0:97014197b0d4 | 25 | if(stringBuf[i]=='\r') { |
mjm2016 | 0:97014197b0d4 | 26 | stringBuf[i]='\0'; |
mjm2016 | 0:97014197b0d4 | 27 | } |
mjm2016 | 0:97014197b0d4 | 28 | return; |
mjm2016 | 0:97014197b0d4 | 29 | } |
mjm2016 | 0:97014197b0d4 | 30 | if(stringBuf[i]=='\b') { |
mjm2016 | 0:97014197b0d4 | 31 | pc.puts("\b \b"); |
mjm2016 | 0:97014197b0d4 | 32 | stringBuf[i]='\0'; |
mjm2016 | 2:880559109781 | 33 | if (i>0){ |
mjm2016 | 0:97014197b0d4 | 34 | i--; |
mjm2016 | 2:880559109781 | 35 | } |
mjm2016 | 4:183056edc0f4 | 36 | stringBuf[i]='\0'; |
mjm2016 | 0:97014197b0d4 | 37 | } else { |
mjm2016 | 0:97014197b0d4 | 38 | pc.putc(stringBuf[i]); |
mjm2016 | 2:880559109781 | 39 | i++; |
mjm2016 | 0:97014197b0d4 | 40 | } |
mjm2016 | 2:880559109781 | 41 | |
mjm2016 | 0:97014197b0d4 | 42 | } |
mjm2016 | 0:97014197b0d4 | 43 | } |
mjm2016 | 0:97014197b0d4 | 44 | int main() |
mjm2016 | 0:97014197b0d4 | 45 | { |
mjm2016 | 0:97014197b0d4 | 46 | pc.baud(BAUD_RATE_); // DEFAULT IS 9600, THIS IS JUST FOR NOTIFICATION FOR THE READER. |
mjm2016 | 0:97014197b0d4 | 47 | char g[MAX_LENGTH_OF_THE_STRING]; |
mjm2016 | 0:97014197b0d4 | 48 | memset (g,'\0',MAX_LENGTH_OF_THE_STRING); |
mjm2016 | 0:97014197b0d4 | 49 | while (1) { |
mjm2016 | 0:97014197b0d4 | 50 | memset (g,'\0',MAX_LENGTH_OF_THE_STRING); |
mjm2016 | 0:97014197b0d4 | 51 | GetStringFromSerial(g); |
mjm2016 | 0:97014197b0d4 | 52 | pc.printf("\n"); |
mjm2016 | 0:97014197b0d4 | 53 | pc.printf(g); |
mjm2016 | 0:97014197b0d4 | 54 | pc.printf("\n"); |
mjm2016 | 0:97014197b0d4 | 55 | } |
mjm2016 | 0:97014197b0d4 | 56 | } |