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

Dependencies:   mbed

Committer:
mjm2016
Date:
Wed Sep 20 05:38:55 2017 +0000
Revision:
2:880559109781
Parent:
1:e71563467f24
Child:
3:12840abcee40
Bug fix

Who changed what in which revision?

UserRevisionLine numberNew 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 0:97014197b0d4 23 i--;
mjm2016 0:97014197b0d4 24 if(stringBuf[i]=='\r') {
mjm2016 0:97014197b0d4 25 stringBuf[i]='\0';
mjm2016 0:97014197b0d4 26 }
mjm2016 0:97014197b0d4 27 return;
mjm2016 0:97014197b0d4 28 }
mjm2016 0:97014197b0d4 29 if(stringBuf[i]=='\b') {
mjm2016 0:97014197b0d4 30 pc.puts("\b \b");
mjm2016 0:97014197b0d4 31 stringBuf[i]='\0';
mjm2016 2:880559109781 32 if (i>0){
mjm2016 0:97014197b0d4 33 i--;
mjm2016 2:880559109781 34 }
mjm2016 0:97014197b0d4 35 stringBuf[i]='\0';
mjm2016 2:880559109781 36 if (i>0){
mjm2016 0:97014197b0d4 37 i--;
mjm2016 2:880559109781 38 }
mjm2016 2:880559109781 39
mjm2016 0:97014197b0d4 40 } else {
mjm2016 0:97014197b0d4 41 pc.putc(stringBuf[i]);
mjm2016 2:880559109781 42 i++;
mjm2016 0:97014197b0d4 43 }
mjm2016 2:880559109781 44
mjm2016 0:97014197b0d4 45 }
mjm2016 0:97014197b0d4 46 }
mjm2016 0:97014197b0d4 47 int main()
mjm2016 0:97014197b0d4 48 {
mjm2016 0:97014197b0d4 49 pc.baud(BAUD_RATE_); // DEFAULT IS 9600, THIS IS JUST FOR NOTIFICATION FOR THE READER.
mjm2016 0:97014197b0d4 50 char g[MAX_LENGTH_OF_THE_STRING];
mjm2016 0:97014197b0d4 51 memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
mjm2016 0:97014197b0d4 52 while (1) {
mjm2016 0:97014197b0d4 53 memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
mjm2016 0:97014197b0d4 54 GetStringFromSerial(g);
mjm2016 0:97014197b0d4 55 pc.printf("\n");
mjm2016 0:97014197b0d4 56 pc.printf(g);
mjm2016 0:97014197b0d4 57 pc.printf("\n");
mjm2016 0:97014197b0d4 58 }
mjm2016 0:97014197b0d4 59 }