Writing a Serial Communication Library for Windows - Write and Read Timeouts
(Page 5 of 6 )
DWORD ReadIntervalTimeout
Specifies the maximum time, in milliseconds, allowed to elapse between the arrival of two characters on the communications line. During a ReadFile() operation, the time period begins when the first character is received. If the interval between the arrival of any two characters exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used. A value of MAXDWORD, combined with zero values for both the ReadTotalTimeout constant and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the characters that have already been received, even if no characters have been received.
ReadTotalTimeoutConstant
Specifies the constant, in milliseconds, used to calculate the total time-out period for read operations. For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes. A value of zero for both the ReadTotalTimeoutMultiplier and ReadTotalTimeoutConstant members indicates that total time-outs are not used for read operations.
ReadTotalTimeoutMultiplier
Specifies the multiplier, in milliseconds, used to calculate the total time-out period for read operations. For each read operation, this value is multiplied by the requested number of bytes to be read.
WriteTotalTimeoutConstant
Specifies the constant, in milliseconds, used to calculate the total time-out period for write operations. For each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier member and the number of bytes to be written.
WriteTotalTimeoutMultiplier
Specifies the multiplier, in milliseconds, used to calculate the total time-out period for write operations. For each write operation, this value is multiplied by the number of bytes to be written.
A value of zero for both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members indicates that total time-outs are not used for write operations.
For example, if your device transmits a block of characters with a max. timeout value of 500 ms between each characters, you can set the time-out function as SetCommunicationTimeouts(0,500,0,0,0);. If the function succeeds the return value is true otherwise false.
BOOL CSerialComm::WriteByte(BYTE by)
{
m_nBytesWritten=0;
if(WriteFile(m_hComm,&by,1,&m_nBytesWritten,NULL)==0)
{
return FALSE;
}
else
{
return TRUE;
}
}
The WriteByte() member function writes the data byte to the communication port. The parameter to be passed to this function is the byte to be transmitted. You can call this function repeatedly in a loop with your data to be written placed in an array. Each time you send characters, increment the index of the array and call WriteByte() till all data bytes are transmitted.
If the function succeeds the return value is true otherwise false.
BOOL CSerialComm::ReadByte(BYTE& by)
{
BYTE byResByte;
by=0;
DWORD dwBytesTxD=0;
if (ReadFile (m_hComm, &byResByte, 1, &dwBytesTxD, 0))
{
if (dwBytesTxD == 1)
{
by=byResByte;
return TRUE;
}
}
return FALSE;
}
The ReadByte() member function reads data bytes from the communication port. The parameter to be passed to this function is the address of the variable in which the received data byte is to be stored. You can call this function repeatedly in a loop with your received data moved to an array. Each time you receive characters, increment the index of the array and call ReadByte() till all data bytes are received. If you know exactly the no. of response bytes from the external device you can call the ReadByte() function in a loop till all characters are received or a time out occurs. Sometimes you may not be able to predict the no. of response bytes from the external device. In that case call ReadByte file repeatedly till you get a time out and if the character received previously is a character representing end of transmission in your protocol format, the communication process is successfully completed. For example, for a device following 3964 the end of transmission is 'ETX' character. So use the ReadByte() function properly in accordance with the protocol supported by your external device.
If ReadByte() succeeds the return value is true and the received Byte will be stored in the location pointed by the address of ReadByte() 's parameter. If a timeout occurs the return value will be false.
void CSerialComm::ClosePort()
{
CloseHandle (m_hComm);
return;
}
The ClosePort() member function closes a communication Port which is already in an Open state.
Next: How To Use 'CSerialComm' Class >>
More Code Examples Articles
More By Digvijay Chauhan