/***********************************************************************************************
initialSerial
功能:串口初始化
参数:无
返回:无
***********************************************************************************************/
void initialSerial() //串口初始化
{
char szComParams[50];
DCB dcb;
char *m_com; char *m_baud; char *m_jiaoyan;
m_com="Com1";
m_baud="1200";
m_jiaoyan="E";
COMMTIMEOUTS CommTimeOuts;
m_hIDComDev = NULL;
m_hIDComDev = CreateFile(m_com, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //打开串口
if(m_hIDComDev==INVALID_HANDLE_VALUE)
{
AfxMessageBox("打开串口错误0,请检查!");
goto endd;
}
if(m_hIDComDev ==(HANDLE) -1)
{
AfxMessageBox("打开串口错误,请检查!");
goto endd;
}
SetCommTimeouts(m_hIDComDev, &CommTimeOuts); //串口超时配置
CommTimeOuts. ReadIntervalTimeout="0xFFFFFFFF";
CommTimeOuts. ReadTotalTimeoutMultiplier = 0;
CommTimeOuts. ReadTotalTimeoutConstant =5000;
CommTimeOuts. WriteTotalTimeoutMultiplier = 0;
CommTimeOuts. WriteTotalTimeoutConstant = 5000;
PurgeComm(m_hIDComDev, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR ) ;
m_com="Com1:38400,E,8,1";
wsprintf(szComParams, m_com); //设置串口参数
dcb. DCBlength = sizeof(DCB);
GetCommState(m_hIDComDev, &dcb);//
int baud;
baud = atoi(m_baud);
dcb. BaudRate = baud; //设置波特率
dcb. ByteSize= 8; //设置校验字节
if ((!SetCommState(m_hIDComDev, &dcb))||(!SetupComm(m_hIDComDev,10000,10000)))//设置串口和收发缓冲器的大小
{
DWORD dwError = GetLastError();
CloseHandle(m_hIDComDev);
}
PurgeComm(m_hIDComDev,PURGE_RXCLEAR|PURGE_TXCLEAR|PURGE_TXABORT|PURGE_RXABORT);//清收发缓冲器
endd:
;
}
/************************************************************************************************
SendData
功能:发送数据给串口
参数: buff发送的数据
send_length 长度
返回: 成功 1 失败 0
************************************************************************************************/
DWORD SendData( unsigned char buff[],int send_length) //发送数据
{
int t;
DWORD dwBytesWritten;
if(!WriteFile(m_hIDComDev,buff,send_length,&dwBytesWritten,NULL))
{
return 0;
}
for (t=0;t fprintf(stdout," %2X ",buff[t]);
printf("\n");
PurgeComm(m_hIDComDev,PURGE_TXCLEAR); //清发送缓冲区
return 1;
}
/***********************************************************************************************
ReadData
功能:从串口读取数据
参数:rebuff 收到的数据 dwBytesRead 要收到的长度 delay 延时
***********************************************************************************************/
DWORD ReadData(unsigned char rebuff [],DWORD dwBytesRead ) //读取数据
{
DWORD dwErrorFlags;
COMSTAT stat;
if(delay>0)
ClearCommError(m_hIDComDev,&dwErrorFlags,&stat);
if(stat.cbInQue <= 0) //"stat.cbInQue" is bytes in input buffer
return 0;
dwBytesRead = min(dwBytesRead,(DWORD)stat.cbInQue); //获取字符个数
if(!ReadFile(m_hIDComDev,rebuff,dwBytesRead,&dwBytesRead,NULL)) //整体读入
{
return 0;
}
return dwBytesRead;
}
下面三个函数中所用到结构本OR函数
COMMTIMEOUTS
The COMMTIMEOUTS structure is used in the SetCommTimeouts and GetCommTimeouts functions to set and query the time-out parameters for a communications device. The parameters determine the behavior of ReadFile, WriteFile, ReadFileEx, and WriteFileEx operations on the device.
typedef struct _COMMTIMEOUTS {
DWORD ReadIntervalTimeout;
DWORD ReadTotalTimeoutMultiplier;
DWORD ReadTotalTimeoutConstant;
DWORD WriteTotalTimeoutMultiplier;
DWORD WriteTotalTimeoutConstant;
} COMMTIMEOUTS,*LPCOMMTIMEOUTS;
Members
- 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 ReadTotalTimeoutConstant 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.
- 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.
- 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.
- 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.
- 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.
A value of zero for both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members indicates that total time-outs are not used for write operations.
PurgeComm
The PurgeComm function can discard all characters from the output or input buffer of a specified communications resource. It can also terminate pending read or write operations on the resource.
BOOL PurgeComm(
HANDLE hFile, // handle to communications resource
DWORD dwFlags // action to perform
);
Parameters
- hFile
- Handle to the communications resource. The CreateFile function returns this handle.
- dwFlags
- Specifies the action to take. This parameter can be a combination of the following values:
| Value |
Meaning |
| PURGE_TXABORT |
Terminates all outstanding overlapped write operations and returns immediately, even if the write operations have not been completed. |
| PURGE_RXABORT |
Terminates all outstanding overlapped read operations and returns immediately, even if the read operations have not been completed. |
| PURGE_TXCLEAR |
Clears the output buffer (if the device driver has one). |
| PURGE_RXCLEAR |
Clears the input buffer (if the device driver has one). |
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
GetCommState
The GetCommState function fills in a device-control block (a DCB structure) with the current control settings for a specified communications device.
BOOL GetCommState(
HANDLE hFile, // handle to communications device
LPDCB lpDCB // pointer to device-control block structure
);
Parameters
- hFile
- Handle to the communications device. The CreateFile function returns this handle.
- lpDCB
- Pointer to the DCB structure in which the control settings information is returned.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetCommState
The SetCommState function configures a communications device according to the specifications in a device-control block (a DCB structure). The function reinitializes all hardware and control settings, but it does not empty output or input queues.
BOOL SetCommState(
HANDLE hFile, // handle to communications device
LPDCB lpDCB // pointer to device-control block structure
);
Parameters
- hFile
- Handle to the communications device. The CreateFile function returns this handle.
- lpDCB
- Pointer to a DCB structure containing the configuration information for the specified communications device.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SetupComm
The SetupComm function initializes the communications parameters for a specified communications device.
BOOL SetupComm(
HANDLE hFile, // handle to communications device
DWORD dwInQueue, // size of input buffer
DWORD dwOutQueue // size of output buffer
);
Parameters
- hFile
- Handle to the communications device. The CreateFile function returns this handle.
- dwInQueue
- Specifies the recommended size, in bytes, of the device's internal input buffer.
- dwOutQueue
- Specifies the recommended size, in bytes, of the device's internal output buffer.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PurgeComm
The PurgeComm function can discard all characters from the output or input buffer of a specified communications resource. It can also terminate pending read or write operations on the resource.
BOOL PurgeComm(
HANDLE hFile, // handle to communications resource
DWORD dwFlags // action to perform
);
Parameters
- hFile
- Handle to the communications resource. The CreateFile function returns this handle.
- dwFlags
- Specifies the action to take. This parameter can be a combination of the following values:
| Value |
Meaning |
| PURGE_TXABORT |
Terminates all outstanding overlapped write operations and returns immediately, even if the write operations have not been completed. |
| PURGE_RXABORT |
Terminates all outstanding overlapped read operations and returns immediately, even if the read operations have not been completed. |
| PURGE_TXCLEAR |
Clears the output buffer (if the device driver has one). |
| PURGE_RXCLEAR |
Clears the input buffer (if the device driver has one). |
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
COMSTAT
The COMSTAT structure contains information about a communications device. This structure is filled by the ClearCommError function.
typedef struct _COMSTAT {
DWORD fCtsHold : 1; // Tx waiting for CTS signal
DWORD fDsrHold : 1; // Tx waiting for DSR signal
DWORD fRlsdHold : 1; // Tx waiting for RLSD signal
DWORD fXoffHold : 1; // Tx waiting, XOFF char received
DWORD fXoffSent : 1; // Tx waiting, XOFF char sent
DWORD fEof : 1; // EOF character sent
DWORD fTxim : 1; // character waiting for Tx
DWORD fReserved : 25; // reserved
DWORD cbInQue; // bytes in input buffer
DWORD cbOutQue; // bytes in output buffer
} COMSTAT, *LPCOMSTAT;
Members
- fCtsHold
- Specifies whether transmission is waiting for the CTS (clear-to-send) signal to be sent. If this member is TRUE, transmission is waiting.
- fDsrHold
- Specifies whether transmission is waiting for the DSR (data-set-ready) signal to be sent. If this member is TRUE, transmission is waiting.
- fRlsdHold
- Specifies whether transmission is waiting for the RLSD (receive-line-signal-detect) signal to be sent. If this member is TRUE, transmission is waiting.
- fXoffHold
- Specifies whether transmission is waiting because the XOFF character was received. If this member is TRUE, transmission is waiting.
- fXoffSent
- Specifies whether transmission is waiting because the XOFF character was transmitted. If this member is TRUE, transmission is waiting. Transmission halts when the XOFF character is transmitted to a system that takes the next character as XON, regardless of the actual character.
- fEof
- Specifies whether the end-of-file (EOF) character has been received. If this member is TRUE, the EOF character has been received.
- fTxim
- If this member is TRUE, there is a character queued for transmission that has come to the communications device by way of the TransmitCommChar function. The communications device transmits such a character ahead of other characters in the device's output buffer.
- fReserved
- Reserved; do not use.
- cbInQue
- Specifies the number of bytes received by the serial provider but not yet read by a ReadFile operation.
- cbOutQue
- Specifies the number of bytes of user data remaining to be transmitted for all write operations. This value will be zero for a nonoverlapped write.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
点击此处查看原文 >>
系统分类:
嵌入式 | 用户分类:
| 来源:
整理