Click here to Skip to main content
15,922,325 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Displaying time for different time zones Pin
dennisV3-Jun-03 6:51
dennisV3-Jun-03 6:51 
GeneralRe: Displaying time for different time zones Pin
David Chamberlain3-Jun-03 7:10
David Chamberlain3-Jun-03 7:10 
GeneralRe: Displaying time for different time zones Pin
dennisV3-Jun-03 7:21
dennisV3-Jun-03 7:21 
Generalcreating a header blueprint that will be written to memory Pin
johnstonsk2-Jun-03 10:41
johnstonsk2-Jun-03 10:41 
General.rc files and encoding Pin
Jon Sagara2-Jun-03 10:09
Jon Sagara2-Jun-03 10:09 
GeneralCMenu question Pin
Anonymous2-Jun-03 9:59
Anonymous2-Jun-03 9:59 
GeneralRe: CMenu question Pin
Maximilien2-Jun-03 10:08
Maximilien2-Jun-03 10:08 
GeneralSerial Communication Problem Pin
GeneMcAulpin2-Jun-03 9:40
GeneMcAulpin2-Jun-03 9:40 
I am trying to write a C++ program to be used on Win2000 that talks through a RocketPort Serial Hub form Comport. The RocketPort connects to the computer via an ethernet connection and is addressed by virtual COM ports (currently COM3 - COM10). Basically, I will have a rack of devices with each device connected to one of the ports on the back of hub. Since the rack device use will be simple and controlled with function calls and development time is limited, I am using using non-overlapped I/O. From reading through the Platform SDK and Windows API documentation, non-overlapped I/O seems sufficient given how the devices will be used. The program is to write data to the devices in the rack and then receive and parse their response.

The basic structure of each of the program's functions is as follows:

1. Open the port using CreateFile(), and set control settings using
GetCommState() and SetCommState().

2. Write data to the rack device using WriteFile().

3. Receive data from the rack device using ReadFile() and parse the response.

4. Restore previous port settings and close the port.

The problem I am having is in using the Communication Time-outs. When I
don't use them, the program works fine. However, I need to use the
timeouts so that my program can report an error state if there is a
problem with communication (ex. one of the serial cables is unplugged). Without using the timeouts, the current program will
freeze on the ReadFile function if the computer is unable to communicate with the devices.

When I try to set the timeouts, using the COMMTIMEOUTS structure with
GetCommTimeouts() and SetCommTimeouts(), the WriteFile() function returns
as if the data was sent. However, the data isn't actually being sent.
Since the data is not actually being sent, the rack device doesn't respond and the program freezes waiting for the devices' response.

I determined the data is not being sent because (1) the port LED doesn't
flash, (2) the rack device doesn't respond, and (3) when I fill the
COMSTAT structure, using the ClearCommError() function after I called the
WriteFile() function, the cbOutQue member indicates that data is remaining to be transmitted even though the Microsoft Platform SDK explicitly states that this member is zero when non-overlapped operations are used.

Another interesting quirk, if I step through the program in debug mode,
the program actually works with the timeouts, but this is the only way it
will work when using the timeouts.

When I run this same program over the serial port on the back of the
computer (COM1), it works whether or not I use the timeouts. Its only when I try to use one of the ports on the RocketPort (COM3 - COM10), that I have this problem. Also, I've tried this by naming the ports both COM# and \\.\COM# (the naming convention that is suggested may be necessary in Comtrol's documentation) with both naming conventions producing the same results.


/**********************************************************************/
/* This function connects to and configures a COM port */
/**********************************************************************/
BOOL JM::OpenPort()
{
BOOL bSuccess;
dwLastError=ERROR_SUCCESS;
CString m_PortName = "COM3";

hFile = CreateFile(m_PortName,
GENERIC_READ | GENERIC_WRITE,
0, // comm devices must be opened w/exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);

if(hFile == INVALID_HANDLE_VALUE)
{
dwLastError = GetLastError();
return false;
}

bSuccess = GetCommTimeouts(hFile, &TimeOuts);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}
old_TimeOuts = TimeOuts;

memset(&TimeOuts, 0, sizeof(COMMTIMEOUTS));
TimeOuts.ReadIntervalTimeout = 0;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 0;
TimeOuts.WriteTotalTimeoutConstant = 0;

bSuccess = SetCommTimeouts(hFile, &TimeOuts);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}

bSuccess = GetCommState(hFile, &dcb);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}
old_dcb = dcb;

dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit

dcb.fBinary=true;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fRtsControl=RTS_CONTROL_ENABLE;
dcb.fParity=false;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fNull=false;
dcb.fAbortOnError=false;
dcb.fOutxCtsFlow=false;
dcb.fOutxDsrFlow=false;
dcb.fTXContinueOnXoff=false;
dcb.fDsrSensitivity=false;

bSuccess = SetCommState(hFile, &dcb);
if (!bSuccess)
{
dwLastError = GetLastError();
return false;
}

return true;
}



/****************************************************************/
/* This is a boiler-plate query function that is called by each */
/* specific query function. *mstrRX is the CString where the */
/* ASCII response is stored. mstrCommand is the command string */
/* that determines what information is retrieved */
/****************************************************************/
BOOL JM::Query(CString *mstrRX, CString mstrCommand)
{
BOOL bWrite_chk, bRead_chk, bSuccess, bPort_chk;
CString TX_str, Rx_temp_str;
DWORD dwBytes_Written, dwBytes_Read;
char *pcRead_Buff = new char;
int i;

bPort_chk = OpenPort(); //Connect to port and configure
if(!bPort_chk)
return false;

TX_str += mstrCommand;
bWrite_chk = WriteFile(hFile,TX_str,TX_str.GetLength(),
&dwBytes_Written,NULL);
if(!bWrite_chk)
{
dwLastError = GetLastError();
return false;
}

bPort_chk = ClearCommError(hFile,&dwCommError,&PortStatus);
if(!bPort_chk)
return false;

i=0;
bRead_chk = ReadFile(hFile,pcRead_Buff,1,&dwBytes_Read,NULL);

if(bRead_chk)
{
while ((pcRead_Buff[i] != '\r'))
{
bRead_chk = ReadFile(hFile,pcRead_Buff,1,&dwBytes_Read,NULL);
Rx_temp_str += *pcRead_Buff;
}
*mstrRX = Rx_temp_str;
}
else
{
dwLastError = GetLastError();
return false;
}

bSuccess = ClosePort();
if(!bSuccess)
return false;

delete pcRead_Buff;

return true;
}




Thanks in advance for any help that can be offered.
GeneralRe: Serial Communication Problem Pin
Roger Allen3-Jun-03 2:41
Roger Allen3-Jun-03 2:41 
QuestionHow to adapt window size to screen resolution Pin
gmlnd2-Jun-03 8:58
gmlnd2-Jun-03 8:58 
AnswerRe: How to adapt window size to screen resolution Pin
Dominik Reichl2-Jun-03 9:44
Dominik Reichl2-Jun-03 9:44 
GeneralRe: How to adapt window size to screen resolution Pin
gmlnd2-Jun-03 10:00
gmlnd2-Jun-03 10:00 
GeneralDisabling checkbox Pin
dorkshoe2-Jun-03 8:55
dorkshoe2-Jun-03 8:55 
GeneralRe: Disabling checkbox Pin
Dominik Reichl2-Jun-03 9:42
Dominik Reichl2-Jun-03 9:42 
GeneralRe: Disabling checkbox Pin
abc8762-Jun-03 9:56
abc8762-Jun-03 9:56 
GeneralRe: Disabling checkbox Pin
Dominik Reichl2-Jun-03 20:35
Dominik Reichl2-Jun-03 20:35 
GeneralRe: Disabling checkbox Pin
abc8762-Jun-03 9:50
abc8762-Jun-03 9:50 
QuestionHICON to HBITMAP? Pin
Dave_2-Jun-03 7:50
Dave_2-Jun-03 7:50 
AnswerRe: HICON to HBITMAP? Pin
Dominik Reichl2-Jun-03 8:43
Dominik Reichl2-Jun-03 8:43 
GeneralWinRTP and CE Pin
javigimenez2-Jun-03 6:53
javigimenez2-Jun-03 6:53 
GeneralProblem with SQLColumns in ODBC API Pin
insanely4202-Jun-03 6:48
insanely4202-Jun-03 6:48 
GeneralRe: Problem with SQLColumns in ODBC API Pin
David Crow2-Jun-03 7:17
David Crow2-Jun-03 7:17 
GeneralRe: Problem with SQLColumns in ODBC API Pin
insanely4202-Jun-03 8:13
insanely4202-Jun-03 8:13 
GeneralRe: Problem with SQLColumns in ODBC API Pin
Anonymous2-Jun-03 9:24
Anonymous2-Jun-03 9:24 
GeneralRe: Problem with SQLColumns in ODBC API Pin
basementman2-Jun-03 9:25
basementman2-Jun-03 9:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.