📄 uartdrv.c
字号:
*/
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState( p->hPort, &p->portDCB ) )
{
// Could not configure the serial port.
dbgprintf( "Unable to configure the serial port" );
error = GetLastError ();
return FALSE;
}
if( ( p->event = CreateEvent(
NULL, // no security attributes
FALSE, // manual-reset event
FALSE, // initial state is nonsignaled
NULL // object name
) ) == NULL )
return FALSE;
p->overlapped.Internal = 0;
p->overlapped.InternalHigh = 0;
p->overlapped.Offset = 0;
p->overlapped.OffsetHigh = 0;
p->overlapped.hEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
return TRUE;
}
//*********************************************************************
//
//
//
//
//
//
//*********************************************************************
DWORD WINAPI UartInt( void *pdesp )
{
UARTDESCRIPTION *p = (UARTDESCRIPTION *)pdesp;
int id = p->id;
DWORD commModemStatus;
// DWORD waitResult;
// COMSTAT commState;
// Specify a set of events to be monitored for the port.
SetCommMask( p->hPort, UartWaitFlag );
InitializeCriticalSection( &csUartInt[id] );
while( p->hPort != INVALID_HANDLE_VALUE)
{
// Wait for an event to occur for the port.
WaitCommEvent( p->hPort, &commModemStatus, 0 );
SetCommMask( p->hPort, UartWaitFlag );
if( commModemStatus & EV_RXCHAR )
{
// SetEvent( p->event );
// ResumeThread( hUartIntMonitor );
// EnterCriticalSection( &csUartInt[id] );
// ClearCommError( UartPort[id].hPort, &error, &commState );
// dbgoutput( "arrive: %d bytes", bytesArriving[id] );
// dbgoutput( "total unread: %d bytes", bytesReceived[id] );
// LeaveCriticalSection( &csUartInt[id] );
iset_flg( UART_EVENT, UartEventFlag[id] );
Sleep( 100 );
// iset_flg( UART_EVENT, UartEventFlag[id] );
// ClearCommError( UartPort[id].hPort, &error, &commState );
// if( commState.cbInQue == bytesReceived[id] )
// SetCommMask( p->hPort, UartWaitFlag );
}
}
return 0;
}
//*********************************************************************
//
//
//
//
//
//
//*********************************************************************
int WriteUartSendBuf( short id, unsigned char* pData, unsigned long byteCounts, long timeout )
{
UARTDESCRIPTION *p = &UartPort[id];
int error;
DWORD bytesWritten;
error = WriteFile ( p->hPort, // Port handle
pData, // Pointer to the data to write
byteCounts, // Number of bytes to write
&bytesWritten, // Pointer to the number of bytes written
&p->overlapped ); // Must be NULL
return bytesWritten;
}
//***************************************************************************
//
//
//
//
//**************************************************************************
int ReadUartReceiveBuf( short id, unsigned char* pData, unsigned long byteCounts, long timeout )
{
UARTDESCRIPTION *p = &UartPort[id];
int error;
unsigned long bytesRead, bytesRcv, tempCount;
unsigned char *pbuf = pData;
unsigned int flgptn;
COMSTAT commState;
for(;byteCounts != 0;)
{
ClearCommError( UartPort[id].hPort, &error, &commState );
// bytesArriving[id] = commState.cbInQue - bytesUnread[id];
bytesRcv = commState.cbInQue;
while( bytesRcv == 0 )
{
if( timeout == 0 )
return ( pbuf - pData );
if( twai_flg(&flgptn, UART_EVENT, UartEventFlag[id], TWF_CLR, timeout ) == E_TMOUT )
return ( pbuf - pData );
ClearCommError( UartPort[id].hPort, &error, &commState );
// bytesArriving[id] = commState.cbInQue - bytesReceived[id];
bytesRcv = commState.cbInQue;
}
while( bytesRcv != 0 && byteCounts != 0 )
{
tempCount = bytesRcv < byteCounts ? bytesRcv : byteCounts;
error = ReadFile ( p->hPort, // Port handle
pbuf, // Pointer to the data to write
tempCount, // Number of bytes to write
&bytesRead, // Pointer to the number of bytes written
&p->overlapped ); // Must be NULL
bytesRcv -= bytesRead;
byteCounts -= bytesRead;
if( bytesUnread[id] >= bytesRead )
bytesUnread[id] -= bytesRead;
else
bytesUnread[id] = 0;
pbuf += bytesRead;
}
}
return ( pbuf - pData );
}
//***************************************************************************
//
//
//
//
//**************************************************************************
void SetUartRecvDataSize( short id, int datasize )
{
if( datasize < MAX_RECEIVE_BUF )
recvDataSize[id] = datasize;
return;
}
//***************************************************************************
//
//
//
//
//**************************************************************************
//void SetUartTimeout( int id, unsigned long timeout )
//***************************************************************************
//
//
//
//
//**************************************************************************
void EnableUartInt( short id )
{
int i;
UARTDESCRIPTION *p;
for( i = 0; i < UART_NUM; i++ )
{
if( id & ( 1 << i ) )
{
p = &UartPort[i];
if( p->hPort != 0 && p->hInt == 0 )
{
PurgeComm( p->hPort, PURGE_RXCLEAR );
if( ( p->hInt = CreateThread(
NULL,
0,
UartInt,
p,
0,
&p->nInt
) ) == NULL )
return;
}
}
}
// bytesReceived[id] = 0;
// bytesArriving[id] = 0;
return;
}
//***************************************************************************
//
//
//
//
//**************************************************************************
void DisableUartInt( short id )
{
int i;
UARTDESCRIPTION *p;
for( i = 0; i < UART_NUM; i++ )
{
if( id & ( 1 << i ) )
{
p = &UartPort[i];
if( p->hPort != 0 && p->hInt != 0 )
{
TerminateThread( p->hInt, -1 );
p->hInt = 0;
}
}
}
return;
}
//***************************************************************************
//
//
//
//
//**************************************************************************
void FreeUart( void )
{
UARTDESCRIPTION *p;
int i;
for( i = 0; i < UartPortNum; i++ )
{
p = &UartPort[i];
TerminateThread( p->hInt, -1 );
bytesReceived[i] = 0;
bytesArriving[i] = 0;
}
return;
}
//***************************************************************************
//
//
//
//
//**************************************************************************
int CheckUartRecvBuf( short id, unsigned long *num )
{
COMSTAT commState;
int error;
ClearCommError( UartPort[id].hPort, &error, &commState );
*num = (unsigned short)( commState.cbInQue - bytesUnread[id] );
bytesUnread[id] = commState.cbInQue;
// EnterCriticalSection( &csUartInt[id] );
// *num = (unsigned short)bytesArriving[id];
// *num = (unsigned short)bytesReceived[id];
// LeaveCriticalSection( &csUartInt[id] );
if( *num == 0 )
return BUF_EMPTY;
else
return BUF_NORMAL;
}
//***************************************************************************
//
//
//
//
//**************************************************************************
//void PrereadUartRecvBuf( int id, int byteCounts )
//{
// bytesReceived[id] -= byteCounts;
// return;
//}
//***************************************************************************
//
//
//
//
//**************************************************************************
DWORD WINAPI MonitorUartInt( void *p )
{
DWORD waitResult, error;
HANDLE uartEvents[UART_NUM];
int i, eventid;
COMSTAT commState;
for( ; ; )
{
for( i = 0; i < UartPortNum; i++ )
uartEvents[i] = UartPort[i].event;
waitResult = WaitForMultipleObjects(
UartPortNum, // number of handles in array
uartEvents, // array of read-event handles
FALSE, // wait until all are signaled
INFINITE); // indefinite wait
eventid = waitResult - WAIT_OBJECT_0;
if( eventid >= 0 && eventid < UartPortNum )
{
ClearCommError( UartPort[eventid].hPort, &error, &commState );
// bytesArriving[eventid] = commState.cbInQue - bytesReceived[eventid];
// bytesReceived[eventid] += bytesArriving[eventid];
// dbgoutput( " get %ld", count );
set_flg( UART_EVENT, UartEventFlag[ eventid ] );
// ResetEvent( uartEvents[ eventid ] );
}
else
{
// dbgoutput("Wait error: %d\n", GetLastError());
break;
}
// SuspendThread( hUartIntMonitor );
}
return 0;
}
void ChangeUartConfig( short id )
{
UARTDESCRIPTION *p;
DCB PortDCB;
DWORD i, error;
for( i = 0; i < UART_NUM; i++ )
{
if( UartID[i][1] == (DWORD)id )
break;
}
p = &UartPort[i];
if( p->hPort == 0 )
return;
UartCnfgValue;
GetCommState( p->hPort, &PortDCB);
memcpy( &p->portDCB, &PortDCB, sizeof( DCB ) );
// Change the DCB structure settings.
p->portDCB.BaudRate = BaudRate[UartCnfgValue[0]]; // Current baud
p->portDCB.ByteSize = DataWidth[UartCnfgValue[1]]; // Number of bits/byte, 4-8
p->portDCB.Parity = UartCnfgValue[2]; // 0-4=no,odd,even,mark,space
p->portDCB.StopBits = UartCnfgValue[3]; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState( p->hPort, &p->portDCB ) )
{
// Could not configure the serial port.
dbgprintf( "Unable to configure the serial port" );
error = GetLastError ();
return;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -