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

main.cpp

Committer:
mjm2016
Date:
2017-06-07
Revision:
0:97014197b0d4
Child:
1:e71563467f24

File content as of revision 0:97014197b0d4:

#include "mbed.h"
/*
    COPYWRITE Mariwan Jalal
    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.
    USE ON YOUR RESPONSIBILITY .. NO WARANTY .. FREE SOFTWARE

*/

#define MAX_LENGTH_OF_THE_STRING   256                    //Change this line to get longer string.
#define   BAUD_RATE_              9600                    // Serial Terminal baudrate
Serial pc(USBTX, USBRX);

void GetStringFromSerial(char (& stringBuf) [256])
{
    pc.printf("Echoes back to the screen anything you type\n");
    int i=0;
    while(i<MAX_LENGTH_OF_THE_STRING) {
        stringBuf[i]=pc.getc();
        if(stringBuf[i]=='\n') {
            stringBuf[i]='\0';
            i--;
            if(stringBuf[i]=='\r') {
                        stringBuf[i]='\0';
            }
            return;
        }
        if(stringBuf[i]=='\b') {
            pc.puts("\b \b");
            stringBuf[i]='\0';
            i--;
            stringBuf[i]='\0';
            i--;
        } else {
            pc.putc(stringBuf[i]);
        }
        i++;
    }
}
int main()
{
    pc.baud(BAUD_RATE_);   // DEFAULT IS 9600, THIS IS JUST FOR NOTIFICATION FOR THE READER.
    char g[MAX_LENGTH_OF_THE_STRING];
    memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
    while (1) {
        memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
        GetStringFromSerial(g);
        pc.printf("\n");
        pc.printf(g);
        pc.printf("\n");
    }
}