Sending structs through serial.


hi

i working on project of making wireless universal controller old ps2 controller. success far sending single data.

eg
serial.write hex value if press button, read wirelessly through bluetooth on board


the problem sending multiple commands , data @ once  like
-the analog readings of particular button
-read states of multiple buttons @ same time. , on.

my approach using structure, (functions used in code not real thing) :
code: [select]


struct button{
int hex_value; /*each button has own hex value created*/
int analog;
};
button state;

//example of sending command
if (up_button_is_pressed)
{
  state.hex_value=up_button_hex;
  state.analog= read.analog(up_button);
  serial.write(state); //my problem starts here
}

//example of receiving command after serial reading
//using same struct
  if (state.hex_value==up_button_hex)
{
  serial.println("up button pressed");
  serial.println("up button pressed hard:");
  serial.print(state.analog);
}



my problem don't know how send , receive struct through serial. , there alternative way better struct send multiple command , data.

thank you

**just learning how code

the idea of struct good. variable holding struct pointer first byte of struct , sizeof() function tell how many bytes in struct.

so can have byte pointer variable pointing address of struct , iterate each byte in struct , write byte serial. way dump memory structure in binary onto serial port. on receiving side read same way , populate bytes of declared structure. (you can use union make such code looks better arbitrarily casting pointers)

the thing worry if receiving computer not same type sender uno sending due because data types such int not have same number of bytes on both sides. in case need smart , know send , rebuild "manually" new representation on receiving end based on formats should like.


Arduino Forum > Using Arduino > Programming Questions > Sending structs through serial.


arduino

Comments

Popular posts from this blog

Valutazione Template - Joomla! Forum - community, help and support

SD Datastring Convention

First use of Arduino Uno : avrdude error on Blink uploading