Advanced Serial input?

I couldn't help notice that no one has coded a advanced serial input library yet? With current available code I'm finding it very hard to read strings of commands entered to the arduino via serial.

Ive made a terminal style input in asm before on the HCS12 how hard would it be on the Arduino? I want to be able to communicate via serial different string commands for example If you typed flush_Ser the serial would be flushed Set_midiCon1 7 would set midi controller 1 to act as a main volume control

anyone know how to use the serial interrupts on the arduino? im looking for a good serial tutorial but cant find anything decent?

Study the code in hardware\cores\arduino\hardwareserial.h and hardwareserial.cpp They use the interrupts for serial receiving.

Here is what I have working so far, I have the flu so I might have made some mistakes

//Advanced Serial Input By Moogle
//Max input length is 10, so "SetA 110000"
#define LEDpin 13
boolean ledon = false;
#define DEBUG true
// Variables: 
char incomingByte[10] = "test";      // for incoming serial data 
boolean printed = false;

void setup() 
{
  Serial.begin(9600);
  Serial.flush(); // for good luck ?
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin, ledon);
  invertLED();
  if(DEBUG)
  {
    Serial.println("Advance Serial Input Debug Enabled");
  }
}

void loop() 
{
  getSerial();
  void invertLED();
}

void getSerial()
{
  if( Serial.available() > 0) {// was something typed?
    while (Serial.available() > 0) {
      //there is something in the buffer
      for( int i =0; i <10; i++){
        //only loop 10 times dont over flow array
        // read the incoming byte:
       incomingByte[i] = Serial.read();
       invertLED();
      }
    }
    DEBUGPrint();//Print the info typed
  }
}
void invertLED() {  
  //Blink the led for visual reference
  digitalWrite(LEDpin,ledon); 
  ledon =!ledon; //Invert current Led state
  delay(100); //Delay to give buffer a chance
}
void DEBUGPrint()
{
  if( DEBUG) {//Will only print if the debug is enabled
      Serial.print("I received: ");
      for( int i =0; i <10; i++){
       //only loop 10 times dont try to print outside the array
        if(incomingByte[i]!=-1){
        //sometimes not all 10 characters are used 
          Serial.print(char(incomingByte[i]));
        }
        else{
        //dont print null kill the loop
          i=10;
        }  
      }
      Serial.println();
    }
}

will read a word from the user.
TODO:
process the words
split words from numbers; example"Speed= 100"
convert to a re-usable library

ok i started working on the library and ran into a problem
In function AdvancedSerial': AdvancedSerial.cpp:14: undefined reference to AdvancedSerial::invertLED()’
o: In function loop': Temporary_8586_9130.cpp:14: undefined reference to AdvancedSerial::getSerial()’
Couldn’t determine program size: \serialtest.hex’: No such file

I cant figure out what i did wrong??

the AdvancedSerial.h

#ifndef AdvancedSerial_h
#define AdvancedSerial_h
#include "WProgram.h"
//Advanced Serial Input By Moogle

class AdvancedSerial
{
  public:
   AdvancedSerial(boolean Debug, int Speed);
   void getSerial();
   void DEBUGPrint();
   char* GetString();
  private:
  char incomingByte[];
  byte StringSize;
  boolean ledon;
  boolean DEBUG;
  int _speed;
  void invertLED();
};


#endif

the AdvancedSerial.cpp

#include "WConstants.h"

//Advanced Serial Input By Moogle
#include "AdvancedSerial.h"
#define LEDpin 13

AdvancedSerial::AdvancedSerial(boolean Debug, int Speed)
{
  _DEBUG=Debug;
  _speed = Speed;
  Serial.begin(_speed);
  Serial.flush(); // for good luck ?
  pinMode(LEDpin, OUTPUT);
  digitalWrite(LEDpin, ledon);
  StringSize = 10;
  ledon = false;
  incomingSTR[] = "test";
  invertLED();
  if(_DEBUG)
  {
    Serial.println("Advance Serial Input Debug Enabled");
  }
}
char* GetString()
{
  return incomingSTR;
}

void invertLED() {
  //Blink the led for visual reference
  digitalWrite(LEDpin,ledon);
  ledon =!ledon; //Invert current Led state
  delay(100); //Delay to give buffer a chance
}

void getSerial()
{
  if( Serial.available() > 0) {// was something typed?
    while (Serial.available() > 0) {
      //there is something in the buffer
      for( int i =0; i <StringSize; i++){
        //only loop 10 times dont over flow array
        // read the incoming byte:
       incomingSTR[i] = Serial.read();
       invertLED();
      }
    }
    DEBUGPrint();//Print the info typed
  }
}


void DEBUGPrint()
{
  if( _DEBUG) {//Will only print if the debug is enabled
      Serial.print("I received: ");
      for( int i =0; i <StringSize; i++){
       //only loop 10 times dont try to print outside the array
        if(incomingSTR[i]!=-1){
        //sometimes not all 10 characters are used
          Serial.print(char(incomingSTR[i]));
        }
        else{
        //dont print null kill the loop
          i=StringSize;
        }
      }
      Serial.println();
    }
}

any ideas?

In AdvancedSerial.cpp change...

void invertLED() {

...to...

void AdvancedSerial::invertLED() {

Repeat for the remaining member functions.

Good luck, Brian

O.o!!!

I ended up with 32 pages of errors then??

what did I do wrong? Can someone Guide me?

If you post your .h and .cpp here: www.arduino.pastebin.com I'll sure have a look. :)

Remember to post links ;)

CPP file http://www.arduino.pastebin.com/f4741e33 .h file http://www.arduino.pastebin.com/f4b75f402

Working sketch I'm trying to convert to a library http://www.arduino.pastebin.com/fac58442

TY in advance :)

no help yet?

http://www.alexanderbrevig.com/hosting/arduino/libraries/AdvancedSerial.zip

Compiles and works. At least, it does what I think your pastebin-code tried to do. :)

Let me know when you've given it a try.

[TESTED ON IDE 0015]

Works great!! tyvm :slight_smile:

now to expand functionality and usefulness

Im probably making a very noob error here, who can spot it? (I keep thinking vb.net X_x not c++)

#include <AdvancedSerial.h>

AdvancedSerial advser = AdvancedSerial();
char* recieved;
char* lastcommand;
void setup() {
  advser.begin(false,9600);
  Serial.println("AlphaBeta is King");
  Serial.println(advser.getString());
}

void loop(){
  advser.getSerial();
  recieved = advser.getString();
  if(recieved != lastcommand)
  {
    if(recieved == "error?")
    {
     Serial.println("there was not a error!"); 
     }
  lastcommand= recieved;
  }
  
}

I don't know how I missed this thread, but, alas I did.

I'm on the warpath to create a more well-rounded serial library. I've posted a thread http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245549766 in which I describe creating an new underlying class (transparent to end users) which simply enhances any and all current serial classes.

With the new subclass, you can do things like:

mynumber = Serial.getnumber(HEX)

which gets an ASCII string of hexadecimal characters, and converts it into a number (stored into mynumber).

or

Serial.getstring(mystring, 40);

or

Serial.getstring(mystring, 40, '\n')

which gets a string (into mystring), either 40 characters long (no more, no less), or in the second case, gets a string up to a terminator (in this case a new line).

You can download the libraries from here: http://mediafire.com/Arduino

Simply put the files (from Libraries/SerialComm/) into the arduino/hardware/cores/arduino folder/directory. This will overwrite the existing HardwareSerial.h/.cpp - so if you want, you should create a backup of the files.

I'll be adding an updated version of NewSoftSerial as well (it just requires changing the inheritance to SerialComm, and adding a new method called "peek").

I want to add a printf (scaled down from the existing one in stdio), but need to gauge the interest in it. Feedback welcome.

b

nice work!

are you going to fix it so we can put multiple variables on one line
example:
int apples = 5;
int sold = 12;
Serial.Println(“I have " << apples << " in my inventory, and sold " << sold << " apples today.”);