Go Down

Topic: "Input" function to read serial keyboard entry from PC emulation terminal (Read 7452 times) previous topic - next topic

sbfrance

Hi, this is  a function that read serial data keyboard entry from a terminal or PC emulation terminal (e.g. HyperTerminal).
The function work like Basic langage input, you can put a string question and you get the keyboard entry in return in a string variable. It wait until you enter Return or Enter on your terminal and echo each character you type in. Works perfect with Hyper Terminal and others and of course with Arduino development software built in serial monitor.
2 alternatives routines, one using a global variable to pass the entry characters and the other the function return the string.
Key words : Serial, Serial.available, Serial.read, Serial.println, Serial.print, trim, terminal, RS232...

1st alternative :
*************
// function input : return the input character(s) in a string global variable :  input("message :");

String inData;                    // Global var buffer contain data from serial input function
boolean runOnce = true;
   
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (runOnce == true) {
  Serial.println("input function test - v1.0");  // This part of code run once first loop in order to output title on terminal
  Serial.println();
  runOnce = false;
  }
 
  input("test : ");               // Wait for terminal entry charater(s) followed by Enter key
  Serial.print("Received: ");
  Serial.println(inData);         // Output the reveived string on terminal just for demonstration
}

void input(String message) {      // Serial input function routine. Declare inData global String, usage input("message string");
  char received;                  // Each character received
  inData = "";                    // Clear recieved buffer 
  Serial.print(message);

  while (received != '\n') {      // When new line character is received (\n = LF, \r = CR)
    if (Serial.available() > 0)   // When character in serial buffer read it
    {
      received = Serial.read(); 
      Serial.print(received);     // Echo each received character, terminal dont need local echo
      inData += received;         // Add received character to inData buffer
    }
  }
  inData.trim();                  // Eliminate \n, \r, blank and other not "printable"
}

Second alternative :
******************
// Same function but return the input string directly :  entry = input("message :");   // with entry as a String variable

String entry;                 // String that contain the entered character(s) from serial terminal keyboard
boolean runOnce = true;
   
void setup() {
  Serial.begin(9600);
}

void loop() {
  if (runOnce == true) {
  Serial.println("input function test - v1.0");  // This part of code run once first loop in order to output title on terminal
  Serial.println();
  runOnce = false;
  }
 
  entry = input("test : ");   // Wait for terminal keyboard entry charater(s) followed by Enter key
  Serial.print("Received: ");
  Serial.println(entry);      // Output the reveived string on terminal just for demonstration
}

String input(String message) {    // Serial input function routine. Declare inData global String, usage input("message string");
  String inData;                  // buffer contain data from serial input function
  char received;                  // Each character received
  inData = "";                    // Clear recieved buffer 
  Serial.print(message);

  while (received != '\n') {      // When new line character is received (\n = LF, \r = CR)
    if (Serial.available() > 0)   // When character in serial buffer read it
    {
      received = Serial.read(); 
      Serial.print(received);     // Echo each received character, terminal dont need local echo
      inData += received;         // Add received character to inData
    }
  }
  inData.trim();                  // Eliminate \n, \r, blank and other not "printable"
  return inData;                  // Return the string character(s)
}


Go Up