Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having difficulty making use of an RS-422 port in a Windows application. The Microsoft documentation suggests that for a given set of parameters, the initialization sequences for an RS-232 port and an RS-422 port are identical. But the following code:

C++
if ((hComm= ::CreateFile("COM3",
		    GENERIC_READ|GENERIC_WRITE,
		    0,
		    NULL,
		    OPEN_EXISTING,
		    0,
		    NULL)) == (LPVOID)0xFFFFFFFF)
{
    sts= ::GetLastError();
}
if (!::SetupComm(hComm, 1024, 1024))
{
    lastError= ::GetLastError();
    throw new CUserException;
}
::GetCommState(hComm, &dcb);
::BuildCommDCB("BAUD=19200 PARITY=N DATA=8 STOP=1 to=off xon=off odsr=off octs=off dtr=off rts=off idsr=off",
		&dcb);
if (!::SetCommState(hComm, &dcb))
{
    lastError= ::GetLastError();
    throw new CUserException;
}


...throws an exception after the ::SetCommState call when used on an RS-422 port, which it does not do when used on an RS-232 port.

Help! I'm under severe time pressure on this one.
Posted
Updated 24-Jan-12 1:05am
v3
Comments
Richard MacCutchan 24-Jan-12 6:56am    
What exception does it throw? Also if the call to CreateFile() fails you are ignoring the error.
Fran Porretto 24-Jan-12 6:58am    
It throws the CUserException I set up for the failure of the ::SetCommState call.

No error occurs prior to that point. (I'm not really ignoring the possibility of a ::CreateFile failure. No error occurs there, nor at the ::SetupComm call.)
Richard MacCutchan 24-Jan-12 7:02am    
Yes, but what is the error code produced by the system? Also if CreateFile() fails you are just continuing, the same with GetCommState() and BuildCommDCB(). Incidentally in the latter two calls you are not passing the address of dcb as you should so the calls probably fail.
Fran Porretto 24-Jan-12 7:04am    
Sorry! It really is syntactically correct; I just mistyped it. I'll correct it. (It's very early in the morning here.)

The error from ::SetCommState() is 995, which Microsoft translates as "Either a thread abort or an application exit has occurred." This DOES NOT HAPPEN when I use an RS-232 port with identical code.
Richard MacCutchan 24-Jan-12 7:07am    
Having looked at the sample code in this link I suspect that your call to BuildCommDCB() may not be quite correct. You could try following the sample code to see if that makes any difference.

1 solution

Fran Porretto wrote:
::GetCommState(hComm, dcb);
The above line is wrong. You should use the address of the DCB struct:
C++
::GetCommState(hComm, &dcb);


SetCommState shouldn't raise any exception (unless dcb.DCBlength is wrong, of course).
What kind of error did you get? Could you please report it, exactly, here?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900