Android Question union struct convert to byte array

marcbu

Member
Licensed User
Longtime User
Hello

with serial bluetooth connection I try to transfer information to a microcontroller. Best would be to have same structure of data to work on both sides.

For ex. I have following struct programmed in C language at microcontroller side:
typedef struct
{
unsigned int ID;
unsigned char len;
union
{
unsigned char B[8];
unsigned int W[4];
} Data;
} CANMessageDef;

Inside b4a I try following structure:
B4X:
Type CanMsgType (ID As Short,  len, Data(8) As Byte)
Dim myTypeVar As myType

How can I send this via blue to have a byte stream with only valid bytes to copy direct at receiving side into the stucture?
Data may have length from 0 to 8 bytes. All data should be byte aligned. I thought ConvertObjectToBytes will work for this, but wrong way!

Is there any method to create union like data structures?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The structure will be formatted differently on different platforms.
If you were using B4R to program the microcontroller then you could have used B4RSerializator.

You need to use RandomAccessFile to write to the data to an array (RAF.Initialize3) and then send the array. You will need to read the data on the microcontroller and build the structure.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…