serial commards : How can i change eprom via seria

im trying to make it possible so i can change my program settings with a dumb terminal.

What i want to do is simply have say 20 eprom address and i want to be able set them through serial port.

Ideally i want something like this : "set 3,44" to write 44 to location 3'

ive worked out to write to eprom ok , but im stuck when looking for the word "set" and and spliting the string into an array ...

Any help or ideas would be such a help .

cheers
luke

There are several steps involved. The first is collecting all the serial data into a NULL-terminated character array.

The next step is parsing the string, and analyzing the tokens.

The third step is writing the resulting values into EEPROM.

The final step is reading the data from EEPROM, and using it.

Which step are you stuck on?

The easiest way to do this would be to use Bitlash (http://bitlash.net/wiki/start). It accepts commands on the serial port, including commands to read from/write to EEPROM.

Regards,

-Mike

thanks for your replies , Bitlash looks amazing and works well but it seems too much for what i want , i only need to edit a few eprom locations and bitlash seems like a mini operating system.

where im stuck is ....

The next step is parsing the string, and analyzing the tokens,

ok so typed in terminal .... "set 1=34" ect What i need help with is to get ADR = 1 VAL = 33 and more importantly only act when the string has "set" in it.

?

any ideas ?

cheers again
luke

any ideas ?

Yes. The strtok function will allow you parse the string that you get. The first call to strtok has the string to parse as the first argument, and a list of delimiters. The function returns a pointer to a token, AND MODIFIES THE STRING.

Subsequent calls to strtok take NULL as the first argument. There are no more tokens when strtok returns NULL.

You can convert the token to an integer or float, if appropriate. You can compare the string (strcmp) to "set" or "get" or "Fred" or whatever, to determine what to do.

i have found this example

#include <string.h>

char sz[] = "Here; is some; sample;100;data;1.414;1020";
void setup()
{
 char *p = sz;
 char *str;
 Serial.begin(9600);
 while ((str = strtok_r(p, ";", &p)) != NULL) // delimiter is the semicolon
   Serial.println(str);
}

Hey thanks for that , but i cant get working with the serial port data being read.

when i put serial read into strtok_r i get "char to int error" when compling.

Any ideas how i marry up the serial read to the slip string ?

cheers again for your help.

I pasted that code into a sketch, and added a loop function. It compiled, on Arduino 0017. What version are you using?

Um yeah Arduino 0017 ... This how i added the serial read to it but im sure its a simple error why its not right ?

#include <string.h>


void setup()
{
 
   Serial.begin(9600);
}



void loop() {
        
  if (Serial.available() >= 0) {

               
char *p =Serial.read();   // THIS IS THE ERROR 

 char *str;

 while ((str = strtok_r(p, ";", &p)) != NULL) // delimiter is the semicolon
   Serial.println(str);
     Serial.flush();
               
      }
}

char *p =Serial.read(); // THIS IS THE ERROR

Can you post your atempt if its much different ?

cheers paul

luke

char *p is a pointer. You need to allocate space for it to point to. You have not done that.

Arrays are treated as pointers, too. So, if you have

char inData[24];

you can pass inData to strtok. You can fill each position of the array with a value read from the serial port. Just make sure to put a NULL in the next position, too.

Ah i see well so i need to add each serial byte read into the array first , im going have say i am not a pro at this .

I think i found examples of this called serial buffer , i will have a look into this .

Im guessing it involves using a for loop and using Serial.available to count the array numbers ?

cheers again dude

I outlined the steps required in reply #1. From the response you provided, I assumed that you know how to get the data from the serial port as an array. Now, that appears not to be the case. It's easy, really.

int inPos = 0;
char inData[24]; // Adjust the size as needed

void loop()
{
   while(Serial.available() > 0)
   {
       char aChar = Serial.read();  // Read a character
       inData[inPos] = aChar;        // Add it to the array
       inPos++;                              // Move the index forward
       inData[inPos] = '\0';            // Add a NULL terminator
       if(inPos == 23) break;         // Make sure there is still room
   }

   if(inPos > 0)
   {
       // Parse the string here

       // Use the data here

       // Reset to save the next packet in the array
       inPos = 0;
       inData[inPos] = '\0';
   }
}

ah thanks , ive added it in and kind works

I send (1;2;3)

and it prints out

1
2
3

But if i put

I send (1;22;33)

if doesnt seem to like two numbers , have i put into the array wrong ?

int inPos = 0;
char inData[24]; // Adjust the size as needed

char *OUT[24]; 
int OUTPos = 0;
void setup()
{
 
   Serial.begin(9600);
}




void loop()
{
   while(Serial.available() > 0)
   {
       char aChar = Serial.read();  // Read a character
       inData[inPos] = aChar;        // Add it to the array
       inPos++;                              // Move the index forward
       inData[inPos] = '\0';            // Add a NULL terminator
      
//Serial.println(inData[inPos]);

if(inPos == 23) break;         // Make sure there is still room
   }

   if(inPos > 0)
   {
      



char *p =  inData; 

 char *str;

 while ((str = strtok_r(p, ";", &p)) != NULL){ // delimiter is the semicolon
   
   Serial.println(str);    
   
   
              
      }







//Parse the string here

       // Use the data here

       // Reset to save the next packet in the array
       inPos = 0;
       inData[inPos] = '\0';
   }
}

I have one last question , as im getting...

1
2
3

I need to put that back into an array ect so i can asign each to an varible and then use that data like address = 2 value = 3

But thanks its starting to take shape !

After the loop to read the whole string, does the string print correctly?

You don't need to create a pointer to the array.

The strtok command needs to be called once with inData (or p if you are determined to use a pointer), to get the first token. Subsequent calls use NULL as the first argument.

char *token = strtok(inData, ";");
if(token)
{
   int val = atoi(token);
   whle(token = strtok(NULL, ";")
   {
       val = atoi(token);
   }
}

This code also illustrates how to convert the tokens to integers. If you are always sending the same number of tokens, and the order remains the same, you can stuff the output of atoi into the correct variables. Or, keep track of the next position in an integer array, in the same way that you track the position in the character array when reading the serial data.