Serial.write with Arrays

I need to put a certain amount of values over the serial port. So I have created an array that according to the Serial.write documentation i can send in one line of code. But I get errors.

The documentation (again) is not comprehensive: Serial.write() - Arduino Reference

Serial.write(buf, len)

So buf should be the array and len is?

Serial.write(end_control_message);

Produces an error: no matching function for call to 'write(volatile char [12], int)'

At this point i cannot continue.

At this point i cannot continue.

At this point we cannot provide help without seeing your complete program and the full error message

If you are going to write the contents of an array other than a string then you need to supply the length of the array in bytes, as the second parameter.

Sorry my actual code was

Serial.write(end_control_message, 12);

This is what produced the error. I removed the 12 to see what happened.

So "len" is the array length.

The array is:

volatile char end_control_message[12] = {83, 0, 128, 1, 0, 0, 0, 0, 0, 0, 129, 69};

Why have you used the modifier volatile in your array declaration. Try again by discarding the modifier.

I've opened an issue on github for the missing description of the len parameter: Serial.write(buf,len) missing description of len parameter · Issue #473 · arduino/reference-en · GitHub.

It will be fixed one day :wink:

GolamMostafa:
Why have you used the modifier volatile in your array declaration. Try again by discarding the modifier.

Because the variable is outside of the function, I thought this was how C works?

You only need volatile if you share a variable between an ISR and other functions (loop() usually).

You can solve the problem by explicitely casting the array

Serial.write((char*)end_control_message, 12);

Removing volatile works but I do not understand. As the variable is declared outside of any function it is by definition volatile anyway. I prefer to be explicit in my code as it avoids errors.

As the variable is declared outside of any function it is by definition volatile anyway.

Are you sure that you mean volatile and not global ?

aren't they the same thing?

Absolutely not. Google volatile to find out what it does.

Ok so next small problem.

warning: narrowing conversion of '128' from 'int' to 'char' inside { } [-Wnarrowing]

So 128 is in the declaration of a char type array, what decided this wan an int ?

SIGH

Why is it so hard for posters to understand that without an example that shows the problem it is difficult/impossible to provide help

char end_control_message[12] = {83, 0, 128, 1, 0, 0, 0, 0, 0, 0, 129, 69};

It's in the post above, remember the topic was about a char array???

From char - Arduino Reference

The char datatype is a signed type, meaning that it encodes numbers from -128 to 127. For an unsigned, one-byte (8 bit) data type, use the byte data type.

I see but the message implies that it is converting an int to a char.

Maybe instead of re inventing the wheel the gcc data types should be used as it's clear what they are.

SparkyLabs:
Maybe instead of re inventing the wheel the gcc data types should be used as it's clear what they are.

It was you that chose to use a signed 8 bit variable to hold a value of 129 so feel free to use a suitable data type

SparkyLabs:
I see but the message implies that it is converting an int to a char.