while (!m_pRecordset->adoEOF)//遍历所有记录

    TheValue = m_pRecordset->GetCollect("port");
cPort=_com_util::ConvertBSTRToString(_bstr_t(TheValue));
strPort=*cPort;

pThread_com[i]=AfxBeginThread(ThreadFunc_com,(LPVOID)cPort);
m_pRecordset->MoveNext();   //转到下一条纪录
i=i+1;
}

解决方案 »

  1.   

    UINT ThreadFunc_com(LPVOID n)
    {

    DWORD style=WS_VISIBLE|WS_CHILD;

    if (!theDlg.m_ComArray[port].Create(NULL,style,CRect(0,0,0,0),&theDlg,IDC_MSCOMM1))
    {
    AfxMessageBox(" no create!");
    return 0;
    }
    else
    {
    if (theDlg.m_ComArray[port].GetPortOpen()==TRUE) theDlg.m_ComArray[port].SetPortOpen(FALSE);

    theDlg.m_ComArray[port].SetCommPort(port);


    if(theDlg.m_ComArray[port].GetPortOpen()==FALSE) //打开串口 
    {
    theDlg.m_ComArray[port].SetPortOpen(TRUE);


    while (true)
    {

    CTime time=CTime::GetCurrentTime();
         
    int i=0;
    if (time.GetMinute()%interval==0)  {

    theApp.Bdata(&theDlg.m_ComArray[port],strERTU); 

    } Sleep(60*1000);

    }
    }
    }
    return 0;
    }
      

  2.   

    可是有时2个线程可以分别实现访问不同的串口,并接受数据。有时却只有一个线程工作 ???如何实现 串口独占访问??? 延长Sleep()么?
      

  3.   

    补充: CWinThread * pThread_com[50];
    另外,调试时先后有2个提示:
    first-chance exception in com.exe(KERNEL32.DLL):0xE06D7363 Microsoft C++ ExceptionUnhandled Exception in com.exe (MFC42.DLL) 0xC0000005 Access Violation
      

  4.   

    可能是同时访问数据库 or 释放指针m_pCommand最新发现 
    错误发生在该函数里
    m_pCommand->CommandText=_bstr_t("insert into tenergydata(meter_id,datetime,za,fa,zr,fr,r1,r2,r3,r4) values(")+_bstr_t(TheValue)+_bstr_t(",'"+addTime+"',"+za+","+fa+","+zr+","+fr+","+r1+","+r2+","+r3+","+r4+")");
      

  5.   

    TO: 楼主 murenxiang (木人巷) 我也正在解决这个问题,就是多个进程用不同的handle访问同一个串口的问题。不知道操作系统会不会自动进行管理,我正准备试试。可能要设置窗口的 overlapped I/O 属性。如果你解决了,一定记得发信给我,OK?
      

  6.   

    有高手 指教 ,“对COM对象的指针做marshal“谁和实现???
      

  7.   

    两个线程都调用的同一个函数里有sleep(6000);程序运行时,虽可实现设计功能,但界面不响应。sleep可否支持后台运行???调试时,错误如下(有人能看懂么 请指教):KERNEL32! 77e8b2f0()
    MSVCRT! 7800b78d()
    _com_error::_com_error(_com_error * const 0x00000000 {???}, long -2147217873, IErrorInfo * 0x012988d8, unsigned char 216) address 0x0040f087
    _com_issue_errorex(long -2147217873, IUnknown * 0x012988d8, const _GUID & {1F8BF778-0001-0000-6084-290160842901}) + 93 bytes
    _com_issue_errorex(long -2147217873, IUnknown * 0x01261e48, const _GUID & {IID_Command15}) + 93 bytes
    COM_MFC! Command15::Execute(struct tagVARIANT *,struct tagVARIANT *,long) line 246
    COM_MFC! CCOM_MFCApp::Bdata(class CMSComm *,class CString) line 750 + 51 bytes
    COM_MFC! ThreadFunc_com(void *) line 377
    MFC42! 6bc50185()
    MSVCRT! 780060ce()
    KERNEL32! 77e6b2d8()
      

  8.   

    问题基本解决:

    _variant_t TheValue 
     TheValue = m_pRecordset_com->GetCollect("port");  (debug时提示此句有错误)改为
    _variant_t TheValue = m_pRecordset_com->GetCollect(_variant_t("port"));有人 知道这是为什么么?