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:45:34 2017 +0000
Revision:
3:12840abcee40
Parent:
2:880559109781
Child:
4:183056edc0f4
i-- must be protected from being a negative value. 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 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 0:97014197b0d4 36 stringBuf[i]='\0';
mjm2016 2:880559109781 37 if (i>0){
mjm2016 0:97014197b0d4 38 i--;
mjm2016 2:880559109781 39 }
mjm2016 2:880559109781 40
mjm2016 0:97014197b0d4 41 } else {
mjm2016 0:97014197b0d4 42 pc.putc(stringBuf[i]);
mjm2016 2:880559109781 43 i++;
mjm2016 0:97014197b0d4 44 }
mjm2016 2:880559109781 45
mjm2016 0:97014197b0d4 46 }
mjm2016 0:97014197b0d4 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 }