Serial communication (Windows to Arduino)

Hey everyone,

I'm having difficulty understanding how Arduino reads incoming information via serial. My current project has me transmitting information from Windows over serial. I've sent 3 short ints (i.e.: 6 bytes of information on Windows) which I want Arduino to receive as ints. The short ints were stored in an array of ints (int buffer[3]) and I intend to be able to extract those 3 ints so I can use them for whatever later on in my program.

Here are the things that are preventing me from proceeding:

1) I believe that Serial.readBytes() is a better option for me because Serial.read() only reads a single byte of information and I want to store more than one byte. Is this correct or is there a way to manipulate Serial.read() to work for me?

Assuming Serial.readBytes( val1, val2 ) is the way to go, I'd like some clarification because the tutorial on this website leaves a bit of ambiguity in my opinion:

2) the function itself returns the number of bytes that have been transmitted. However, it also stores all of the bytes read in the buffer val1. In my case, is val1 going to have to be at least 6 bytes to contain it all? If so, does the following appear the correct way of setting this up?

int val1[3]; int val2 = 6;

int numberOfBytes = Serial.readBytes( val1 , val2 );

The variable numberOfBytes should contain 6 bytes. The array val1 should contain 3 ints received by Arduino and Arduino will stop reading information once it receives 6 bytes (val2). Correct?

3) Can val2 be longer than 6 bytes? If it can, is it safe to assume that the information will just not be considered in my program?

4) The Arduino tutorial indicates that the data is stored as chars or bytes. Does this mean I need to cast the data as ints to read it?

I have been playing around with Serial communication for the last little while and I feel there is a flaw in my understanding somewhere because what I'm attempting doesn't appear to work. I can post the entirety of my code but I'm trying to understand the "theory" behind it so I can proceed with more ambitious projects with the correct understanding.

Thank you! Let me know if further clarification is required.

I would put a delimiter between the numbers, and an end of packet character (probably a beginning character too), and stuff them into a fixed length character array (being sure to not overflow it!) as they come in. When you get the end character, then that's your signal to process the received characters with strtok, atoi and similar.

ex, something like

!123|213|545*

My usual MO is to store whether we're currently in the middle of a packet (ie, have received the !), and ignore the incoming characters otherwise (under the assumption that we came in in the middle of a packet, or have otherwise wound up with gibberish on the line)...

Click here for my post which may help you

acondax: I'm having difficulty understanding how Arduino reads incoming information via serial.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

If you can, you should use the technique in the 3rd example for greatest reliability.

...R

Read 6 bytes into a buffer of size 6 bytes; how is not so important at this moment.

// demo received data
byte buffer[6] = { 0xAA, 0x65, 0xFF, 0x12,  0x13, 0x00};
// interprete buffer as int
int *ptr = (int*)buffer;
// print the 3 ints
Serial.println(*ptr);
ptr++;
Serial.println(*ptr);
ptr++;
Serial.println(*ptr);

Not tested.