Hi. I want to know is there any way to mapping a struct in arduino to c. The C program is the receiver.
Arduino:
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 = (byte*)1;
joystickEstadoAtual.btn1 = (byte*)2;
joystickEstadoAtual.btn2 = (byte*)3;
joystickEstadoAtual.btn3 = (byte*)0;
}
void loop() {
// put your main code here, to run repeatedly:
Serial.write((const uint8_t *)&joystickEstadoAtual, sizeof(joystickEstadoAtual));
}
C program:
#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);
}
}
}
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
joystickEstadoAtual.movimento = 0;
joystickEstadoAtual.btn1 = 0;
joystickEstadoAtual.btn2 = 0;
joystickEstadoAtual.btn3 = 1;
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()
The problem is the receiver get what is to be in btn1, for example, it gets to movimento. The data is wrong! And I can't do something like <1, 0, 0, 0> for example, and wait to find the first is <.