Serial received data values in Integer array

How can i store serial received data values in a Integer array?

I'm sending a message like this "c;1;2;3;4" to the Arduino.

"c" means the command / action
1;2;3;4 are the values that arudino should process

The message length can differ.

At the moment i have this code:

// This example sets all the values of the digital pins with a list through a callback function

#include <Messenger.h>
// Instantiate Messenger object with the default separator (the space character)
Messenger message = Messenger(',');

// Create the callback function
void messageReady()
{
if ( message.checkString("c") )
{
// Loop through all the available elements of the message
while ( message.available() )
{
int value = message.readInt();

Serial.println(value);
}
}
else if
{
if ( message.checkString("PING") )
Serial.println("ACK");
}
else
//falls es eine message für die es kein handling gibt
message.readInt();
}

void setup() {
// Initiate Serial Communication
Serial.begin(115200);
// Attach the callback function to the Messenger
message.attach(messageReady);
}

void loop() {
// The following line is the most effective way of using Serial and Messenger's callback
while ( Serial.available() ) message.process(Serial.read () );
}

The problem is that i don't know how to add the received values into a Integer array.
Can someone help?
thx a lot

I'm still new to serial comms but you need to read the entire buffer not just the first byte seen which means Serial.read() should not be used. Serial.bufferUntil("\n") is the .net equivalent but I do not know its Arduino version. Once you have the full buffer you can parse it for its five arguements although you may wish to read until you get a character (a-z) then parse what is after as an array as arrays are a single type (all integers etc).
Goto to the reference section for some command listing and further help on Serial and Arrays
Hope this gives you at least some help

The Messenger class handles converting the numbers to ints for you. The message.readInt(); call returns an int.

Storing that value in an array is done just like storing any other value in an array. You need to define an array, and some index into that array. Then, store the value in the array at the position pointed to by the index.

I managed now to get it (nearly) working with following code:

void setup()
{
// Initiate Serial Communication
Serial.begin(9600);

// Attach the callback function to the Messenger
message.attach(messageReady);

Serial.println(deviceName + " ready!!");
}

// Create the callback function
void messageReady()
{
char firstChar = message.readChar(); // Gets the first word as a character

if (firstChar == 's')
{
int msg[maxMsgLength];

for (int i=0; i<maxMsgLength;i++)
msg = -1;

  • int value;*

  • int counter = 0;*

  • do*

  • {*

  • value = message.readInt();*

  • if (value != -1)*

  • msg[counter] = value;*

  • counter++;*

  • }*

  • while (value != -1);*

  • for (int i=0; i<maxMsgLength;i++)*
    _ if (msg != -1)_
    _ Serial.println(msg*);
    }
    else*
    * Serial.println("UNKNOWN CMD");*_

* Serial.println("---");*
}
void loop()
{
* while ( Serial.available() )*
* message.process(Serial.read () );*
}
[/quote]
Only 2 problems left:
1. If i send a "big" Integer arduino returns something wrong (for example i send 123456, Arduino shows -7616)
*2. If i send a long(er) message (seems more than 63 chars) it doesn't work anymore *
Any hints?

          int msg[maxMsgLength];
         
          for (int i=0; i<maxMsgLength;i++)
            msg = -1;

See the pretty box with the white background and scroll bars (if needed)?

int msg[maxMsgLength];

for (int i=0; i<maxMsgLength;i++)
msg = -1;

See the ugly gray background?

There is a reason for using the # icon when posting code, rather than the speech bubble icon.

The code you posted looks like you are trying to set an entire array to -1. Clearly (at least to me), you can't do that. The forum software may be to blame for stripping the [ i ] from your code, or you may be to blame for not putting it in in the first place. The only way for us to be sure is if you post your code correctly (using the # icon).

          do
          {
            value = message.readInt();
           
            if (value != -1)
              msg[counter] = value;
           
            counter++;
          }
          while (value != -1);

No. A do/while loop will always be executed at least once. That is not what you want to do. How do you know, for a random message that there will be a series of ints in a row? In your example, there are not.

If i send a "big" Integer arduino returns something wrong

No, you are using the wrong function, or not respecting the limitations of the Messenger class. If you are sending a long value, you must use the Messenger::readLong() method, if there is one. If there isn't, you can't send longs using the Messenger class.

If i send a long(er) message (seems more than 63 chars) it doesn't work anymore

Have you looked at the Messenger class documentation or code? Is is set up to handle messages that long?

Most likely not, as that was not it's intent. Break your messages up into smaller pieces.

No, you are using the wrong function, or not respecting the limitations of the Messenger class. If you are sending a long value, you must use the Messenger::readLong() method, if there is one. If there isn't, you can't send longs using the Messenger class.

Yes i found it, this is working now.

Unfortunately it still seems i can't recieve messages with more than 255 characters (also after setting the MESSAGE_BUFFER_SIZE of the messenger library to something > 255)...

Unfortunately it still seems i can't recieve messages with more than 255 characters (also after setting the MESSAGE_BUFFER_SIZE of the messenger library to something > 255)...

255 characters is a whole paragraph, not a message.

Perhaps you need to re-think what you are doing, or how you are sending data to the Arduino.