⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opcclient.cpp

📁 VC++实现的预测控制
💻 CPP
📖 第 1 页 / 共 3 页
字号:
         hr = opcAsyncIO.Refresh(dwConnection1, OPC_DS_CACHE, &transactionID);
         if( FAILED(hr) )
         {
            //ReportError( _T("Refresh: "), hr );
			 AfxMessageBox("Refresh failed");
            return;
         }
   #endif // FULL_TEST
      }
   }
}
void VariantToString(VARIANT value, CString& strText)
{
    switch(value.vt)
    {
        case VT_BOOL:
            strText = value.boolVal ? _T("On") : _T("Off");
            break;
        case VT_UI1:
            strText.Format( _T("%hd"), (USHORT)value.bVal );
            break;
        case VT_UI2:
            strText.Format( _T("%d"), value.uiVal );
            break;
		case VT_I1: // 
			strText.Format( _T("char (%d)"), value.cVal );
            break;
        case VT_I2:
            strText.Format( _T("%hd"), value.iVal );
            break;
        case VT_I4:
            strText.Format( _T("%d"), value.lVal );
            break;
        case VT_UI4:
            strText.Format( _T("%d"), value.ulVal );
            break;
        case VT_R4:
            strText.Format( _T("%g"), value.fltVal );
            break;
        case VT_R8:
            strText.Format( _T("%g"), value.dblVal );
            break;
        case VT_BSTR:
            strText = value.bstrVal;
            break;
        default: // Arrays of simple types
        {
            if( (value.vt & VT_ARRAY)==VT_ARRAY )
            {
                CString temp;
                SAFEARRAY* pArray = value.parray;
                LONG lBound = 0, uBound = 0;
                SafeArrayGetLBound( pArray, 1, &lBound );
                SafeArrayGetUBound( pArray, 1, &uBound );
                for( long element=lBound; element<=uBound; element++ )
                {
                    if( !strText.IsEmpty() )
                        strText += _T(", ");
                    switch( value.vt & ~VT_ARRAY )
                    {
                    case VT_BOOL:
                        {
                            VARIANT_BOOL b=0;
                            SafeArrayGetElement(pArray, &element, &b);
                            temp = b ? _T("1") : _T("0");
                        }
                        break;
                    case VT_UI1:
                        {
                            BYTE b=0;
                            SafeArrayGetElement(pArray, &element, &b);
                            temp.Format( _T("%hd"), b );
                        }
                        break;
                    case VT_UI2:
                    case VT_I2:
                        {
                            short b=0;
                            SafeArrayGetElement(pArray, &element, &b);
                            temp.Format( _T("%hd"), b );
                        }
                        break;
                    case VT_I4:
                        {
                            long b=0;
                            SafeArrayGetElement(pArray, &element, &b);
                            temp.Format( _T("%d"), b );
                        }
                        break;
                    case VT_R4:
                        {
                            float d=0;
                            SafeArrayGetElement(pArray, &element, &d);
                            temp.Format( _T("%g"), d );
                        }
                        break;
                    case VT_R8:
                        {
                            double d=0;
                            SafeArrayGetElement(pArray, &element, &d);
                            temp.Format( _T("%g"), d );
                        }
                        break;
                    case VT_BSTR:
                        {
                            BSTR b;
                            SafeArrayGetElement(pArray, &element, &b);
                            temp = b;
                        }
                        break;
                    }
                    strText += temp;
                }
            }
            else
                strText = _T("?");
        }
    }
}

void COPCClient::ReadItems()
{
	int k = AllItemList.GetSize();
	for(int i=0;i<k;i++)
	 {
		m_value = AllItemList.GetAt(i);
		 POSITION pos = items.GetHeadPosition();
          for( int index=0; pos; index++ )
          {
             Item* pItem = items.GetNext( pos );
			 if(pItem->name.Compare(m_value)==0)
			    pCurrentItem = pItem;
		  }
		 int n = Read();
		
	 }
         
}
void COPCClient::Write(CString name,double s)
{
	      m_async = FALSE;
          
          /*for(int i=0;i<3;i++)
		  {
		    if(PIDParas.GetSize()==0)
			{
				AfxMessageBox("没有连接OPC Server!");
				break;
			}
			if(PIDParas.GetAt(i)=="")
            {
				AfxMessageBox("没有导入数据!");
				break;
			}*/
		    m_value = name;//PIDParas.GetAt(i);
		  
		    POSITION pos = items.GetHeadPosition();
            for( int index=0; pos; index++ )
			{
                Item* pItem = items.GetNext( pos );
			    if(pItem->name.Compare(m_value)==0)
			    pCurrentItem = pItem;
			}
		    /*if(pCurrentItem->value.vt == VT_BSTR)
			{
			    m_value = m_Itemvalue[i];
		        AutoWrite();
			}
		    else
			{*/
                m_doublevalue = s;
			    AutoDoubleWrite();
			//}
		  //}  
		  return;
}
void COPCClient::WriteItems()
{
	      m_async = FALSE;
          int k = AOTagList->GetSize();
          for(int i=0;i<k;i++)
		  {
		       
		       m_value = AOTagList->GetAt(i)->name;
		  
		    POSITION pos = items.GetHeadPosition();
            for( int index=0; pos; index++ )
			{
                Item* pItem = items.GetAt( pos );
			    if(pItem->name.Compare(m_value)==0)
			    pCurrentItem = pItem;
				items.GetNext(pos);
			}
		    if(pCurrentItem->value.vt == VT_BSTR)
			{
			    m_value = m_Itemvalue[i];
		        AutoWrite();
			}
		    else
			{
                m_doublevalue = AOTagList->GetAt(i)->value;
			    AutoDoubleWrite();
			}
		  }      
}

void COPCClient::AutoWrite()
{
    HRESULT* pErrors = NULL;
	CWaitCursor wait;
    //这里是自动写入
    if( m_async  )//改正
      {
	    if( usingCP )
          {
             OPCAsyncIO2 opcAsyncIO2;
             if( opcAsyncIO2.Attach( opcGroup ) == S_OK )
             {
                transactionID = 2;   // any number the client wants
                COleVariant vt( m_value );   // initialize as a string
                vt.ChangeType( pCurrentItem->value.vt );  // let COleVariant convert!
                HRESULT hr = opcAsyncIO2.Write(1, &pCurrentItem->hServerHandle,
                                              vt, transactionID, &transactionID, &pErrors);
                if( SUCCEEDED(hr) )
                {
                   if( FAILED(pErrors[0]) )
                   {
                      //ReportError( _T("ASync Write: "), pErrors[0] );
					   AfxMessageBox("ASync Write failed");
                   }
                   CoTaskMemFree( pErrors );
                }
                else
                {
                   //ReportError( _T("ASync Write: "), hr );
					AfxMessageBox("ASync Write failed");
                }
             }
         }
         else
         {
             OPCAsyncIO opcAsyncIO;
             if( opcAsyncIO.Attach( opcGroup ) == S_OK )
             {
                COleVariant vt( m_value );   // initialize as a string
                vt.ChangeType( pCurrentItem->value.vt );  // let COleVariant convert!
                HRESULT hr = opcAsyncIO.Write(dwConnection2, 1, &pCurrentItem->hServerHandle,
                                              vt, &transactionID, &pErrors);
                if( SUCCEEDED(hr) )
                {
                   if( FAILED(pErrors[0]) )
                   {
                      //ReportError( _T("ASync Write: "), pErrors[0] );
					   AfxMessageBox("ASync Write failed");
					   return;
                   }
                   CoTaskMemFree( pErrors );
                }
                else
                {
                   //ReportError( _T("ASync Write: "), hr );
					  AfxMessageBox("ASync Write failed");
					  return;
                }
             }
         }
      }
      else
      {
         OPCSyncIO opcSyncIO;
         if( opcSyncIO.Attach( opcGroup ) == S_OK )
         {
            COleVariant vt( m_value );   // initialize as a string
            vt.ChangeType( pCurrentItem->value.vt );  // let COleVariant convert!
            HRESULT hr = opcSyncIO.Write( 1, &pCurrentItem->hServerHandle, vt, &pErrors);
            if( SUCCEEDED(hr) )
            {
               if( FAILED(pErrors[0]) )
               {
                  //ReportError( _T("Sync Write: "), pErrors[0] );
				   AfxMessageBox("ASync Write failed");
               }
               CoTaskMemFree( pErrors );
            }
            else
            {
               //ReportError( _T("Sync Write: "), hr );
				AfxMessageBox("ASync Write failed");
				return;
            }
         }
	  }
}
void COPCClient::AutoDoubleWrite()
{
    HRESULT* pErrors = NULL;
	CWaitCursor wait;
    //这里是自动写入
	m_async = 1;
    if( m_async  )//改正
      {
	    if( usingCP )
          {
             OPCAsyncIO2 opcAsyncIO2;
             if( opcAsyncIO2.Attach( opcGroup ) == S_OK )
             {
                transactionID = 2;   // any number the client wants
                COleVariant vt( m_doublevalue );   // initialize as a string
                vt.ChangeType( pCurrentItem->value.vt );  // let COleVariant convert!
                HRESULT hr = opcAsyncIO2.Write(1, &pCurrentItem->hServerHandle,
                                              vt, transactionID, &transactionID, &pErrors);
                if( SUCCEEDED(hr) )
                {
                   if( FAILED(pErrors[0]) )
                   {
                      //ReportError( _T("ASync Write: "), pErrors[0] );
					   AfxMessageBox("ASync Write failed");
                   }
                   CoTaskMemFree( pErrors );
                }
                else
                {
                   //ReportError( _T("ASync Write: "), hr );
					AfxMessageBox("ASync Write failed");
                }
             }
         }
         else
         {
             OPCAsyncIO opcAsyncIO;
             if( opcAsyncIO.Attach( opcGroup ) == S_OK )
             {
                COleVariant vt( m_doublevalue );   // initialize as a string
                vt.ChangeType( pCurrentItem->value.vt );  // let COleVariant convert!
                HRESULT hr = opcAsyncIO.Write(dwConnection2, 1, &pCurrentItem->hServerHandle,
                                              vt, &transactionID, &pErrors);
                if( SUCCEEDED(hr) )
                {
                   if( FAILED(pErrors[0]) )
                   {
                      //ReportError( _T("ASync Write: "), pErrors[0] );
					   AfxMessageBox("ASync Write failed");
                   }
                   CoTaskMemFree( pErrors );
                }
                else
                {
                   //ReportError( _T("ASync Write: "), hr );
					  AfxMessageBox("ASync Write failed");
                }
             }
         }
      }
      else
      {
         OPCSyncIO opcSyncIO;
         if( opcSyncIO.Attach( opcGroup ) == S_OK )
         {
            COleVariant vt( m_doublevalue );   // initialize as a string
            vt.ChangeType( pCurrentItem->value.vt );  // let COleVariant convert!
            HRESULT hr = opcSyncIO.Write( 1, &pCurrentItem->hServerHandle, vt, &pErrors);
            if( SUCCEEDED(hr) )
            {
               if( FAILED(pErrors[0]) )
               {
                  //ReportError( _T("Sync Write: "), pErrors[0] );
				   AfxMessageBox("ASync Write failed");
               }
               CoTaskMemFree( pErrors );
            }
            else
            {
               //ReportError( _T("Sync Write: "), hr );
				AfxMessageBox("ASync Write failed");
            }
         }
	  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -