Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial RS232 thread interupt problems

Status
Not open for further replies.

ugly

Programmer
Jul 5, 2002
70
GB
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(&quot;unable to write to port&quot;);
}
else
{
MessageBox(&quot; written to port&quot;);
}
}


HANDLE hPort;

UINT ThreadProc(LPVOID param)
{

DWORD dwCommModemStatus;
::MessageBox((HWND) param ,&quot;thread activated&quot;,&quot;thread&quot;,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 ,&quot;activity on port &quot;,&quot;thread&quot;,MB_OK);


}

}



return 0;

}


void CSerialPROTO1Dlg::OnCheck1()
{

UpdateData(TRUE);

hPort = CreateFile(&quot;COM1:&quot; ,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0
,NULL);
if ( hPort == INVALID_HANDLE_VALUE)
{

MessageBox (&quot;Unable to open the port&quot;);
//return FALSE;
}
else
{
MessageBox(&quot;port opened&quot;);
}

// 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(&quot;unable to configure the port&quot;);
}
else
{
MessageBox(&quot;port configured&quot;);
}


// 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(&quot;unable to set the time out parameters&quot;);
this->EnableWindow(FALSE);
}

else
{
MessageBox(&quot;configured the time&quot;);
}
// line is now open to write data down the serial RS232 line

HWND hWnd = GetSafeHwnd();
AfxBeginThread(ThreadProc,hWnd,THREAD_PRIORITY_NO
RMAL);


}

 
>> Can any one pleazzze help, I been told not to use any of
>> the various encapsulated serial classes floating
>> around on the internet.

Who told you and did they explain why? (By who I mean do they know what they are talking about)

So it's been many years so this may not help at all... but try reversing these lines

>> WaitCommEvent(hPort,&dwCommModemStatus,0);
>> SetCommMask(hPort,EV_RXCHAR);


SetCommMask(hPort,EV_RXCHAR);
WaitCommEvent(hPort,&dwCommModemStatus,0);


Good luck
-pete
 
Be sure the flow control (RTS-CTS, XOn-XOff, None) is set properly on both computers. Also, if using RTS-CTS, be sure that the CTS signal is on. Likewise, if using XON-XOFF, be sure XON has been sent.

I would highly recommend the Serial Library version 1.03 found on The Code Project. I used it recently to connect to an Optical Mark Reader (ie bubble scanner). At one point, I adapted the nine pin cable back to a twenty-five pin cable so I could read the wire signals on an RS232 Serial Breakout Box I bought fifteen years ago.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top