1

I'm successfully sent single int values from Processing to Arduino, but now I need to sent a pair of ints (both having values between 0 and 90) together and I'm not sure how to do that.

Reading the docs, I see I can send byte/byte[]/int/char/String types. One idea is to send string containing the two ints concatenated by a comma character, but am not 100% sure how to convert the data to a String and split it in Arduino.

serial.write(intValue1+","+intValue2);

Another thing I was thinking was packing two ints into a byte[] somehow, but I haven't worked with bytes much, so any tips on getting started would be helpful.

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Sounds to me you should try all of the above to gain more experience. Sort of the point of an Arduino, isn't it? Use the int.ToString() method in your snippet, although I don't recognize the language. – Hans Passant Mar 03 '12 at 18:41

1 Answers1

2

If you're sending pairs of ints, or any number of bytes, it seems you may want to consider a simple serial protocol. There are a few questions here specifically about serial command protocols for embedded systems, for example:

  1. How do you design a serial command protocol for an embedded system?
  2. Simple serial point-to-point communication protocol

One suggestion is starting off your message with a header byte, usually something outside of your data range if possible (>90 in your case?). The following code has not been tested; it's just an idea, but it should run.

int lookingForHeader = 1;
int lookingForByteOne = 0;
int lookingForByteTwo = 0;

while (myPort.available() > 0) {
    while (lookingForHeader == 1) {
        int header = myPort.read();
        if (header == HEADER_BYTE) {
            lookingForHeader = 0;
            lookingForByteOne = 1;
        }
        // Consider incrementing a counter here and timing out if it gets too large.
    }

    if (lookingForByteOne == 1) {
        int byteOne = myPort.read();
        // Check if byteOne is between 0 and 90.
        lookingForByteOne = 0;
        lookingForByteTwo = 1;
    }

    if (lookingForByteTwo == 1) {
        int byteTwo = myPort.read();
        // Check if byteTwo is between 0 and 90.
        lookingforByteTwo = 0;
        lookingForHeader = 1;

        // Perhaps do a serial.write() here to acknowledge that 
        // a header and two bytes have been received.

        // Finally, do stuff with byteOne and byteTwo.
    }
}

Another option would be to acknowledge the receipt of each byte (i.e. receive header, respond with a special "ack header" byte, receive next byte, respond with "ack byte one", etc.). This is slower but it may fine for you.

Finally, a couple key points from the questions I linked to above:

Consider sending a checksum (i.e. sum of bytes) at the end of the message. The Arduino would add up the header, byte 1 and byte 2. If it doesn't match the check sum, then it could respond with a failure, or send again message.

Rex's answer to question [2] sums up what a message protocol might look like:

// total packet length minus flags len+4
U8 sflag;         //0x7e start of packet end of packet flag from HDLC
U8 cmd;           //tells the other side what to do.
U8 len;           // payload length
U8 payload[len];  // could be zero len
U16 crc;
U8 eflag;         //end of frame flag
Community
  • 1
  • 1
gary
  • 4,227
  • 3
  • 31
  • 58
  • That is a wealth of data. I'm don't know much about message protocols, so this is quite handy, thanks! – George Profenza Mar 04 '12 at 22:47
  • I hope it helps. The code sample would be for the Processing side. You would need similar hand-shaking code in the Arduino. The best bet is to write up the protocol (on paper), then implement it on each side. You could even start one step smaller and send a two-byte message - one for the header, one for the data. – gary Mar 05 '12 at 13:40