Synch Arduino/C

Hi I'm having a synch problem.

Arduino Code:

typedef struct JoystickEstadoAtual{
  int movimento;
  int btn1;
  int btn2;
  int btn3;
  }JoystickEstadoAtual;


  JoystickEstadoAtual joystickEstadoAtual;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  joystickEstadoAtual.movimento = 1;
  joystickEstadoAtual.btn1 = 2;
  joystickEstadoAtual.btn2= 3;
  joystickEstadoAtual.btn3 = 0;

}

void loop() {
  // put your main code here, to run repeatedly:
Serial.write((const uint8_t *)&joystickEstadoAtual, sizeof(joystickEstadoAtual));
}

C code:

#include <Windows.h>
#include <stdio.h>

typedef struct JoystickEstadoAtual {
	byte movimento;
	byte btn1;
	byte btn2;
	byte btn3;
}JoystickEstadoAtual;

JoystickEstadoAtual joystickEstadoAtual;



void openPort(HANDLE* hComm) {

	char  ComPortName[] = "\\\\.\\COM3";

	*hComm = CreateFile(ComPortName,                  // Name of the Port to be Opened
		GENERIC_READ | GENERIC_WRITE, // Read/Write Access
		0,                            // No Sharing, ports cant be shared
		NULL,                         // No Security
		OPEN_EXISTING,                // Open existing port only
		0,                            // Non Overlapped I/O
		NULL);                        // Null for Comm Devices

	if (*hComm == INVALID_HANDLE_VALUE) {
		printf("\n    Error! - Port %s can't be opened\n", ComPortName);
		system("exit");
	}
	else {
		printf("\n    Port %s Opened\n ", ComPortName);
	}
}


BOOL settings(HANDLE hComm) {

	BOOL Status = TRUE;

	/*------------------------------- Setting the Parameters for the SerialPort ------------------------------*/

	DCB dcbSerialParams = { 0 };                         // Initializing DCB structure
	dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

	Status = GetCommState(hComm, &dcbSerialParams);      //retreives  the current settings

	if (Status == FALSE) {
		printf("\n    Error! in GetCommState()");
		return Status;
	}

	dcbSerialParams.BaudRate = CBR_9600;      // Setting BaudRate = 9600
	dcbSerialParams.ByteSize = 8;             // Setting ByteSize = 8
	dcbSerialParams.StopBits = ONESTOPBIT;    // Setting StopBits = 1
	dcbSerialParams.Parity = NOPARITY;        // Setting Parity = None 

	Status = SetCommState(hComm, &dcbSerialParams);  //Configuring the port according to settings in DCB 

	if (Status == FALSE)
	{
		printf("\n    Error! in Setting DCB Structure");
		return Status;
	}
	else //If Successfull display the contents of the DCB Structure
	{
		printf("\n\n    Setting DCB Structure Successfull\n");
		printf("\n       Baudrate = %d", dcbSerialParams.BaudRate);
		printf("\n       ByteSize = %d", dcbSerialParams.ByteSize);
		printf("\n       StopBits = %d", dcbSerialParams.StopBits);
		printf("\n       Parity   = %d", dcbSerialParams.Parity);
	}

	/*------------------------------------ Setting Timeouts --------------------------------------------------*/

	COMMTIMEOUTS timeouts = { 0 };
	timeouts.ReadIntervalTimeout = 50;
	timeouts.ReadTotalTimeoutConstant = 50;
	timeouts.ReadTotalTimeoutMultiplier = 10;
	timeouts.WriteTotalTimeoutConstant = 50;
	timeouts.WriteTotalTimeoutMultiplier = 10;

	if (SetCommTimeouts(hComm, &timeouts) == FALSE) {
		printf("\n\n    Error! in Setting Time Outs");
		return Status;
	}
	else
		printf("\n\n    Setting Serial Port Timeouts Successfull");

	/*------------------------------------ Setting Receive Mask ----------------------------------------------*/

	Status = SetCommMask(hComm, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception

	if (Status == FALSE) {
		printf("\n\n    Error! in Setting CommMask");
		return Status;
	}
	else {
		printf("\n\n    Setting CommMask successfull");
	}

	return TRUE;



}




void closePort(HANDLE hComm) {
	CloseHandle(hComm);//Closing the Serial Port

}

void readPort(HANDLE hComm) {
	BOOL Status;
	DWORD NoBytesRead;                     // Bytes read by ReadFile()
	DWORD dwEventMask;                     // Event mask to trigger

	/*------------------------------------ Setting WaitComm() Event   ----------------------------------------*/

	
		Status = WaitCommEvent(hComm, &dwEventMask, NULL); //Wait for the character to be received*/

	/*-------------------------- Program will Wait here till a Character is received ------------------------*/

		if (Status == FALSE)
		{
		}
		else //If  WaitCommEvent()==True Read the RXed data using ReadFile();
		{
			while (1) {
				Status = ReadFile(hComm, &joystickEstadoAtual, sizeof(joystickEstadoAtual), &NoBytesRead, NULL);

				printf("Movimento: %d\n", joystickEstadoAtual.movimento);
				printf("BTN 1: %d\n", joystickEstadoAtual.btn1);
				printf("BTN 2: %d\n", joystickEstadoAtual.btn2);
				printf("BTN 3: %d\n", joystickEstadoAtual.btn3);

				Sleep(1000);



			}
	}
}

void main(void)
{
	HANDLE hComm;                          // Handle to the Serial port  // Name of the Serial port(May Change) to be opened,
	BOOL  Status;                          // Status of the various operations           



	openPort(&hComm); // Open Port

	Status = settings(hComm);  //Set configuration Port
	if (Status == FALSE) {
		printf("ERRO!");
	}

	else {
		
			readPort(hComm);

		
	}



	closePort(hComm);
	system("exit");



}//End of Main()

Is there a way to get the right data? What I need to do is write the whole struct(it can be an array too, if it's possible) and the C program will read the right data.

Because what I got until now is for example, the arduino code is sending a 1, 2, 3, 0, and the c program is getting 2, 3, 0, 1 for example.

I think about another two solution: The first solution is to increase the buffer COM PORT, but how can I do this in arduino? The second solution is split a com port in two another ports, but how can I do this in arduino too?

I'm accepting another solutions too.

Thanks !

Is there a way to get the right data?

Use ASCII transmissions, and beginning and end of record markers, as described here.

This looks very like the same query in your other thread
https://forum.arduino.cc/index.php?topic=644371