Serial RS232 thread interupt problems
I am trying to get two computers to communicate to each other via a RS232 communication link. Hyperterminal is running on computer A and my executable is running on computer B. My exec is a simple dialog based app. The user ticks a box on the dialog which invokes “OnCheck1() . Within OnCheck() I create a new thread that calls the function ThreadProc(), this does get called but I cannot enter the if statement when there is activity on hyperterminal. I can succesfully send info from my machine B to hyperterminal running on machine A using “OnSend”. However if I type into hyperterminal I am unable to detect it within my app. The following code has been entered in my dialog class.
Can any one pleazzze help, I been told not to use any of the various encapsulated serial classes floating around on the internet.
// this does its job ok
void CSerialPROTO1Dlg::OnSend()
{
UpdateData(TRUE);
// TODO: Add your control notification handler code here
unsigned long dwBytesWritten;
CString hod = m_message;
if(!WriteFile(hPort,static_cast<LPCTSTR>(hod),hod.GetLength(),&dwBytesWritten,NULL))
{
MessageBox("unable to write to port"
}
else
{
MessageBox(" written to port"
}
}
HANDLE hPort;
UINT ThreadProc(LPVOID param)
{
DWORD dwCommModemStatus;
::MessageBox((HWND) param ,"thread activated","thread",MB_OK);
SetCommMask(hPort,EV_RXCHAR);
EV_RXCHAR;
while(hPort!= INVALID_HANDLE_VALUE)
{
WaitCommEvent(hPort,&dwCommModemStatus,0);
SetCommMask(hPort,EV_RXCHAR);
// PROBLEMS PROBLEMS
if(dwCommModemStatus & EV_RXCHAR)
{
::MessageBox((HWND) param ,"activity on port ","thread",MB_OK);
}
}
return 0;
}
void CSerialPROTO1Dlg::OnCheck1()
{
UpdateData(TRUE);
hPort = CreateFile("COM1:" ,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0
,NULL);
if ( hPort == INVALID_HANDLE_VALUE)
{
MessageBox ("Unable to open the port"
//return FALSE;
}
else
{
MessageBox("port opened"
}
// configure the propertities of the port
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
GetCommState(hPort,&PortDCB);
PortDCB.BaudRate = 2400;
PortDCB.fBinary = TRUE;
PortDCB.fParity = TRUE;
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
PortDCB.fOutxCtsFlow = FALSE;
PortDCB.fOutxDsrFlow = FALSE;
PortDCB.fDsrSensitivity = FALSE;
PortDCB.fTXContinueOnXoff = TRUE;
PortDCB.fOutX = FALSE;
PortDCB.fInX = FALSE;
PortDCB.fErrorChar = FALSE;
PortDCB.fNull = FALSE;
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
PortDCB.fAbortOnError = FALSE;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;
if(!SetCommState(hPort,&PortDCB))
{
MessageBox("unable to configure the port"
}
else
{
MessageBox("port configured"
}
// set the time out for the transmission
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);
// change the commtimeouts structure settings
CommTimeouts.ReadIntervalTimeout= MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// set the time out parameters for the port
if(!SetCommTimeouts(hPort, &CommTimeouts))
{
MessageBox("unable to set the time out parameters"
this->EnableWindow(FALSE);
}
else
{
MessageBox("configured the time"
}
// line is now open to write data down the serial RS232 line
HWND hWnd = GetSafeHwnd();
AfxBeginThread(ThreadProc,hWnd,THREAD_PRIORITY_NO
RMAL);
}
I am trying to get two computers to communicate to each other via a RS232 communication link. Hyperterminal is running on computer A and my executable is running on computer B. My exec is a simple dialog based app. The user ticks a box on the dialog which invokes “OnCheck1() . Within OnCheck() I create a new thread that calls the function ThreadProc(), this does get called but I cannot enter the if statement when there is activity on hyperterminal. I can succesfully send info from my machine B to hyperterminal running on machine A using “OnSend”. However if I type into hyperterminal I am unable to detect it within my app. The following code has been entered in my dialog class.
Can any one pleazzze help, I been told not to use any of the various encapsulated serial classes floating around on the internet.
// this does its job ok
void CSerialPROTO1Dlg::OnSend()
{
UpdateData(TRUE);
// TODO: Add your control notification handler code here
unsigned long dwBytesWritten;
CString hod = m_message;
if(!WriteFile(hPort,static_cast<LPCTSTR>(hod),hod.GetLength(),&dwBytesWritten,NULL))
{
MessageBox("unable to write to port"
}
else
{
MessageBox(" written to port"
}
}
HANDLE hPort;
UINT ThreadProc(LPVOID param)
{
DWORD dwCommModemStatus;
::MessageBox((HWND) param ,"thread activated","thread",MB_OK);
SetCommMask(hPort,EV_RXCHAR);
EV_RXCHAR;
while(hPort!= INVALID_HANDLE_VALUE)
{
WaitCommEvent(hPort,&dwCommModemStatus,0);
SetCommMask(hPort,EV_RXCHAR);
// PROBLEMS PROBLEMS
if(dwCommModemStatus & EV_RXCHAR)
{
::MessageBox((HWND) param ,"activity on port ","thread",MB_OK);
}
}
return 0;
}
void CSerialPROTO1Dlg::OnCheck1()
{
UpdateData(TRUE);
hPort = CreateFile("COM1:" ,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0
,NULL);
if ( hPort == INVALID_HANDLE_VALUE)
{
MessageBox ("Unable to open the port"
//return FALSE;
}
else
{
MessageBox("port opened"
}
// configure the propertities of the port
DCB PortDCB;
PortDCB.DCBlength = sizeof(DCB);
GetCommState(hPort,&PortDCB);
PortDCB.BaudRate = 2400;
PortDCB.fBinary = TRUE;
PortDCB.fParity = TRUE;
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
PortDCB.fOutxCtsFlow = FALSE;
PortDCB.fOutxDsrFlow = FALSE;
PortDCB.fDsrSensitivity = FALSE;
PortDCB.fTXContinueOnXoff = TRUE;
PortDCB.fOutX = FALSE;
PortDCB.fInX = FALSE;
PortDCB.fErrorChar = FALSE;
PortDCB.fNull = FALSE;
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
PortDCB.fAbortOnError = FALSE;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;
if(!SetCommState(hPort,&PortDCB))
{
MessageBox("unable to configure the port"
}
else
{
MessageBox("port configured"
}
// set the time out for the transmission
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hPort, &CommTimeouts);
// change the commtimeouts structure settings
CommTimeouts.ReadIntervalTimeout= MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// set the time out parameters for the port
if(!SetCommTimeouts(hPort, &CommTimeouts))
{
MessageBox("unable to set the time out parameters"
this->EnableWindow(FALSE);
}
else
{
MessageBox("configured the time"
}
// line is now open to write data down the serial RS232 line
HWND hWnd = GetSafeHwnd();
AfxBeginThread(ThreadProc,hWnd,THREAD_PRIORITY_NO
RMAL);
}