com.h
#ifndef __COM_H__
#define __COM_H__
#ifdef __cplusplus
extern "C" {
#endif
//打开串口
int OpenComPort(int port, int userBaudRate);//打开串口
//关闭串口
void CloseComPort(void);//关闭串口
int ReadComBlock(unsigned char *buf,int maxLen);
void _DoRxTx(void *args);
int ReadComBuf(unsigned char *buf, int maxLen);//读串口
int WriteComBuf(unsigned char *buf, int maxLen);//写串口
int WriteComByte(unsigned char byte);//往串口写一个字节
int ReadComByte(unsigned char byte);//从串口读取一个字节
void resetComPort(void);//重置串口
void resetRTS(void);//resetRTS_sub(150,20);
int ReceiveMessage(unsigned char *pBuf, int maxlen, int time);//带时间的接收操作
void resetRTSLowHigh(int Low,int High);//CTS高低电平操作
void SetRTSControlEnable(bool enable);//CTS操作:enable=true:上拉,false:下拉
#ifdef __cplusplus
}
#endif
#endif
com.c:
/*******************************************************************************
* FileName : com.cpp
* Author :
* Description :
* Create Data : 2005-6-28 9:09
* modify History:
*******************************************************************************/
//#include <windowsx.h>
//#include <commctrl.h>
//#include <tchar.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "com.h"
//#include "common.h"
//#include "Debug.h"
#include <windows.h>
#include <process.h>
#define DelayXms Sleep
//#define EB_Printf Debug_Printf
#define COM_OVERTIME 5 // 此延时控制Uart_ReadBuf中每次读串口的延时:步长为10ms
#define IS_OVERLAPPED_IO (TRUE)
#define MAX_BLOCK_SIZE (0x20000) // 缓冲区最大128Kbytes
#define MAX_Uart_SIZE (4096) // 缓冲区最大128Kbytes
HANDLE idComDev;
OVERLAPPED osWrite,osRead;
volatile int isConnected=FALSE;
volatile int txEmpty=TRUE;
unsigned char rxBuf[MAX_Uart_SIZE+1];
unsigned char RoundBuf[MAX_BLOCK_SIZE];
struct {
int Enable; // 允许接收
int Ok; // 接收成功
int Off1; // 缓冲区当前有效数据 开始偏移
int Off2; // 缓冲区当前有效数据 末尾偏移
unsigned int Cnt; // 缓冲区当前有效数据 计数器=Off2-Off1
}rxFlag;
//打开串口
int OpenComPort(int port, int userBaudRate)
//port=0,1,2,3
{
//TCHAR *textCom[]={TEXT("COM1"),TEXT("COM2"),TEXT("COM3"),TEXT("COM4"),TEXT("COM5"),TEXT("COM6"),TEXT("COM7"),TEXT("COM8"),TEXT("COM9"),TEXT("\\\\.\\COM10"),TEXT("\\\\.\\COM11"),TEXT("\\\\.\\COM12")};
TCHAR textCom[255][14];
int i;
DCB dcb;
COMMTIMEOUTS commTimeOuts;
for(i=0;i<255;i++)
{
swprintf(textCom[i],14,L"\\\\.\\COM%d",i);
}
if(isConnected) // 若重复打开时先关闭
{
CloseComPort();
/*Debug_Printf("\n[OK:Already Opened. Close first!]");*/
}
//======&#