I have developed two dialog based application One for reading from COM1 port and other
to write on COM1 port.Read application waits for com event.Problem is write application able to open com port if read applcation is not running.but not able to
open com port if read application is running.This is not threading problem,
Its something is regarding serial Port Basic concepts.
My Read and write functions are as follows.I need to extend this basic functionality
furthur,but before this simple read and write needs to work
int CReadDlg::Readme()
{
CString PortSpecifier = "COM1";
DCB dcb;
int retVal;
BYTE Byte;
DWORD dwBytesTransferred;
DWORD dwCommModemStatus;
HANDLE hPort = CreateFile(
PortSpecifier,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL
);
if (!GetCommState(hPort,&dcb))
return 0x100;
dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if (!SetCommState(hPort,&dcb))
return 0x100;
SetCommMask (hPort, EV_RXCHAR | EV_ERR); WaitCommEvent (hPort, &dwCommModemStatus, 0); if (dwCommModemStatus & EV_RXCHAR)
ReadFile (hPort, &Byte, 1, &dwBytesTransferred, 0); else if (dwCommModemStatus & EV_ERR)
retVal = 0x101;
retVal = Byte;
CloseHandle(hPort);
return retVal;
}
void CSimulatorDlg::OnWrite()
{
CString PortSpecifier = "COM1";
CString data = "A"; DCB dcb;
DWORD byteswritten;
HANDLE hPort = CreateFile(PortSpecifier,GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if (!GetCommState(hPort,&dcb))
return ;
dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if (!SetCommState(hPort,&dcb))
return ;
bool retVal = WriteFile(hPort,data,1,&byteswritten,NULL);
CloseHandle(hPort); }