Hi all,
Im trying to read data that is send from my Arduino Duemilanove(Atmega328).
Link to arduino: http://arduino.cc/en/Main/ArduinoBoardDuemilanove
My arduino sends a max of 24 chars.
example 1: 32767*32767*32767*32767
Now if send 24 chars and read them in "UitBuff"(24 chars) the data is correct.
However if i send less then 24 chars and still try to read 24 chars the data is not always correct.
example 2: if i try to send: 33*32767*57*4231* (17 chars)
I sometimes get values like "3*32767*57*4231**2"
Now i know that values behind the the 17th char can be anything but the values like "33" in de beginning is now "3". Can anyone explain to me why?
Also if i change UitBuff to 17 chars and nNumberOfBytesToRead to 17 instead of 24 the data is always correct.
So the problem is if i send 17 and try to read 24 chars i dont always get the the correct data. Can anyone help me out here??
Thanks you!
Alex
Code:
HANDLE hComm = NULL;
COMMTIMEOUTS ctmoNew = {0}, ctmoOld;
//DWORD dwBytesRead;
DCB dcbCommPort;
hComm = CreateFile( "\\\\.\\COM1", // The "\\\\.\\" is for com ports above numeric 9.
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if (hComm != INVALID_HANDLE_VALUE){
Sleep(1500); //w8 1.5 seconds for arduino to be ready
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 10;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);
// SET BAUD RATE, PARITY, WORD SIZE, en STOP BITS.
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("9600,N,8,1", &dcbCommPort);
SetCommState(hComm, &dcbCommPort);
char UitBuff[24];
DWORD dwBytesRead;
TransmitCommChar(hComm, 'r');
//when r is received by arduino it sends the characters back
ReadFile(hComm, UitBuff, 24, &dwBytesRead, NULL);//
ShowMessage(UitBuff);
}
else {
ShowMessage("Cant open port!");
}