Society of Robots - Robot Forum
Software => Software => Topic started by: madsci1016 on December 21, 2009, 07:38:39 PM
-
Hey guys,
I am trying to send data from one uC ( an arduino) to another (a Axon)
This is what i wrote so far:
out_str[0] = 'S';
out_str[1] = distance_moved;
out_str[3] = voltage;
out_str[7] = current;
CS = 0;
for (int num = 0; num <= 10; num++)
CS ^= out_str[num];
out_str[11] = '*';
out_str[12] = CS;
Serial.write(out_str, 13);
distance is a signed int, and voltage and current are floats. I just figured the 4 bytes of float voltage would copy into out_str[3 - 6] but i think that is incorrect. how would you do this?
-
I also didn't mention that the array elements were 'byte' type. Took me forever to figure out that the Serial.write would not accept an array of 'char' type.
I think I'm gonna scrub this plan of 'controlled overflow'. I'll change my voltage and current to signed ints and store it as mV and mA so i still have the precision i want, and just make the array elements int as well.
-
Well, that worked, but now i don't know how to calculate a 8 bit checksum using 2 byte int values in an array. How could i do direct pointers so that &out_str + 2 was really out_str[1]? Since each element is two bytes for int?
-
may i make a suggestion? create a structure
the first element of the structure should be a char, assign it 's'
the 2nd, 3rd, and 4th element should be your three floating values
add a char for the star
then another for the checksum
so basically
struct
{
char msgHeader;
float[3] data;
char star;
char chksum;
} msg;
msg.msgHeader = 's';
msg.data[0] = distance_moved;
msg.data[1] = voltage;
msg.data[2] = current;
msg.star = '*';
msg.chksum = 0;
for (int i = 0; i < sizeof(msg) - 2; i++)
{
msg.chksum ^= msg[i];
}
for (int i = 0; i < sizeof(msg); i++)
{
Serial.write((byte)msg[i]);
}
-
Wow, nifty.
I haven't gotten to structure in my self taught adventures in C.
Few questions,
Your checksum loop creates a 8 bit checksum, Xor's every byte? Meaning msg[2], msg[3], msg[4], msg[5] would be the 4 bytes of msg.data[0]?
Was there a reason for the loop for Serial.write, would Serial.write(msg, sizeof(msg)) not work?
Thanks for your help.
-
first question, yes
second question
the compiler would not be smart enough to cast an user defined struct to a pointer, or at least i think it couldn't, i wrote that example really fast
-
Ok, Thanks Frank, you saved about half of my hair from getting pulled out.
I will play with the Serial.Write to see if it can handle the whole buffer or not. In my experiences with Arduino, the less number of calls to the 'shortcut' functions, the better.
-
Th compiler does not like
msg.chksum ^= msg;
It responds " error: no match for 'operator[]' in 'msg' "
I have tried a pointer but it did not like that either. Any ideas?
Thanks.
-
Well, i did some furter reading on pointers to structures, and coded this:
struct message
{
char msgHeader;
int data[3];
char star;
byte CS;
} msg;
message * msgPnt;
msgPnt = &msg;
for (int i = 0; i < sizeof(msg) - 2; i++)
{
msg.CS = msgPnt + i;
}
It still does not like what is in the loop, says : invalid conversion from 'message*' to 'byte'
Man i'm losing my hair.
-
hmm that was weird, i could've sworn structs could be accessed like arrays... i thought i did it before
or maybe i did use a pointer, since this piece of code compiled fine
int main()
{
struct
{
char msgHeader;
float data[3];
char star;
char chksum;
} msg;
char * p = &msg;
msg.msgHeader = 's';
msg.data[0] = 1;
msg.data[1] = 1;
msg.data[2] = 1;
msg.star = '*';
msg.chksum = 0;
for (int i = 0; i < sizeof(msg) - 2; i++)
{
msg.chksum ^= p[i];
}
return 0;
}
sorry for the mix up, but structs really helps in your situation
-
still does not like
char * p = &msg;
says
cannot convert '<anonymous struct>*' to 'char*' in initialization
This is in the aurduino IDE which uses the gcc compiler i believe.
ARGGG!
-
Thank goodness for co-workers with PhD's
the solution was to type cast
byte *p = (byte*)&msg;
It compiles, i have yet to test it on hardware.