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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 /*
00003     Author Mariwan Jalal
00004     This function will take a string from the terminal and save it in a buffer.
00005     The string is editable during the time user input the string.
00006     This will be useful if any one want to have an interactive interface for an application.
00007     USE IT AT YOUR OWN RESPONSIBILITY .. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
00008     Free to use.
00009 
00010 */
00011 
00012 #define MAX_LENGTH_OF_THE_STRING   256                    //Change this line to get longer string.
00013 #define   BAUD_RATE_              9600                    // Serial Terminal baudrate
00014 Serial pc(USBTX, USBRX);
00015 
00016 void GetStringFromSerial(char (& stringBuf) [MAX_LENGTH_OF_THE_STRING])
00017 {
00018     pc.printf("Echoes back to the screen anything you type\n");
00019     int i=0;
00020     while(i<MAX_LENGTH_OF_THE_STRING) {
00021         stringBuf[i]=pc.getc();
00022         if(stringBuf[i]=='\n') {
00023             stringBuf[i]='\0';
00024             if (i>0) {
00025                 i--;
00026                 if(stringBuf[i]=='\r') {
00027                     stringBuf[i]='\0';
00028                 }
00029                 return;
00030             }
00031         }
00032         if(stringBuf[i]=='\b') {
00033             pc.puts("\b \b");
00034             stringBuf[i]='\0';
00035             if (i>0) {
00036                 i--;
00037             }
00038             stringBuf[i]='\0';
00039 
00040         } else {
00041             pc.putc(stringBuf[i]);
00042             i++;
00043         }
00044 
00045     }
00046 }
00047 
00048 int main()
00049 {
00050     pc.baud(BAUD_RATE_);   // DEFAULT IS 9600, THIS IS JUST FOR NOTIFICATION FOR THE READER.
00051     char g[MAX_LENGTH_OF_THE_STRING];
00052     memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
00053     while (1) {
00054         memset (g,'\0',MAX_LENGTH_OF_THE_STRING);
00055         GetStringFromSerial(g);
00056         pc.printf("\n");
00057         pc.printf(g);
00058         pc.printf("\n");
00059     }
00060 }