n00b question on reading serial strings

Hi im fairly new to arduino programming, im trying to find a good code example on reading serial strings into an arduino so i can store the values in the EEPROM.

What im trying to achieve is an interactive way to use the serial port and setup a basic terminal session to store an ipaddress, subnet mask and gateway address in the eeprom for use by the ethernet shield.

below is the nearest post i've found so far but doesn't really help me.

TIA

themoog

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238944818

If you want to read and store strings, you need a place to store the data:

char ipAddr[16];
char ipMask[16];
char gateAddr[16]; // Or whatever size it needs to be (+1 for trailing NULL)

Then, you need some way to know where to put the next character in the array:

byte index;

Then, you need to read the data:

index = 0;
while(Serial.available() > 0 && index < 15)
{
   char aChar = Serial.read();
   ipAddr[index] = aChar;
   index++;
   ipAddr[index] = '\0'; // Keep the string NULL terminated
}

Repeat this block of code for IP mask and gate address.

Thanks!

Im guessing I can create three functions that I can select using a switch case… ?

Yes, you can. Switch based on some identifier that defines whether the data is an IP address, a mask, or a gateway.

Ive got this far

char ipAddr[16];
char ipMask[16];
char gateAddr[16]; // Or whatever size it needs to be (+1 for trailing NULL)



byte index;

void setup() {

  Serial.begin(9600);

}

void loop() {

  if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
    case 'a':    
      ipAddress();
      break;
    case 'b':    
      Serial.println(ipAddr);
      break;
    default:
      break;
    }

  }

}

void ipAddress() {

  index = 0;


  while(Serial.available() > 0 && index < 15 )
  {
    char aChar = Serial.read();
    ipAddr[index] = aChar;
    index++;
    ipAddr[index] = '\0'; // Keep the string NULL terminated 
  } 

  Serial.print(ipAddr);

}

Pressing a runs the ip.address code pressing b prints it out.

Some reason though printing ipAddr doesn’t work? Any thoughts

TIA

Neil

I also tried using the string library

still not having much luck

heres my code

#include <WString.h>                // include the String library

#define maxLength 3

String inString = String(maxLength);       // allocate a new String

void setup() {

  Serial.begin(9600);
  Serial.println("Ethernet Terminal");

}

void loop() {

  if (Serial.available() > 0) {
    int inByte = Serial.read();

    switch (inByte) {
    case 'a':    
      getIncomingChars();
      //ipAddress();
      break;
    case 'b':    
      Serial.print("Value ");
      Serial.println(inString);
      Serial.print("Value Length ");
      Serial.println (inString.length());
      break;
    default:
      break;
    }
  }
}



void ipAddress() {

  if(Serial.available() > 0) {
    // print the string
    Serial.println(inString);
  }
}




void getIncomingChars() {
  // read the incoming data as a char:
  Serial.println("a been pressed? ");



  char inChar = Serial.read();



  while (inString.length() < maxLength) {

    if(Serial.available() > 0) {
      // if you're not at the end of the string, append
      // the incoming character:
      //  if (inString.length() < maxLength) {


      inString.append(inChar);

      Serial.println("loop");
      Serial.println(inString);
      Serial.println(maxLength);
      Serial.println(inString.length());
    } 
    //   else {
    //empty the string by setting it equal to the inoming char:
    //inString = inChar;

  }
}

TIA

You can't print ipAddr in the ipAddress function or in loop?

I dont think ive explained it very well and most of the Serial.println commands are there for debugging

You original code works well, but as soon as I use a switch case to branch of too the function ipAddress() i need to wait unitl i recived 3bytes (for now) then return.

Im not very good with loops I guess this is where im getting stuck

Thanks

Neil

sorry not ipAddress() thats not being used at the moment

its getIncomingChars() im having problems with

Neil

Stripped of commented out code and blank lines, getIncomingChars() looks like this:

void getIncomingChars() {
  Serial.println("a been pressed? ");

  char inChar = Serial.read();

  while (inString.length() < maxLength) {
    if(Serial.available() > 0) {
      inString.append(inChar);

      Serial.println("loop");
      Serial.println(inString);
      Serial.println(maxLength);
      Serial.println(inString.length());
    }
  }
}

You read a character from the serial port, regardless of whether there is anything to read, or not. Then, while the string is not full, you append that character to the string.

Try this:

void getIncomingChars()
{
   while (Serial.available() > 0 && inString.length() < maxLength)
   {
      char inChar = Serial.read();
      inString.append(inChar);

      Serial.println("loop");
      Serial.println(inString);
      Serial.println(maxLength);
      Serial.println(inString.length());
   }
}

This code will determine that there is serial data to read, and that there is room to hold the data. If so, it will read a character, and add it to the string.

When there is no more data, or there is no more room for data, it returns.

Yeah I tried that but i don’t get anything from the loop.

Im wondering if when I use case ‘a’ to jump to the function, i have to worry about handling carriage returns and line feeds.

Im using the terminal in the arduino 017 IDE

Thanks again

Neil

The IDE does not send carriage returns or line feeds, so, no, you do no need to worry about handling them.

If you change what is sending the data, you might have to.