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 Jun 07 06:04:30 2017 +0000
Revision:
0:97014197b0d4
Child:
1:e71563467f24
This function will take a string from the terminal and save it in a buffer. The string is editable during the time user input the string. This will be useful if any one want to have an interactive interface for an application.

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 0:97014197b0d4 7 USE ON YOUR RESPONSIBILITY .. NO WARANTY .. FREE SOFTWARE
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 0:97014197b0d4 15 void GetStringFromSerial(char (& stringBuf) [256])
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 0:97014197b0d4 32 i--;
mjm2016 0:97014197b0d4 33 stringBuf[i]='\0';
mjm2016 0:97014197b0d4 34 i--;
mjm2016 0:97014197b0d4 35 } else {
mjm2016 0:97014197b0d4 36 pc.putc(stringBuf[i]);
mjm2016 0:97014197b0d4 37 }
mjm2016 0:97014197b0d4 38 i++;
mjm2016 0:97014197b0d4 39 }
mjm2016 0:97014197b0d4 40 }
mjm2016 0:97014197b0d4 41 int main()
mjm2016 0:97014197b0d4 42 {
mjm2016 0:97014197b0d4 43 pc.baud(BAUD_RATE_); // DEFAULT IS 9600, THIS IS JUST FOR NOTIFICATION FOR THE READER.
mjm2016 0:97014197b0d4 44 char g[MAX_LENGTH_OF_THE_STRING];
mjm2016 0:97014197b0d4 45 memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
mjm2016 0:97014197b0d4 46 while (1) {
mjm2016 0:97014197b0d4 47 memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
mjm2016 0:97014197b0d4 48 GetStringFromSerial(g);
mjm2016 0:97014197b0d4 49 pc.printf("\n");
mjm2016 0:97014197b0d4 50 pc.printf(g);
mjm2016 0:97014197b0d4 51 pc.printf("\n");
mjm2016 0:97014197b0d4 52 }
mjm2016 0:97014197b0d4 53 }