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!!!!!!!!