Author Topic: assembling a string to send out over serial, need help  (Read 2814 times)

0 Members and 1 Guest are viewing this topic.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
assembling a string to send out over serial, need help
« 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:

Code: [Select]
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?


Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #1 on: December 21, 2009, 10:13:01 PM »
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.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #2 on: December 21, 2009, 10:42:25 PM »
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?


Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: assembling a string to send out over serial, need help
« Reply #3 on: December 21, 2009, 11:32:32 PM »
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
Code: [Select]
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]);
}
« Last Edit: December 21, 2009, 11:34:28 PM by frank26080115 »

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #4 on: December 21, 2009, 11:48:40 PM »
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.

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: assembling a string to send out over serial, need help
« Reply #5 on: December 22, 2009, 12:08:37 AM »
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

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #6 on: December 22, 2009, 12:14:35 AM »
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.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #7 on: December 22, 2009, 04:19:25 PM »
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.

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #8 on: December 22, 2009, 04:52:51 PM »
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.

Offline frank26080115

  • Supreme Robot
  • *****
  • Posts: 322
  • Helpful? 2
Re: assembling a string to send out over serial, need help
« Reply #9 on: December 22, 2009, 05:21:43 PM »
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
Code: [Select]

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

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #10 on: December 22, 2009, 06:07:11 PM »
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!

Offline madsci1016Topic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,450
  • Helpful? 43
    • Personal Website
Re: assembling a string to send out over serial, need help
« Reply #11 on: December 22, 2009, 08:36:50 PM »
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.

 


Get Your Ad Here

data_list