上星期寫了一篇利用程式語言取得三菱 PLC 點位資料的介紹,因為太忙還沒給出程式範例 (其實是

太懶),這次使用 VB.NET 來寫個範例程式,並使用三菱 GX Developer 與 PLC 模擬器一起來完成這

個範例。

 

範例:抓取 PLC 時間資料

  

PLC 範例程式:

SETP 1 . MX Component 為三菱所提供元件,需要安裝 SW3D5C-ACT-E

SETP 2. 來寫個簡單的 PLC 程式,並且透過模擬器提供 VB.NET PLC  時間資料

 

PLC 程式:

image

 

參考手冊內容: 依手冊說明我們將一開始的點位設為 D0 (一開始點位可以自由設定),而 DATERD 指

令會佔用了 7 個 D 點位,占用內容如下。

image

 

寫好了 PLC 程式,來繼續寫 VB.NET 程式吧。

 

VB.NET 範例: (使用 Visual Studio 2010)

SETP 1 . 先在 Visual Studio 2010 建立一個新的 Windows Form 程式

image

 

SETP 2.在 Visual Studio 目錄中點選 【工具】 –> 【選擇工具箱項目】,點選 COM 元件選擇

今天要使用的 ActLlt.dll

 

SETP 3. 在工具箱中將 ActLlt 元件與 Timer 元件拉進 Windows Form ,Windows Form 元件如下

2011-10-29_123039 

2011-10-29_123047

 

SETP 4. 設定 Timer Tick 事件

2011-10-29_124053

 

VB.NET 程式碼:

Public Class Form1
    Private CPU_Q02CPU = &H22
    Private PORT_1 = &H1
 
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
 
        AxActLLT1.ActCpuType = CPU_Q02CPU
        AxActLLT1.Open()
        Timer1.Enabled = True
        Me.btnStart.Enabled = False
        Me.btnClose.Enabled = True
    End Sub
 
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click        
        Timer1.Enabled = False
        AxActLLT1.Close()
 
        Me.btnStart.Enabled = True
        Me.btnClose.Enabled = False
    End Sub
 
    '取得 PLC 點位資料
    Private Sub GetDeviceValue(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim val As Long
        Dim str As String = ""
        AxActLLT1.GetDevice("D0", val)
        str += CStr(val) + " / "
 
        AxActLLT1.GetDevice("D1", val)
        str += CStr(val) + " / "
 
        AxActLLT1.GetDevice("D2", val)
        str += CStr(val) + " "
 
        AxActLLT1.GetDevice("D3", val)
        str += val.ToString("00") + " : "
 
        AxActLLT1.GetDevice("D4", val)
        str += val.ToString("00") + " : "
 
        AxActLLT1.GetDevice("D5", val)
        str += val.ToString("00") + " "
 
        Me.TextBox1.Text = str
    End Sub
End Class

 

程式執行:

SETP 1.利用 PLC 模擬器模擬剛才完成的 PLC 程式

SETP 2. 開啟寫好的 VB.NET 程式,點選 Start 按鈕即可取得 PLC 內部時間資料

image

arrow
arrow
    全站熱搜

    iammic 發表在 痞客邦 留言(0) 人氣()