我想用CSerialPort类来实现工业控制,我想把它做成一个基于单文档的程序,但是由于对VC++不是很熟悉,所以在这里请教下大侠们,我到底该怎么去实现呢!我知道怎么添加到单文档,就是不明白我要做的事情该怎么去添加到程序中,望大家帮帮忙,谢谢!!!

解决方案 »

  1.   

    补充说明:
         在信息采集方面的应用,多创建几个对象,然后预留一个对象来实现实时通讯的,如果环路设备中有设备损坏或者故障的,就挂起线程,3分钟重启一次,如果三次都不好用就直接关闭!基本要求就是这样的吧。有CSerialPort使用经验的前辈们,给我指点下!
      

  2.   

    其实你的想法已经有了,但如何用程序实现,这个一时半时说不清楚。
    建议先把多线程及基本的MFC学好再说。通讯核心思想:
    1、建立发送数据管理类
    2、协议翻译类
    3、按协议发送请求数据
    4、等待回应,如果超时,则重发或进行下一个数据
    5、如果回应正确,进行下一个数据发送3-5是一个循环过程大概就是这么个样子。
      

  3.   

    那些已经有了,现在就是串口打不开了,我想打开4个串口,或者更多的,我定义的是50个,
    assert(portnr>0 && portnr<50);//怎么就错了呢?我很无语,运行的结果就是Debug Error!
    到底我的程序该怎么改啊?
    BOOL CSerialPort::InitPort(CWnd* pPortOwner,UINT portnr,UINT baud,char parity,
       UINT databits,UINT stopbits,
    DWORD dwCommEvents,
       UINT writebuffersize,
       DWORD ReadIntervalTimeout,
       DWORD ReadTotalTimeoutMultiplier,
       DWORD ReadTotalTimeoutConstant,
       DWORD WriteTotalTimeoutMultiplier,
       DWORD WriteTotalTimeoutConstant)
    {
    // assert(portnr>0 && portnr<50);
    assert(pPortOwner!=NULL);m_hComm=CreateFile(szPort,GENERIC_READ|GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,
    0);
    望知道的给我指点下,详细点啊 ,谢谢!!!
      

  4.   

     #3楼 得分:0回复于:2012-07-05 10:50:27其实你的想法已经有了,但如何用程序实现,这个一时半时说不清楚。
    建议先把多线程及基本的MFC学好再说。通讯核心思想:
    1、建立发送数据管理类
    2、协议翻译类
    3、按协议发送请求数据
    4、等待回应,如果超时,则重发或进行下一个数据
    5、如果回应正确,进行下一个数据发送3-5是一个循环过程大概就是这么个样子。 
     
      

  5.   

    BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message)
       UINT  portnr, // portnumber (1..4)
       UINT  baud, // baudrate
       char  parity, // parity 
       UINT  databits, // databits 
       UINT  stopbits, // stopbits 
       DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc
       UINT  writebuffersize) // size to the writebuffer
    {
    assert(portnr > 0 && portnr < 5);
    assert(pPortOwner != NULL); // if the thread is alive: Kill
    if (m_bThreadAlive)
    {
    do
    {
    SetEvent(m_hShutdownEvent);
    } while (m_bThreadAlive);
    TRACE("Thread ended\n");
    } // create events
    if (m_ov.hEvent != NULL)
    ResetEvent(m_ov.hEvent);
    m_ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (m_hWriteEvent != NULL)
    ResetEvent(m_hWriteEvent);
    m_hWriteEvent = CreateEvent(NULL, 
    TRUE, FALSE, NULL);
    // m_hWriteEvent = CreateEvent(NULL, 
    // FALSE, FALSE, "");

    if (m_hShutdownEvent != NULL)
    ResetEvent(m_hShutdownEvent);
    m_hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL); // initialize the event objects
    m_hEventArray[0] = m_hShutdownEvent; // highest priority
    m_hEventArray[1] = m_ov.hEvent;
    m_hEventArray[2] = m_hWriteEvent; // initialize critical section
    InitializeCriticalSection(&m_csCommunicationSync);

    // set buffersize for writing and save the owner
    m_pOwner = pPortOwner; if (m_szWriteBuffer != NULL)
    delete [] m_szWriteBuffer;
    m_szWriteBuffer = new char[writebuffersize]; m_nPortNr = portnr; m_nWriteBufferSize = writebuffersize;
    m_dwCommEvents = dwCommEvents; BOOL bResult = FALSE;
    char *szPort = new char[50];
    char *szBaud = new char[50]; // now it critical!
    EnterCriticalSection(&m_csCommunicationSync); // if the port is already opened: close it
    if (m_hComm != NULL)
    {
    CloseHandle(m_hComm);
    m_hComm = NULL;
    } // prepare port strings
    sprintf(szPort, "\\\\.\\COM%d", portnr); //打开10以上的串口号
    sprintf(szBaud, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopbits); // get a handle to the port
    m_hComm = CreateFile(szPort, // communication port string (COMX)
         GENERIC_READ | GENERIC_WRITE, // read/write types
         0, // comm devices must be opened with exclusive access
         NULL, // no security attributes
         OPEN_EXISTING, // comm devices must use OPEN_EXISTING
         FILE_FLAG_OVERLAPPED, // Async I/O
         0); // template must be 0 for comm devices if (m_hComm == INVALID_HANDLE_VALUE)
    {
    // port not found
    delete [] szPort;
    delete [] szBaud; return FALSE;
    } // set the timeout values
    m_CommTimeouts.ReadIntervalTimeout = 1000;
    m_CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
    m_CommTimeouts.ReadTotalTimeoutConstant = 1000;
    m_CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
    m_CommTimeouts.WriteTotalTimeoutConstant = 1000; // configure
    if (::SetCommTimeouts(m_hComm, &m_CommTimeouts))
    {    
    if (::SetCommMask(m_hComm, dwCommEvents))
    {
    if (::GetCommState(m_hComm, &m_dcb))
    {
    m_dcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
    if (::BuildCommDCB(szBaud, &m_dcb))
    {
    if (::SetCommState(m_hComm, &m_dcb))
    ; // normal operation... continue
    else
    ProcessErrorMessage("SetCommState()");
    }
    else
    ProcessErrorMessage("BuildCommDCB()");
    }
    else
    ProcessErrorMessage("GetCommState()");
    }
    else
    ProcessErrorMessage("SetCommMask()");
    }
    else
    ProcessErrorMessage("SetCommTimeouts()"); delete [] szPort;
    delete [] szBaud; // flush the port
    ::PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); // release critical section
    LeaveCriticalSection(&m_csCommunicationSync); TRACE("Initialisation for communicationport %d completed.\nUse Startmonitor to communicate.\n", portnr); return TRUE;
    }
    通常一个CSerialPort实例只对应一个串口,如果要开50个,就有50个CSerialPort实例,这样就不会有问题了。
      

  6.   

    嗯哪,我也知道一个CSerialPort只对应一个串口,所以我把串口名搞成数组了,生成了多个对象啊,可是怎么就是编译通过,但是执行就报错呢?这是让我不理解的地方,我发现我遇到的问题有点妖!该改的地方我也改了啊!有知情人士,望伸出援助之手,HELP!HELP!HELP!
      

  7.   

    没有出现过这类问题。有看了一下你的问题:
    现在就是串口打不开了,我想打开4个串口,或者更多的,我定义的是50个,
    assert(portnr>0 && portnr<50);//怎么就错了呢?我很无语,运行的结果就是Debug Error!
    在assert(portnr>0 && portnr<50);程序里的串口只能是1-49,定义到50当然错了。如果非要50个以上,则将assert(portnr>0 && portnr<101); // 可以支持COM1-COM100了。