这个类把串口操作进行封装
#include
class COM //串口操作类
{
//私有方法部分
protected:
HANDLE hCom; //串口句柄
COMMTIMEOUTS TimeOuts; //设置读写超时
DCB dcb; //设置串口状态结构体
//公共方法部分
public:
COM(void);
~COM(void);
//以阻塞方式打开一个串口参数依次为[串口号,波特率,校验标记,停止位长度]
int open(LPCTSTR com_name,DWORD com_bt,BYTE com_jy,BYTE com_sz,BYTE com_tz);
//关闭当前串口
void close(void);
//从串口读sz个字节到r_data
int read(LPVOID r_data,DWORD sz);
//从w_data发送sz个字节到串口
int write(LPCVOID w_data,DWORD sz);
};
#include "stdafx.h"
#include "串口操作类.h"
COM::COM()
{
}
COM::~COM()
{
//销毁对象时先关闭串口
close();
}
int COM::open(LPCTSTR com_name,DWORD com_bt,BYTE com_jy,BYTE com_sz,BYTE com_tz)
{
//以阻塞方式打开一个串口参数依次为[串口号,波特率,校验标记,停止位长度]
hCom=CreateFile(com_name,//COM1口
GENERIC_READ|GENERIC_WRITE, //允许读和写
0, //独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
0, //同步方式
NULL);
if(hCom==(HANDLE)-1)
{
printf("打开%s失败!\n",com_name);
return 0;
}
SetupComm(hCom,1024,1024); //输入缓冲区和输出缓冲区的大小都是1024
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=5000;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
GetCommState(hCom,&dcb); //获取串口原始参数
dcb.BaudRate=com_bt; //设置波特率取值范围是 CBR_110,CBR_300,CBR_600,CBR_1200,CBR_2400,CBR_4800,CBR_9600,CBR_19200, CBR_38400,CBR_56000, CBR_57600, CBR_115200, CBR_128000, CBR_256000, CBR_14400
dcb.ByteSize=com_sz; //设置通信字节位数取值范围是 4到8
dcb.fParity=1; //允许奇偶校验检查
dcb.Parity=com_jy; //设置奇偶校验位取值范围是 EVENPARITY 偶校验,NOPARITY 无校验 ,MARKPARITY 标记校验,ODDPARITY 奇校验
dcb.StopBits=com_tz; //设置停止位取值范围是 ONESTOPBIT 1位停止位 ,TWOSTOPBITS 2位停止位,ONE5STOPBITS 1.5位停止位
SetCommState(hCom,&dcb); //设置串口新的参数
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);//操作串口前先清空缓冲区
printf("打开%s成功!\n",com_name);
return 1;
}
void COM::close(void)
{
//关闭当前串口
CloseHandle(hCom);
}
int COM::read(LPVOID r_data,DWORD sz)
{
//从串口读sz个字节到r_data
DWORD wCount;//读取的字节数
BOOL bReadStat;
bReadStat=ReadFile(hCom,r_data,sz,&wCount,NULL);
if(!bReadStat)
{
printf("读串口失败!\n");
PurgeComm(hCom, PURGE_TXABORT|
PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
return 0;
}
if(wCount!=sz) return 0;
return 1;
}
int COM::write(LPCVOID w_data,DWORD sz)
{
//从w_data发送sz个字节到串口
BOOL bWriteStat;
DWORD dwBytesWrite;
bWriteStat=WriteFile(hCom,w_data,sz,&dwBytesWrite,NULL);
if(!bWriteStat)
{
printf("写串口失败!\n");
PurgeComm(hCom, PURGE_TXABORT|
PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
return 0;
}
if(dwBytesWrite!=sz) return 0;
return 1;
}这个类对按键译码进行封装,键码太多我只译码了26个字母按键,译码表这部分是个体力活大家可以根据实际使用情况来调整。
#include
class PS2
{
//私有方法部分
protected:
SYSTEMTIME TIME; //标准系统时间存放结构体共8个成员16字节
int up; //按键释放标记
//公共方法部分
public:
PS2(void);
~PS2(void);
int PS2_ym(BYTE m); //译码函数通过一个按键码来翻译成WINDOWS键盘事件
};
#include "stdafx.h"
#include "PS2键盘译码.h"
BYTE key[26][2]={{0x1c,65},{0x32,66},{0x21,67},{0x23,68},{0x24,69},{0x2b,70},{0x34,71},{0x33,72},{0x43,73},{0x3b,74},{0x42,75},{0x4b,76},{0x3a,77},{0x31,78},{0x44,79},{0x4d,80},{0x15,81},{0x2d,82},{0x1b,83},{0x2c,84},{0x3c,85},{0x2a,86},{0x1d,87},{0x22,88},{0x35,89},{0x1a,90}}; //键码表
char *key_name[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
PS2::PS2()
{
up=0;//释放标记清0
}
PS2::~PS2()
{
}
int PS2::PS2_ym(BYTE m)
{
//译码函数通过一个按键码来翻译成WINDOWS键盘事件
int i;
if(m==0xf0)
{
//有断码就做标记
up=1;
return 0;
}else
{
if(up==0)
{
for(i=0;i<26;i++)
{
if(m==key[i][0])
{
keybd_event(key[i][1], 0, 0 ,0);
printf("按键%s按下!",key_name[i]);
GetLocalTime(&TIME);//获得系统当前时间
printf("事件触发时间为:%d年%d月%d日%d时%d分%d秒\n",TIME.wYear,TIME.wMonth,TIME.wDay,TIME.wHour,TIME.wMinute,TIME.wSecond);
}
}
}else if(up==1)
{
for(i=0;i<26;i++)
{
if(m==key[i][0])
{
keybd_event(key[i][1], 0,KEYEVENTF_KEYUP,0);
printf("按键%s弹起!",key_name[i]);
GetLocalTime(&TIME);//获得系统当前时间
printf("事件触发时间为:%d年%d月%d日%d时%d分%d秒\n",TIME.wYear,TIME.wMonth,TIME.wDay,TIME.wHour,TIME.wMinute,TIME.wSecond);
up=0; //键已经释放标记清0
}
}
}
}
return 1;
}