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@5:bfc6c756c8e9, 2019-09-02 (annotated)
- Committer:
- mjm2016
- Date:
- Mon Sep 02 12:28:53 2019 +0000
- Revision:
- 5:bfc6c756c8e9
- Parent:
- 4:183056edc0f4
Bug fixed
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 | 5:bfc6c756c8e9 | 3 | Author Mariwan Jalal |
mjm2016 | 5:bfc6c756c8e9 | 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 | 5:bfc6c756c8e9 | 8 | Free to use. |
mjm2016 | 0:97014197b0d4 | 9 | |
mjm2016 | 0:97014197b0d4 | 10 | */ |
mjm2016 | 0:97014197b0d4 | 11 | |
mjm2016 | 0:97014197b0d4 | 12 | #define MAX_LENGTH_OF_THE_STRING 256 //Change this line to get longer string. |
mjm2016 | 0:97014197b0d4 | 13 | #define BAUD_RATE_ 9600 // Serial Terminal baudrate |
mjm2016 | 0:97014197b0d4 | 14 | Serial pc(USBTX, USBRX); |
mjm2016 | 0:97014197b0d4 | 15 | |
mjm2016 | 1:e71563467f24 | 16 | void GetStringFromSerial(char (& stringBuf) [MAX_LENGTH_OF_THE_STRING]) |
mjm2016 | 0:97014197b0d4 | 17 | { |
mjm2016 | 0:97014197b0d4 | 18 | pc.printf("Echoes back to the screen anything you type\n"); |
mjm2016 | 0:97014197b0d4 | 19 | int i=0; |
mjm2016 | 0:97014197b0d4 | 20 | while(i<MAX_LENGTH_OF_THE_STRING) { |
mjm2016 | 0:97014197b0d4 | 21 | stringBuf[i]=pc.getc(); |
mjm2016 | 0:97014197b0d4 | 22 | if(stringBuf[i]=='\n') { |
mjm2016 | 0:97014197b0d4 | 23 | stringBuf[i]='\0'; |
mjm2016 | 5:bfc6c756c8e9 | 24 | if (i>0) { |
mjm2016 | 5:bfc6c756c8e9 | 25 | i--; |
mjm2016 | 5:bfc6c756c8e9 | 26 | if(stringBuf[i]=='\r') { |
mjm2016 | 5:bfc6c756c8e9 | 27 | stringBuf[i]='\0'; |
mjm2016 | 5:bfc6c756c8e9 | 28 | } |
mjm2016 | 5:bfc6c756c8e9 | 29 | return; |
mjm2016 | 0:97014197b0d4 | 30 | } |
mjm2016 | 0:97014197b0d4 | 31 | } |
mjm2016 | 0:97014197b0d4 | 32 | if(stringBuf[i]=='\b') { |
mjm2016 | 0:97014197b0d4 | 33 | pc.puts("\b \b"); |
mjm2016 | 0:97014197b0d4 | 34 | stringBuf[i]='\0'; |
mjm2016 | 5:bfc6c756c8e9 | 35 | if (i>0) { |
mjm2016 | 5:bfc6c756c8e9 | 36 | i--; |
mjm2016 | 2:880559109781 | 37 | } |
mjm2016 | 5:bfc6c756c8e9 | 38 | stringBuf[i]='\0'; |
mjm2016 | 5:bfc6c756c8e9 | 39 | |
mjm2016 | 0:97014197b0d4 | 40 | } else { |
mjm2016 | 0:97014197b0d4 | 41 | pc.putc(stringBuf[i]); |
mjm2016 | 2:880559109781 | 42 | i++; |
mjm2016 | 0:97014197b0d4 | 43 | } |
mjm2016 | 5:bfc6c756c8e9 | 44 | |
mjm2016 | 0:97014197b0d4 | 45 | } |
mjm2016 | 0:97014197b0d4 | 46 | } |
mjm2016 | 5:bfc6c756c8e9 | 47 | |
mjm2016 | 0:97014197b0d4 | 48 | int main() |
mjm2016 | 0:97014197b0d4 | 49 | { |
mjm2016 | 0:97014197b0d4 | 50 | pc.baud(BAUD_RATE_); // DEFAULT IS 9600, THIS IS JUST FOR NOTIFICATION FOR THE READER. |
mjm2016 | 0:97014197b0d4 | 51 | char g[MAX_LENGTH_OF_THE_STRING]; |
mjm2016 | 0:97014197b0d4 | 52 | memset (g,'\0',MAX_LENGTH_OF_THE_STRING); |
mjm2016 | 0:97014197b0d4 | 53 | while (1) { |
mjm2016 | 0:97014197b0d4 | 54 | memset (g,'\0',MAX_LENGTH_OF_THE_STRING); |
mjm2016 | 0:97014197b0d4 | 55 | GetStringFromSerial(g); |
mjm2016 | 0:97014197b0d4 | 56 | pc.printf("\n"); |
mjm2016 | 0:97014197b0d4 | 57 | pc.printf(g); |
mjm2016 | 0:97014197b0d4 | 58 | pc.printf("\n"); |
mjm2016 | 0:97014197b0d4 | 59 | } |
mjm2016 | 0:97014197b0d4 | 60 | } |