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!");
	}

If you have a receive buffer which always reads 24 chars, and you're going to send less than 24 chars, be sure to send a 'end of string' character which is a '\0' .

Also a good idea: Use English variable names and comments instead of Dutch. Not everyone here can read Dutch. :P

If you have a receive buffer which always reads 24 chars, and you're going to send less than 24 chars, be sure to send a 'end of string' character which is a '\0' .

Tried this but does not seem to work.....

Also a good idea: Use English variable names and comments instead of Dutch. Not everyone here can read Dutch.

Hmmm yeah ur right!ill do that next time!

However i found sort of an solution..
I changed my code in the atmega328 it now always sends 24 chars.
so if i wanted to send this 33*32767*57*4231*
ill now add zero's to "33", "57" and "4231" so i will get 00033*32767*00057*04231*.

Btw those numbers represend 4-channel adc if you guys where wondering. 32767 = 5v and 00000 = 0v.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.