| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 435 人关注过本帖
标题:求大神帮我找找错误
只看楼主 加入收藏
usasun
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-5-7
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
求大神帮我找找错误
#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 ;
}


这是
搜索更多相关主题的帖子: private include public 通信 
2014-05-07 15:21
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:14 
好长啊

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2014-05-07 16:15
快速回复:求大神帮我找找错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025475 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved