本文整理汇总了C++中TimeOut::ElapsedTime_ms方法的典型用法代码示例。如果您正苦于以下问题:C++ TimeOut::ElapsedTime_ms方法的具体用法?C++ TimeOut::ElapsedTime_ms怎么用?C++ TimeOut::ElapsedTime_ms使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TimeOut
的用法示例。
在下文中一共展示了TimeOut::ElapsedTime_ms方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readBytes
/*!
\brief Read an array of bytes from the serial device (with timeout)
\param Buffer : array of bytes read from the serial device
\param MaxNbBytes : maximum allowed number of bytes read
\param TimeOut_ms : delay of timeout before giving up the reading
\return >=0 return the number of bytes read before timeout or
requested data is completed
\return -1 error while setting the Timeout
\return -2 error while reading the byte
*/
int rOc_serial::readBytes (void *Buffer,unsigned int MaxNbBytes,unsigned int TimeOut_ms, unsigned int SleepDuration_us)
{
#if defined (_WIN32) || defined(_WIN64)
DWORD dwBytesRead = 0;
timeouts.ReadTotalTimeoutConstant=(DWORD)TimeOut_ms; // Set the TimeOut
if(!SetCommTimeouts(hSerial, &timeouts)) // Write the parameters
return -1; // Error while writting the parameters
if(!ReadFile(hSerial,Buffer,(DWORD)MaxNbBytes,&dwBytesRead, NULL)) // Read the bytes from the serial device
return -2; // Error while reading the byte
return dwBytesRead;
#endif
#ifdef __linux__
TimeOut Timer; // Timer used for timeout
Timer.InitTimer(); // Initialise the timer
unsigned int NbByteRead=0;
while (Timer.ElapsedTime_ms()<TimeOut_ms || TimeOut_ms==0) // While Timeout is not reached
{
unsigned char* Ptr=(unsigned char*)Buffer+NbByteRead; // Compute the position of the current byte
int Ret=read(fd,(void*)Ptr,MaxNbBytes-NbByteRead); // Try to read a byte on the device
if (Ret==-1) return -2; // Error while reading
// One or several byte(s) has been read on the device
if (Ret>0)
{
NbByteRead+=Ret; // Increase the number of read bytes
if (NbByteRead>=MaxNbBytes) // Success : bytes has been read
return NbByteRead;
}
// Suspend the loop to avoid charging the CPU
usleep (SleepDuration_us);
}
return NbByteRead; // Timeout reached, return the number of bytes read
#endif
}
示例2: readChar
/*!
\brief Wait for a byte from the serial device and return the data read
\param pByte : data read on the serial device
\param TimeOut_ms : delay of timeout before giving up the reading
If set to zero, timeout is disable (Optional)
\return 1 success
\return 0 Timeout reached
\return -1 error while setting the Timeout
\return -2 error while reading the byte
*/
char rOc_serial::readChar(char *pByte,unsigned int TimeOut_ms)
{
#if defined (_WIN32) || defined(_WIN64)
DWORD dwBytesRead = 0;
timeouts.ReadTotalTimeoutConstant=TimeOut_ms; // Set the TimeOut
if(!SetCommTimeouts(hSerial, &timeouts)) // Write the parameters
return -1; // Error while writting the parameters
if(!ReadFile(hSerial,pByte, 1, &dwBytesRead, NULL)) // Read the byte
return -2; // Error while reading the byte
if (dwBytesRead==0) return 0; // Return 1 if the timeout is reached
return 1; // Success
#endif
#ifdef __linux__
TimeOut Timer; // Timer used for timeout
Timer.InitTimer(); // Initialise the timer
while (Timer.ElapsedTime_ms()<TimeOut_ms || TimeOut_ms==0) // While Timeout is not reached
{
switch (read(fd,pByte,1)) { // Try to read a byte on the device
case 1 : return 1; // Read successfull
case -1 : return -2; // Error while reading
}
}
return 0;
#endif
}
示例3: ReadChar
/*!
\brief Wait for a byte from the serial device and return the data read
\param pByte : data read on the serial device
\param TimeOut_ms : delay of timeout before giving up the reading
If set to zero, timeout is disable (Optional)
\return 1 success
\return 0 Timeout reached
\return -1 error while setting the Timeout
\return -2 error while reading the byte
*/
char Serial::ReadChar(char *pByte,int TimeOut_ms)
{
TimeOut Timer; // Timer used for timeout
Timer.InitTimer(); // Initialise the timer
while (Timer.ElapsedTime_ms()<TimeOut_ms || TimeOut_ms==0) // While Timeout is not reached
{
switch (read(fd,pByte,1)) { // Try to read a byte on the device
case 1 : return 1; // Read successfull
case -1 : return -2; // Error while reading
}
}
return 0;
}
示例4: readString
/*!
\brief Read a string from the serial device (with timeout)
\param String : string read on the serial device
\param FinalChar : final char of the string
\param MaxNbBytes : maximum allowed number of bytes read
\param TimeOut_ms : delay of timeout before giving up the reading (optional)
\return >0 success, return the number of bytes read
\return 0 timeout is reached
\return -1 error while setting the Timeout
\return -2 error while reading the byte
\return -3 MaxNbBytes is reached
*/
int rOc_serial::readString(char *String,char FinalChar,unsigned int MaxNbBytes,unsigned int TimeOut_ms)
{
if (TimeOut_ms==0)
return readStringNoTimeOut(String,FinalChar,MaxNbBytes);
unsigned int NbBytes=0; // Number of bytes read
char ret; // Returned value from Read
TimeOut Timer; // Timer used for timeout
long int TimeOutParam;
Timer.InitTimer(); // Initialize the timer
while (NbBytes<MaxNbBytes) // While the buffer is not full
{ // Read a byte with the restant time
TimeOutParam=TimeOut_ms-Timer.ElapsedTime_ms(); // Compute the TimeOut for the call of ReadChar
if (TimeOutParam>0) // If the parameter is higher than zero
{
ret=readChar(&String[NbBytes],TimeOutParam); // Wait for a byte on the serial link
if (ret==1) // If a byte has been read
{
if (String[NbBytes]==FinalChar) // Check if it is the final char
{
String [++NbBytes]=0; // Yes : add the end character 0
return NbBytes; // Return the number of bytes read
}
NbBytes++; // If not, just increase the number of bytes read
}
if (ret<0) return ret; // Error while reading : return the error number
}
if (Timer.ElapsedTime_ms()>TimeOut_ms) { // Timeout is reached
String[NbBytes]=0; // Add the end caracter
return 0; // Return 0
}
}
return -3; // Buffer is full : return -3
}
示例5: Read
/*!
\brief Read an array of bytes from the serial device (with timeout)
\param Buffer : array of bytes read from the serial device
\param MaxNbBytes : maximum allowed number of bytes read
\param TimeOut_ms : delay of timeout before giving up the reading
\return 1 success, return the number of bytes read
\return 0 Timeout reached
\return -1 error while setting the Timeout
\return -2 error while reading the byte
*/
int Serial::Read (void *Buffer,unsigned int MaxNbBytes,unsigned int TimeOut_ms)
{
TimeOut Timer; // Timer used for timeout
Timer.InitTimer(); // Initialise the timer
unsigned int NbByteRead=0;
while (Timer.ElapsedTime_ms()<TimeOut_ms || TimeOut_ms==0) // While Timeout is not reached
{
unsigned char* Ptr=(unsigned char*)Buffer+NbByteRead; // Compute the position of the current byte
int Ret=read(fd,(void*)Ptr,MaxNbBytes-NbByteRead); // Try to read a byte on the device
if (Ret==-1) return -2; // Error while reading
if (Ret>0) { // One or several byte(s) has been read on the device
NbByteRead+=Ret; // Increase the number of read bytes
if (NbByteRead>=MaxNbBytes) // Success : bytes has been read
return 1;
}
}
return 0; // Timeout reached, return 0
}