I am having a lot of trouble opening up a serial communications port with win32 in VC++ 6.0.
This is the following code I have. It is in a very standard win32 application, one that just displays hello world (at this point, I am just trying to get the thing opened).
The following is under a WM_CREATE message. The handlePort and config are instantiated before the switch messsage in the WndProcedure as a HANDLE and DCB respectively.
Code:
handlePort = CreateFile("COM1", GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
if (handlePort = INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
MessageBox(hWnd,"MAy","May",MB_OK);
}else{
MessageBox(hWnd,"OKAY","OKAY",MB_OK);
}
}
GetCommState(handlePort,&config);
config.BaudRate = 38400;
config.Parity = NOPARITY;
config.ByteSize = 8;
config.StopBits = 1;
SetCommState(handlePort, &config);
// instance an object of COMMTIMEOUTS.
COMMTIMEOUTS comTimeOut;
// Specify time-out between charactor for receiving.
comTimeOut.ReadIntervalTimeout = 3;
// Specify value that is multiplied
// by the requested number of bytes to be read.
comTimeOut.ReadTotalTimeoutMultiplier = 3;
// Specify value is added to the product of the
// ReadTotalTimeoutMultiplier member
comTimeOut.ReadTotalTimeoutConstant = 2;
// Specify value that is multiplied
// by the requested number of bytes to be sent.
comTimeOut.WriteTotalTimeoutMultiplier = 3;
// Specify value is added to the product of the
// WriteTotalTimeoutMultiplier member
comTimeOut.WriteTotalTimeoutConstant = 2;
// set the time-out parameter into device control.
SetCommTimeouts(handlePort,&comTimeOut);
Can anyone tell me what is wrong with my code?