📄 tserial_event.cpp
字号:
/* */
/* */
/* ------------------------------------------------------------------ */
// Main wait function. Waiting for something to happen.
// This may be either the completion of a Read or a Write or
// the reception of modem events, Power Down, new Tx
//
status = WaitForMultipleObjects(SERIAL_SIGNAL_NBR, serial_events,
FALSE, INFINITE);
// processing answer to filter other failures
status = status - WAIT_OBJECT_0;
if ((status<0) || (status>=SERIAL_SIGNAL_NBR))
done=true; // error
else
{
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++ EVENT DISPATCHER ++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
switch(status)
{
/* ######################################################## */
case SIG_POWER_DOWN:
// receiving a POWER down signal. Stopping the thread
done = true;
break;
/* ######################################################## */
/* # # */
/* # # */
/* # RX # */
/* # # */
/* # # */
/* ######################################################## */
case SIG_READ_DONE:
// previous reading is finished
// I start a new one here
if (!rx_in_progress)
{
// locking reading
rx_in_progress = 1;
// starting a new read
success = (char) ReadFile(serial_handle,&rxBuffer,
max_rx_size,&read_nbr,&ovReader);
if (!success)
{
// failure
if(GetLastError() != ERROR_IO_PENDING )
{
// real failure => quiting
done = true;
#ifdef DEBUG_EVENTS
printf("Readfile error (not pending)\n");
#endif DEBUG_EVENTS
}
#ifdef DEBUG_EVENTS
else
printf("ReadFile pending\n");
#endif DEBUG_EVENTS
}
#ifdef DEBUG_EVENTS
else
{
// I make nothing here since the overlapped
// will be signaled anyway, so I'll make
// the processing there
printf("ReadFile immediate success\n");
}
#endif
}
break;
/* ######################################################## */
case SIG_READER:
// reading the result of the terminated read
//BOOL GetOverlappedResult(
// HANDLE hFile, // handle of file, pipe, or communications device
// LPOVERLAPPED lpOverlapped, // address of overlapped structure
// LPDWORD lpNumberOfBytesTransferred, // address of actual bytes count
// BOOL bWait // wait flag
// );
//
if (GetOverlappedResult(serial_handle, &ovReader,
&result_nbr, FALSE))
{
#ifdef DEBUG_EVENTS
printf("ReadFile => GetOverlappedResult done\n");
#endif DEBUG_EVENTS
// no error => OK
// Read operation completed successfully
ResetEvent(serial_events[SIG_READER]);
// Write operation completed successfully
received_size = result_nbr;
rx_in_progress = 0; // read has ended
// if incoming data, I process them
if ((result_nbr!=0) &&(manager!=0))
manager((uint32) this, SERIAL_DATA_ARRIVAL);
// I automatically restart a new read once the
// previous is completed.
//SetEvent(serial_events[SIG_READ_DONE]);
// BUG CORRECTION 02.06.22
}
else
{
// GetOverlapped didn't succeed !
// What's the reason ?
if(GetLastError()!= ERROR_IO_PENDING )
done = 1; // failure
}
break;
/* ######################################################## */
/* # # */
/* # # */
/* # TX # */
/* # # */
/* # # */
/* ######################################################## */
case SIG_DATA_TO_TX:
// Signal asserted that there is a new valid message
// in the "txBuffer" variable
// sending data to the port
success = (char) WriteFile(serial_handle, txBuffer, tx_size,
&result_nbr, &ovWriter);
if (!success)
{
// ouups, failure
if(GetLastError() != ERROR_IO_PENDING )
{
// real failure => quiting
done = true;
#ifdef DEBUG_EVENTS
printf("WriteFile error (not pending)\n");
#endif DEBUG_EVENTS
}
#ifdef DEBUG_EVENTS
else
printf("WriteFile pending\n");
#endif DEBUG_EVENTS
}
#ifdef DEBUG_EVENTS
else
{
// I make nothing here since the overlapped
// will be signaled anyway, so I'll make
// the processing there
printf("WriteFile immediate success\n");
}
#endif
break;
/* ######################################################## */
case SIG_WRITER:
// WriteFile has terminated
// checking the result of the operation
if (GetOverlappedResult(serial_handle, &ovWriter,
&result_nbr, FALSE))
{
// Write operation completed successfully
ResetEvent(serial_events[SIG_WRITER]);
// further write are now allowed
tx_in_progress = 0;
// telling it to the manager
if (manager!=0)
manager((uint32) this, SERIAL_DATA_SENT);
}
else
{
// GetOverlapped didn't succeed !
// What's the reason ?
if(GetLastError() != ERROR_IO_PENDING )
done = 1; // failure
}
break;
/* ######################################################## */
/* # # */
/* # # */
/* # MODEM_EVENTS EVENTS # */
/* # # */
/* # # */
/* ######################################################## */
case SIG_MODEM_CHECKED:
if ((!WaitCommEventInProgress) && check_modem)
// if no wait is in progress I start a new one
{
WaitCommEventInProgress=1;
success = (char) WaitCommEvent(serial_handle,&dwCommEvent,
&ovWaitEvent);
// reading one byte only to have immediate answer on each byte
if (!success)
{
// ouups, failure
if(GetLastError() != ERROR_IO_PENDING )
{
// real failure => quiting
done = true;
#ifdef DEBUG_EVENTS
printf("WaitCommEvent error (not pending)\n");
#endif DEBUG_EVENTS
}
#ifdef DEBUG_EVENTS
else
printf("WaitCommEvent pending\n");
#endif DEBUG_EVENTS
}
#ifdef DEBUG_EVENTS
else
{
// I make nothing here since the overlapped
// will be signaled anyway, so I'll make
// the processing there
printf("WaitCommEvent immediate success\n");
}
#endif
}
break;
/* ######################################################## */
case SIG_MODEM_EVENTS:
// reading the result of the terminated wait
if (GetOverlappedResult(serial_handle, &ovWaitEvent,
&result_nbr, FALSE))
{
// Wait operation completed successfully
ResetEvent(serial_events[SIG_MODEM_EVENTS]);
WaitCommEventInProgress = 0;
// if incoming data, I process them
OnEvent(dwCommEvent);
// automatically starting a new check
SetEvent(serial_events[SIG_MODEM_CHECKED]);
}
else
{
// GetOverlapped didn't succeed !
// What's the reason ?
if(GetLastError() != ERROR_IO_PENDING )
done = 1; // failure
}
break;
/* ######################################################## */
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
}
};
// --------------------- Disconnecting ----------------
ready = false;
if (serial_handle!=INVALID_HANDLE_VALUE)
CloseHandle(serial_handle);
serial_handle = INVALID_HANDLE_VALUE;
if (manager!=0)
manager((uint32) this, SERIAL_DISCONNECTED);
}
/* -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -