Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:27:40+00:00 2026-06-12T06:27:40+00:00

I have written a program for the Arnuino that sends a struct witin a

  • 0

I have written a program for the Arnuino that sends a struct witin a union to a program on the PC. The struct has to integers but i dont’t get the right output. The program on the PC makes use of the boost library for the serial conection. And is build and compiled in 64bit (with vs2010).

The code works if i have a single integer variable within a union. But a struct witin a union doesn’t work. Only one integer gets data and that data is wrong.

I it maby a 64 bit(pc) and 32bit(Ardunio) problem? And can anyone help me with this. Thanks in advance.

The PC code snippet (serial settings are omitted):

union packed{
  struct test{
  unsigned int data;
  unsigned int data2;
} struc;

 unsigned char bytes[8];
}SerialPacked;

  SerialPacked.struc.data = 0;
  SerialPacked.struc.data2 = 0;

  cout << "Data before: " << SerialPacked.struc.data << endl;
  cout << "Data2 before: " << SerialPacked.struc.data2 << endl;

  read(port,buffer((unsigned char*)&SerialPacked.bytes[0], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[1], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[2], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[3], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[4], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[5], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[6], 1));
  read(port,buffer((unsigned char*)&SerialPacked.bytes[7], 1));

  cout << "Data after: " << SerialPacked.struc.data << endl;
  cout << "Data2 after: " << SerialPacked.struc.data2 << endl;

The Arduino code:

int ledPin = 13;


union packed{
    struct test{
      unsigned int data;
      unsigned int data2;
}struc;
    unsigned char bytes[8];
} 
SerialPacked;

void setup() {  
    pinMode(ledPin, OUTPUT); 
    Serial.begin(9600); 

   SerialPacked.struc.data = 0;
   SerialPacked.struc.data2 = 0;
};




void loop() {

while(1){
  digitalWrite(ledPin,HIGH);
  SerialPacked.struc.data = SerialPacked.struc.data + 1;
  SerialPacked.struc.data2 = SerialPacked.struc.data2 + 1;;

  for(int i=0;i <8; i++){ 
    Serial.write(SerialPacked.bytes[i]);
  };

    digitalWrite(ledPin,LOW);
    delay(1000);
 };

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:27:42+00:00Added an answer on June 12, 2026 at 6:27 am

    The issue is that int on Arduino is two bytes, but an int on your PC is probably four bytes. Depending on your compiler, there may be a switch that you can use to set the size for int, or you can just use a more explicit type. The idea with int is that it’s supposed to allow easy adaptation of code from one platform to another by adopting whatever the natural size is for the host platform. For that same reason, though, it’s not a good choice for transferring data between platforms.

    To confirm that this is the problem, try reading the bytes out of SerialPacked instead of accessing struc. I’m sure you’ll find that all the data is there — it’s just the way you’re trying to read it that’s the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program that uses qhttp to get a webpage. This works
I have written a program that makes sure each cookie has more than 5
I have written a program that sends a tcp request to a web address
I have written this program that has a main function, inside which, I am
I have written a program in PowerShell that loops and checks stuff. I would
hey guys, beginner here. I have written a program that outputs files to .txt's
I have written a program that runs on Linux and uses sigevent and timer_create
I have written a program that is compiled and run in Netbeans and Eclipse
I have written a program that stores booking information in a text file in
I have written a program that runs with ROOT permission in Terminal following login

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.