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
Diff: main.cpp
- Revision:
- 0:97014197b0d4
- Child:
- 1:e71563467f24
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jun 07 06:04:30 2017 +0000 @@ -0,0 +1,53 @@ +#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"); + } +} \ No newline at end of file