📄 uartdrv.c
字号:
//*******************************************************************
//------------------------------------------------------------------------------
// File Name: uartdrv.c
// Date: 2001-12-3 pm 17:39
// Written by: xiaochun
// Decription: uart driver
//
//------------------------------------------------------------------------------
// Copyright: CNASIC
// All Rights Reserved
//
//-------------------------------------------------------------------
// Modification record
// 2002/01/23 longn_qi incorporate uart0 and uart1, and revise
//
//
//
//*******************************************************************
//*******************************************************************
#define _SIZE_T_DEFINED
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <kernel\ros33\ros33.h>
#include <sys\at.h>
#include <sys\systsk.h>
//#include <hardware\uart.h>//only for test uart
#include <uart.h>
#include <intdef.h>
#include <sys\sysdebug.h>
unsigned char UartCnfgValue[4] = { 4, 0, 0, 0 };
extern unsigned long UartID[UART_NUM][2];
//-------------------------------------------------------------------
typedef struct
{
HANDLE hPort;
int id;
char *name;
DCB portDCB;
HANDLE hInt;
int nInt;
HANDLE event;
OVERLAPPED overlapped;
}UARTDESCRIPTION;
//-------------------------------------------------------------------
//--- static variables -----------------------------------------------
static UARTDESCRIPTION UartPort[UART_NUM];
static int UartPortNum;
static unsigned long UartWaitFlag = EV_RXCHAR | EV_RLSD;// | EV_CTS | EV_DSR | EV_RING;
//static unsigned char sendBuf[2][MAX_SEND_BUF];
//static unsigned char receiveBuf[2][MAX_RECEIVE_BUF];
static unsigned long bytesSend[UART_NUM] = { 0, 0 };
static unsigned long bytesReceived[UART_NUM] = { 0, 0 };
static unsigned long bytesArriving[UART_NUM] = { 0, 0 };
static unsigned long bytesUnread[UART_NUM] = { 0, 0 };
static int recvDataSize[UART_NUM] = { MAX_RECEIVE_BUF-1, MAX_RECEIVE_BUF-1 };
static HANDLE hUartIntMonitor;
static CRITICAL_SECTION csUartInt[UART_NUM];
static DWORD BaudRate[] =
{
38400,
28800,
19200,
14400,
9600,
4800,
1200,
600,
300,
};
static BYTE DataWidth[] = { 8,7,6,5 };
//--- external variables ----------------------------------------------
DWORD UartEventFlag[UART_NUM] = { INTUART0_DR_FLG, INTUART1_DR_FLG };
//--------------------------------------------------------------------
static BOOL ConfigUart( int id );
static DWORD WINAPI UartInt( void *pid );
static DWORD WINAPI MonitorUartInt( void *p );
//*********************************************************************
//
//
//
//
//
//
//*********************************************************************
void InitUart( void )
{
FILE *fp;
char line[128], *s;
long lineCount = 0;
UARTDESCRIPTION *p;
const char *property[] = { "Name", // Port name
"BaudRate", // Current baud
"Binary", // Binary mode; no EOF check
"Parity", // Parity checking
"OutxCtsFlow", // CTS output flow control
"OutxDsrFlow", // DSR output flow control
"DtrControl", // DTR flow control type
"DsrSensitivity", // DSR sensitivity
"TXContinueOnXoff", // ON/OFF continues Tx
"OutX", // ON/OFF out flow control
"InX", // ON/OFF in flow control
"ErrorChar", // Error replacement
"Null", // Null stripping
"RtsControl", // RTS flow control
"AbortOnError", // Abort reads/writes on error
"ByteSize", // Number of bits/byte, 4-8
"Parity", // 0-4=no,odd,even,mark,space
"StopBits", // 0,1,2 = 1, 1.5, 2
""
};
short i;
// int nUartIntMonitor;
fp = fopen( "config\\device.ini", "r" );
if( fp == NULL )
{
dbgprintf( "Can not open file: device.ini !" );
return;
}
UartPortNum = 0;
memset( UartPort, 0, UART_NUM * sizeof( UARTDESCRIPTION ) );
while( feof( fp ) == 0 )
{
while( fgetc( fp ) != '[' );
if( feof( fp ) )
break;
fgets( line, 128, fp );
lineCount++;
if( strncmp( line, "UART]", 5 ) == 0 )
{
p = &UartPort[UartPortNum];
for( i = 0; *property[i] != '\0'; i++ )
{
fgets( line, 128, fp );
lineCount++;
if( strncmp( line, property[i], strlen( property[i] ) ) == 0 )
{
s = line + strlen( property[i] ) +3; // escape string " = "
switch( i )
{
case 0:
p->name = (char *)malloc( strlen( s ) +1 );
strcpy( p->name, s );
p->name[ strlen( s ) -1 ] = '\0';
break;
case 1:
p->portDCB.BaudRate = atoi( s );
break;
case 2:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fBinary = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fBinary = FALSE;
else
goto InitUartErrorHandle;
break;
case 3:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fParity = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fParity = FALSE;
else
goto InitUartErrorHandle;
break;
case 4:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fOutxCtsFlow = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fOutxCtsFlow = FALSE;
else
goto InitUartErrorHandle;
break;
case 5:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fOutxDsrFlow = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fOutxDsrFlow = FALSE;
else
goto InitUartErrorHandle;
break;
case 6:
if( strncmp( s, "ENABLE", 6 ) == 0 )
p->portDCB.fDtrControl = DTR_CONTROL_ENABLE;
else
if( strncmp( s, "DISABLE", 7 ) == 0 )
p->portDCB.fDtrControl = DTR_CONTROL_DISABLE;
else
goto InitUartErrorHandle;
break;
case 7:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fDsrSensitivity = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fDsrSensitivity = FALSE;
else
goto InitUartErrorHandle;
break;
case 8:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fTXContinueOnXoff = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fTXContinueOnXoff = FALSE;
else
goto InitUartErrorHandle;
break;
case 9:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fOutX = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fOutX = FALSE;
else
goto InitUartErrorHandle;
break;
case 10:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fInX = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fInX = FALSE;
else
goto InitUartErrorHandle;
break;
case 11:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fErrorChar = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fErrorChar = FALSE;
else
goto InitUartErrorHandle;
break;
case 12:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fNull = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fNull = FALSE;
else
goto InitUartErrorHandle;
break;
case 13:
if( strncmp( s, "ENABLE", 6 ) == 0 )
p->portDCB.fRtsControl = RTS_CONTROL_ENABLE;
else
if( strncmp( s, "DISABLE", 7 ) == 0 )
p->portDCB.fRtsControl = RTS_CONTROL_DISABLE;
else
goto InitUartErrorHandle;
break;
case 14:
if( strncmp( s, "TRUE", 4 ) == 0 )
p->portDCB.fAbortOnError = TRUE;
else
if( strncmp( s, "FALSE", 5 ) == 0 )
p->portDCB.fAbortOnError = FALSE;
else
goto InitUartErrorHandle;
break;
case 15:
p->portDCB.ByteSize = atoi( s );
break;
case 16:
p->portDCB.Parity = atoi( s );
break;
case 17:
p->portDCB.StopBits = atoi( s );
break;
default:
goto InitUartErrorHandle;
}
}
else
goto InitUartErrorHandle;
}
UartPortNum++;
}
}
if( UartPortNum != 0 )
{
for( i = 0; i < UartPortNum; i++ )
{
ConfigUart( i );
}
// hUartIntMonitor = CreateThread(
// NULL,
// 0,
// MonitorUartInt,
// NULL,
// 0,
// &nUartIntMonitor );
// if( hUartIntMonitor == 0 )
// return 0;
return;
}
InitUartErrorHandle:
dbgoutput( " Incorrect file: device.ini at line %ld!", lineCount );
return;
}
//*********************************************************************
//
//
//
//
//
//
//*********************************************************************
BOOL ConfigUart( int id )
{
UARTDESCRIPTION *p = &UartPort[id];
DCB PortDCB;
// COMMTIMEOUTS CommTimeouts;
DWORD error;
p->id = id;
p->hPort = CreateFile ( p->name, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
FILE_FLAG_OVERLAPPED, // Port attributes
NULL); // Handle to port with attribute
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof( DCB );
// Get the default port setting information.
GetCommState( p->hPort, &PortDCB);
// Change the DCB structure settings.
p->portDCB.DCBlength = PortDCB.DCBlength;
p->portDCB.EofChar = PortDCB.EofChar;
p->portDCB.ErrorChar = PortDCB.ErrorChar;
p->portDCB.EvtChar = PortDCB.EvtChar;
p->portDCB.fDummy2 = PortDCB.fDummy2;
p->portDCB.fErrorChar = PortDCB.fErrorChar;
p->portDCB.fOutX = PortDCB.fOutX;
p->portDCB.fTXContinueOnXoff = PortDCB.fTXContinueOnXoff;
p->portDCB.XoffChar = PortDCB.XoffChar;
p->portDCB.XoffLim = PortDCB.XoffLim;
p->portDCB.XonChar = PortDCB.XonChar;
p->portDCB.XonLim = PortDCB.XonLim;
// Change the DCB structure settings.
/* PortDCB.BaudRate = 9600; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -