Reputation: 31
I want to include writeFile function of serial port wtiting to a fuctiona and call that function several time. I wrote following code. But function is returning false. I could not find any error.
bool WriteBuffer (char *lpBuf,DWORD dwToWrite){
// DWORD dwBytesWritten;
// DWORD dwToWrite;
printf("%s", lpBuf);
if(!WriteFile(hSerial, lpBuf, sizeof(lpBuf) ,&dwBytesWritten, NULL))
{
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&lastError,
1024,
NULL);
CloseHandle(hSerial);
hSerial=NULL;
printf("ERROR in WRITE FILE \n");
return false;
//Handle Error Condition
}
printf("You wrote'%s'",lpBuf);
return true;
}
Upvotes: 0
Views: 1375
Reputation: 612954
You are passing sizeof(lpBuf)
to the nNumberOfBytesToWrite
parameter. But sizeof(lpBuf)
is simply the size of a pointer. You should be passing dwToWrite
I presume.
If that doesn't solve your problem then the other cause of errors in hSerial
not being valid.
Having gone to all the trouble of calling GetLastError
and FormatMessage
, it would have been helpful if you had told us what those functions returned.
Upvotes: 1