求大神帮我找找错误
#include "stdafx.h"#include "stdio.h"
#include "afx.h"
#include "Windows.h"
#include "winbase.h"
#include <iostream>
using namespace std;
class CommUtils
{
public:
bool OpenCom(int Port);
void SendCommand(byte *wbuffer);
bool ReadCom();
void CloseCom();
CommUtils();
virtual ~CommUtils();
private:
OVERLAPPED ReadovReady, WriteovReady;
HANDLE m_hCom; //创建串行通信句柄
bool bOpenCom;
};
CommUtils::CommUtils()
{
bOpenCom = false;
}
CommUtils::~CommUtils()
{
this->CloseCom();
}
bool CommUtils::OpenCom(int Port)
{
if (bOpenCom)
{
this->CloseCom();
bOpenCom = false;
}
char szport[10];
sprintf(szport,"COM%d",Port);
m_hCom=CreateFile(szport, //对象名称
GENERIC_READ|GENERIC_WRITE, //指定对象的访问方式
0, //共享模式,“0”是不共享
NULL, //指向一个LPSECURITY_ATTRIBUTES结构的指针,若为NULL,则返回的句柄不能被继承
OPEN_EXISTING, //指定对文件存在应采用哪种方式,对于串口应采取OPEN,EXISTING
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //指定对象的属性和标志
NULL);
if(m_hCom==INVALID_HANDLE_VALUE) //打开串口是否成功
{
MessageBox(NULL,_T("打开COM失败!"),_T("警告"),MB_OK);
return FALSE;
}
return TRUE;
if(!SetupComm(m_hCom,1024,1024)) return false; //输入输出缓冲区大小
COMMTIMEOUTS TimeOuts; //定义超时结构,并填写该结构
TimeOuts.ReadIntervalTimeout=1000; //设定读超时
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
TimeOuts.WriteTotalTimeoutMultiplier=500; //设定写超时
TimeOuts.WriteTotalTimeoutConstant=2000;
if(!SetCommTimeouts(m_hCom,&TimeOuts)) return false; //设置读写操作的时间溢出
memset(&ReadovReady,0,sizeof(OVERLAPPED));
memset(&WriteovReady,0,sizeof(OVERLAPPED));
ReadovReady.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
WriteovReady.hEvent =CreateEvent(NULL,TRUE,FALSE,NULL);
DCB dcb; //定义数据控制块结构
GetCommState(m_hCom,&dcb); //读串口原来的参数设置
dcb.BaudRate=9600; //波特率
dcb.ByteSize=8; //端口当前使用的数据位
dcb.Parity=NOPARITY; //校验是否允许
dcb.StopBits=ONESTOPBIT; //停止位数
if(!SetCommState(m_hCom,&dcb)) return false; //串口参数配置
PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR); //清空缓冲区
bOpenCom = true;
return bOpenCom;
}
void CommUtils::SendCommand(byte *wbuffer)
{
BOOL bResult=FALSE;
DWORD SendLength=0;
DWORD ToSend=21;
WriteovReady.hEvent=CreateEvent(NULL,
FALSE,//手工设置事件有无信号
FALSE,//初始化事件为无信号状态
NULL);
WriteovReady.Offset=0;
WriteovReady.OffsetHigh=0;
WriteovReady.Internal=0;
WriteovReady.InternalHigh=0;
WriteovReady.Pointer=0;
bResult = WriteFile(m_hCom, // Handle to COMM Port
wbuffer, // Pointer to message buffer in calling finction
ToSend, // Length of message to send
&SendLength, // Where to store the number of bytes sent
&ReadovReady); // Overlapped structure
if(!bResult)
{
if(GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(WriteovReady.hEvent,2000); //等待结束信号并设置最长等待时间
bResult=FALSE;
}
}
bResult=GetOverlappedResult(m_hCom, // Handle to COMM port
&WriteovReady, // Overlapped structure
&SendLength, // Stores number of bytes sent
TRUE); // Wait flag
if(SendLength!=ToSend)
cout<<"WARNING: WriteFile() error.. Bytes Sent:"<<SendLength<<","<<"Message Length:"<<ToSend<<endl;
else
cout<<"send successful"<<endl;
}
bool CommUtils::ReadCom()
{
char rbuffer[1024];
DWORD ReceiveLength=0;
DWORD ToReceive=1024;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
ReceiveLength=min(ReceiveLength,(DWORD)ComStat.cbInQue);
if(!ReceiveLength) return false;
BOOL bReadStatus;
bReadStatus=ReadFile(m_hCom,
rbuffer,
ToReceive,
&ReceiveLength,
&ReadovReady);
if(!bReadStatus)
{
if(GetLastError()==ERROR_IO_PENDING)
{ WaitForSingleObject(ReadovReady.hEvent,2000);
PurgeComm(m_hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); //清空缓冲区
return ToReceive;
}
return 0;
}
CFile file;
file.Open( "F:\game\sun\C++\123.txt", CFile::modeCreate|CFile::modeWrite );
file.Write(rbuffer,1024);
file.Close();
}
void CommUtils::CloseCom()
{
HANDLE m_hCom;
}
void main()
{
CommUtils com;
com.OpenCom(1);
byte unlog_command[10]="unlogall";
unlog_command[8]=13;
unlog_command[9]=10;
com.SendCommand(unlog_command);
com.ReadCom();
com.CloseCom();
return ;
}
这是