西門子Prodave5.5使用說明及VC示例


 西門子PLC的通信協議主要是PPI、MPI、Profibus、CP243/CP343/CP443 網絡協議,prodave是早期完成的程序接口,除了網絡協議外其它的主要協議都支持,SoftNet是西門子最新推出的通信協議接口,穩定,並且大而全,目前西門子所有主流的協議都支持(我的blog文章西門子Softnet驅動的成功開發已經做了簡單介紹),由於好多朋友對prodave都比較關注,所以我這里專門寫篇blog來簡單介紹一下。

        我所知道的最新的Prodave的版本是V5.5,完整版的要45兆左右,由於出的比較早,所以動態庫“W95_s7.dll”的名稱保留至今,我最早接觸是在01~02年,不過當時版本好像不到V5.5,與S7-200通信很不穩定,並且訪問周期比較長。給我的感覺Prodave好像專門為S7-300制作的(從庫函數的聲明可以看出),連S7-300相對而言比較順利。

       組態王、力控好多主流工控軟件訪問西門子PLC都是通過Prodave或Softnet的,可以在驅動程序中看到熟悉的W95_s7.dll,所以通信能力大家還是應該放心的。

       題外話,對嵌入式系統,如WinCE,由於不能直接使用Prodave和Softnet,所以要實現與西門子PLC通信,一般只有破解了(西門子的通信協議都是保密的,並且也是加密的,一般不公開給客戶),目前實現的較好的主要有PPI,MPI(需要MPI適配器,不同適配器通信協議有一定區別),CP243,CP343/CP443。

      下面是我在開發相關西門子通信程序時,做的一個VC測試程序,僅供參考(Prodave簡版驅動和相關測試代碼,我已經上傳,文章后面附下載連接)。 

void CTestDlg::OnProdave() 
{
    int iRes;
    CString myStr;
    signed char Buffer[2048];

 

    WORD *Buffer_int = (WORD *)Buffer;
    unsigned char *Buffer_byte = (unsigned char *)Buffer;    //WORD wValue;

    //m_field_read MB200
    iRes=m_field_read(200,1,Buffer);

    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");

        myStr.Format("MB200=%3d",Buffer_byte[0]);
        m_Dis.ReplaceSel(myStr);

        UpdateData(false);
    }
    else
    {
        //myStr.Format("m_field_read error no:%d",iRes);
        AfxMessageBox(ErrString(iRes));        
    }

    //m_field_read
    iRes=m_field_read(100,1,Buffer);

    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");
        myStr.Format("MB100=%3d",Buffer_byte[0]);

        m_Dis.ReplaceSel(myStr);

        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));        
    }    

    //m_field_read
    iRes=a_field_read(0,1,Buffer);

    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");
        myStr.Format("QB0=%3d",Buffer_byte[0]);

        m_Dis.ReplaceSel(myStr);

        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));        
    }

    //寫數據 MB110
    unsigned long value;
    value=100;
    memcpy(Buffer,&value,4);

    iRes=m_field_write(111,1,Buffer);
    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");
        myStr.Format("MB110=%3d",Buffer[0]);
        m_Dis.ReplaceSel(myStr);
        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));        
    }

    //寫數據 MB4
    BYTE value1;
    value1=33;
    memcpy(Buffer,&value,1);
    iRes=m_field_write(4,1,Buffer);
    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");
        myStr.Format("MD4=%3d",Buffer[0]);
        m_Dis.ReplaceSel(myStr);
        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));        
    }
}

void CTestDlg::OnLoad() 
{
    adr_table_type myTable[2];
    myTable[0].adr=3;
    myTable[0].segmentid=0;
    myTable[0].slotno=2;
    myTable[0].rackno=0;
    myTable[1].adr=0;
    myTable[1].segmentid=0;
    myTable[1].slotno=2;
    myTable[1].rackno=0;
    int iRes;
    CString myStr;
    //初始化ProDave300
    iRes=load_tool(1,"S7ONLINE",myTable);
    if(iRes==0)
    {
        m_Dis.SetSel(30000,30000);
        m_Dis.ReplaceSel(" ");
        m_Dis.ReplaceSel("load_tool ok!");
        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));
        unload_tool();
        return;        
    }    
}

void CTestDlg::OnUnloadtool() 
{
    int iRes;
    CString myStr;
    iRes=unload_tool();
    if(iRes==0)
    {
        m_Dis.SetSel(30000,30000);
        m_Dis.ReplaceSel(" ");
        m_Dis.ReplaceSel("unload_tool ok!");
        m_Dis.ReplaceSel(" ");
        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));        
    }    
}

void CTestDlg::OnStatus() 
{
    int iRes;
    CString myStr;
    char  myInfo[512];
    iRes=ag_zustand(myInfo);
    if(iRes==0)
    {
        m_Dis.SetSel(30000,30000);
        m_Dis.ReplaceSel(" ");
        m_Dis.ReplaceSel("ag_zustand ok!");
        UpdateData(false);
        if(myInfo[0]==0)
        {
            m_Dis.ReplaceSel(" ");
            m_Dis.ReplaceSel("RUN");
        }
        else
        {
            m_Dis.ReplaceSel(" ");
            m_Dis.ReplaceSel("STOP");
        }
        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));        
    }    
}
LPSTR CTestDlg::ErrString(WORD wErrCode)
{
    LPSTR myStr1;
    switch(wErrCode)
    {
    case 517:
        {
            return "PRODAVE not initialized.";
            break;
        }
    case 787:
        {
            return "Incorrect rate/Interrupt vector.";
            break;
        }
    case 789:
        {
            return "MPI Address error.";
            break;
        }
    case 800:
    case 818:
        {
            return "hardware fault.";
            break;
        }
    case 820:
        {
            return "com not avaliable.";
            break;
        }
    case 898:
    case 900:
        {
            return "no driver or device found.";
            break;
        }
    case 16386:
        {
            return "Connection not established.";
            break;
        }
    default:
        {
            CString myStr;
            myStr.Format("%d",wErrCode);
            myStr1=myStr.GetBuffer(0);
            myStr.ReleaseBuffer();
            return myStr1;
        }
    }
}

void CTestDlg::OnNewss() 
{
    //激活連接
    int iRes;
    iRes=new_ss(1);
    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");
        m_Dis.ReplaceSel("new_ss ok!");
        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));
        unload_tool();
        return;
    }    
}

void CTestDlg::OnAginfo() 
{
    //讀PLC信息
    int iRes;
    char  myInfo[512];
    iRes=ag_info(&myInfo[0] );
    if(iRes==0)
    {
        m_Dis.SetSel(100000,100000);
        m_Dis.ReplaceSel(" ");
        m_Dis.ReplaceSel("ag_info ok!");
        UpdateData(false);

        m_Dis.ReplaceSel(" ");
        m_Dis.ReplaceSel(&myInfo[4]);

        UpdateData(false);
    }
    else
    {
        AfxMessageBox(ErrString(iRes));
        unload_tool();
        return;
    }    
}

prodave 測試程序:http://download.csdn.net/source/228758


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM