I have an array sent by serial port to an embedded chip.
the data is passed on reception to an ARRAY, in my case a ‘char’ ARRAY.
the format is fixed, uses only ASCII data, its a simple message protocol.
char start (‘v’)
char address (‘1’)
char serial number (‘0’ to ‘9’)
char[2] message type (an array of 2 characters)
char[20] payload (an array of up to 20 char)
this all lives in ARRAY of 26 char in size, 25 data plus 1 for string terminator.
what I just cant get working is taking this ARRAY data and ‘pouring’ it into a STRUCTURE that is formatted exactly as shown above.
it seems to me I need some UNION to align the array and structure, but I just cant get it to work out! it should be very straight forward.
I did not give enough info.
the C compiler is XC8 used in MPLABX by Microchip, my issue is not of alignment (I have not got to the point where anything could be misaligned)
the structure is no issue, I have the as you typed in code and can allocated data to all elements individually no issue.
but I can not take my ARRAY and make the first address of the array and the char ‘start’ have the same address and then that structure be able to reference the elements of the array (their memory location not array)
so simply:-
struct MyMessage = &array
for instance, or in xojo I would have used:-
there is no issue in serial port, no issue in me receiving the data, all that is not where I am having the issue.
I have an array of data, it is all ASCII data, CHAR
it is 26 char in size, it is sent from a structure and all elements are fixed in position, with the final element being of variable length.
none of this is really important.
what I want to do is take this linear memory (assume it is packed and consecutive)
and make it somehow appear in my structure :-
it should be simple, but I can not make it happen.
char start needs the same address as my char array[0] and then I access the array data by reference to the structure.
I have tried union, pointers and I just do not know where the syntax is failing.
the C compiler I am using is supposed to be C99, but it is really strict in casting so I have to do a lot of this in code and it gets stupid complex for what in theory should be some simple referencing exercise.
ok, to be totally clear and painfully obvious to anyone not following my jovial comments and taking the written words out of context:
“yes, I did try the too, but again its not worked out with the syntax I used, I blame the batteries in the keyboard getting low.”
in this phrase I create a reference, one that is absolutely nothing to do with the thread context, ‘keyboard’ being the reference.
“I know all the characters are there, but just not in the right order…”
2 references here, one is the context of that message (which I thought obvious, but seemingly not by some readers), the ‘keyboard’ and its ‘characters’, the ones typed, nothing to do with CHAR or the code in previous portions of the thread.
the second reference was somewhat specific, to a UK audience or an aficionado of historic UK comedic shows that is obviously lost entirely on a wider audience.
I will try to stick to actual labeled facts in the future and never imply anything!
I blame myself entirely for moving this thread off topic.
your understanding of what I expected to do is 100% and the code is what I thought should work, but I have tries so many variations that I can not remember them.
using the code you posted exactly produces this error in my compiler:-
g_messages_rx.c:130:23: warning: incompatible pointer types initializing ‘struct MyMessage *’ with an expression of type ‘char [26]’ [-Wincompatible-pointer-types]
struct MyMessage *message = input;