Geeting invalid value from arduino

Hi. My arduino is getting invalid value from a C program. The code is here:

Arduino Code:

uint8_t n;
int aux = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  aux = Serial.read();
  Serial.println(aux);
}

C code:

void main() {
	//FILE* arquivo;
	char  ComPortName[15] = "COM4";
	HANDLE hComm;
	int qtdTeclasDiagonaisMapeadas = 2;
	BOOL Status = protocoloComunicacao(&hComm, &qtdTeclasDiagonaisMapeadas, ComPortName);
	closePort(hComm);
}

BOOL protocoloComunicacao(HANDLE* hComm, int* aux, char ComPortName[]) {


	BOOL Status = FALSE;

	Status = openPort(hComm, ComPortName); // Open Port

	if (Status == FALSE) {
		return Status;
	}

	Status = settings(*hComm);  //Set configuration Port
	if (Status == FALSE) {
		return Status;
	}

	else {
		Status = writePort(*hComm, aux);
		if (Status == FALSE) {
			return Status;
		}

	}

	return Status;
}

BOOL writePort(HANDLE hComm, int* aux) {


	DWORD NoBytesWrite;                     // Bytes read by ReadFile()
	DWORD dwEventMask;                     // Event mask to trigger
	BOOL Status = FALSE;

	Status = WriteFile(hComm, aux, sizeof(aux), &NoBytesWrite, NULL);
	
	return Status;

}

BOOL settings(HANDLE hComm) {

	BOOL Status = TRUE;

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

	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
	
}

The problem is the arduino is getting a -1 value and not 2 as should be.

What am I do wrong?

Thank you!!!!!!!!

What am I do wrong?

In the Arduino code, you are reading the serial buffer before you know that there is anything there to read. -1 means that the serial input buffer is empty. The serial input basics tutorial will show how to get serial data from the serial port.

Also, programs written for the Arduino IDE need a loop() function call, not main(). The main() function is called at startup as part of main.c in the core libraries.

Also, from the PC, you are trying (incorrectly) to send a 4-byte int.
An int on most arduino is 2-byte.
The Serial.read() can only read 1 byte at a time.

how can I do this at the setup?

It is like the C code is not write in the COM port.

How can I write in C to Arduino?

Can someone post an example?

It is like the C code is not write in the COM port.

It is like you do not wait until C program actually does write to the serial port. The statements in the setup() section occur one, very brief, time. Your Arduino code does not wait for input, your code expects for it to be there and if there is nothing to read, reads anyway and goes blind to any data that may come in.

I can't help with the C code, but if you study the serial input basics that I linked the Arduino side will be more clear.