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:
- How do you design a serial command protocol for an embedded system?
- 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