基于MX組件的上位計(jì)算機(jī)與三菱PLC通信
摘要:
筆者曾以前拙文基于MS-COMMON組件與上位計(jì)算機(jī)的通信(杭州自動化10周年論文集,中國工控網(wǎng)等轉(zhuǎn)載),隨著FA產(chǎn)品的開放性等市場的要求,上位計(jì)算機(jī)與PLC的通信還是一個十分廣泛的話題。三菱電機(jī)推出了MX-Componet組件,使我們開發(fā)上位計(jì)算機(jī)組態(tài)等更加方便快揭。
關(guān)鍵詞:通訊,控件,三菱電機(jī)
根據(jù)微軟權(quán)威的軟件開發(fā)指南MSDN(Microsoft Developer Network)的定義,ActiveX是Micrsoft適應(yīng)組件對象模型(Component Object Model,COM)開發(fā)技術(shù)提出的。核心COM允許應(yīng)用程序之間互相通信和使用彼此的功能,組件對象模型起源于面向?qū)ο蟮木幊蹋∣bject-oriented programming,OOP)的概念,并把這個模型延伸到操作系統(tǒng),從而方便任何COM程序?qū)ο笈c其他需要的接口的COM對象通訊。
目前,在工業(yè)控制FA領(lǐng)域,實(shí)現(xiàn)人機(jī)交互和數(shù)據(jù)備份的一般選擇工業(yè)計(jì)算機(jī)及組態(tài)軟件。筆者在南京某居住區(qū)中水處理項(xiàng)目管理計(jì)算機(jī)中,選用了Visal Basic6.0作為開發(fā)工具,結(jié)合三菱電機(jī)發(fā)布的MX-Component 控件,方便的實(shí)現(xiàn)了計(jì)算機(jī)與三菱PLC的通訊。由于采用三菱MX-Component 控件方面的文獻(xiàn)資料比較少,筆者茁文,希望大家指正。
SW3D5C-ACT-E軟件目前版本3.0,支持OS有98,2000,XP等。平臺支持VB,VC,VB.NET,VC.NET ,LABVIEW,BC等開發(fā)工具。經(jīng)過安裝SW3D5C-ACT-E,系統(tǒng)OCX控件列表內(nèi)就有相應(yīng)的驅(qū)動OCX:
一 系統(tǒng)組成構(gòu)架:
中水處理系統(tǒng)組成
系統(tǒng)描述:由上位計(jì)算機(jī),1號從站,2號從站。計(jì)算機(jī)和PLC之間通過RS-232通訊,直接在PLC編程口讀取數(shù)據(jù)。
1號站配置:
電源模塊: Q61P-A2
主基板: Q38B
CPU模塊: Q01CPU
CCLINK主站:QJ61BT11
本地輸入模塊:QX40
本地輸出模塊:QY40
模擬量模塊: Q68ADI 采集PH,濁度等信號
模擬量輸出: Q64DA 控制壓濾機(jī),風(fēng)機(jī),提升泵
2號站配置:
電源模塊: Q61P-A2
主基板: Q35B
CPU模塊: Q01CPU
CCLINK主站:QJ61BT11
本地輸入模塊:QX40
本地輸出模塊:QY40
模擬量模塊: Q68ADI
3-12號站
遠(yuǎn)程輸入輸出:AJ65SBTB1-16D,AJ65SBTB1-16T
分別采集遠(yuǎn)程水泵運(yùn)行狀態(tài)和監(jiān)視設(shè)備。
系統(tǒng)運(yùn)行登陸畫面
編寫登陸程序。
標(biāo)準(zhǔn)語法:Open (Communication line opening)
(1) Applicable ACT controls
This function is available for all ACT controls.
(2) Feature
Opens the communication line.
(3) Format
R R
Visual Basic , Visual C++ ,VBA : lRet = object.Open()
LonglRetReturned valueOutput
VBScript : varRet = object.Open()
VARIANTvarRetReturned value (LONG type)Output
(4) Explanation
The line is connected on the basis of the value set to the property for Open
method.
(5) Returned value
Normal termination: 0 is returned.
Abnormal termination: A value other than 0 is returned.
(Refer to Chapter 6 ERROR CODES.)
編寫程序:
Dim fLogin As New frmLogin
fLogin.Show vbModal
If Not fLogin.OK Then
'登錄失敗,退出應(yīng)用程序
End
End If
Unload fLogin
frmSplash.Show
'//////////////////////////
Open App.Path & "\wecom.txt" For Append As #1
Print #1, Format(Date) + " " + Format(Time) + "系統(tǒng)開啟"
'//////////////////////////
frmSplash.Refresh
Load frmMain
IRet = frmMain.Q1.Open
'/////////////////////////
Print #1, Format(Date) + " " + Format(Time) + "準(zhǔn)備與PLC通信"
'/////////////////////////////
If IRet <> 0 Then
Print #1, Format(Date) + " " + Format(Time) + "無法鏈接到PLC"
Close #1
'////////////////////////////
MsgBox "無法鏈接到PLC,請檢查硬件設(shè)置!"
ex: End
End If
'///////////////////////////
' Print #1, Format(Date) + " " + Format(Time) + "與PLC通信成功"
Close #1
' Set db = OpenDatabase(App.Path & "\base.mdb")
' Set res = db.OpenRecordset("xml")
' res.AddNew
' res.Fields(0) = "系統(tǒng)開啟時間" + Format(Date) + " " + Format(Time)
' res.Update
Set fMainForm = New frmMain
Load fMainForm
Unload frmSplash
fMainForm.Show
運(yùn)行界面
數(shù)據(jù)采集,讀取位元件,由于采樣上來的16個位元件組成一個字讀取的,必須在VB程序編寫相應(yīng)的程序,便于在界面反映每個閥門,泵的運(yùn)行狀態(tài)。
標(biāo)準(zhǔn)語法;讀數(shù)據(jù)塊
ReadDeviceBlock (Device batch-read)
(1) Applicable ACT controls
This function is available for all ACT controls.
(2) Feature
Batch-reads data from devices.
(3) Format
Visual Basic , VBA :
lRet = object.ReadDeviceBlock(szDevice, lSize, lData(0))
LonglRetReturned valueOutput
StringszDeviceDevice nameInput
LonglSizeNumber of read pointsInput
實(shí)際采集輸入狀態(tài)子程序模塊
Dim jy As Integer
Dim jx As Integer
Dim jz As Integer
'將讀出的數(shù)據(jù)分配給數(shù)組,注意,這里從Y(1)開始
'----OUTPUT Y
'------READ X SATU
IRet = Q1.ReadDeviceBlock("X0", 5, Wordx(1))
'------READ Y SATU
'IRet = Q1.ReadDeviceBlock("Y30", 8, Wordy(1))
On Error Resume Next
' ky = 0
' For YI = 1 To 7
' Call DecBinX(Wordy(YI), OTY(YI))
' For jy = 1 To Len(OTY(YI))
' Y(ky + jy - 1) = CBool(Mid$(OTY(YI), 17 - jy, 1))
' Next jy
' ky = ky + 16
' Next YI
'-----INPUT X
' For i = 1 To 5
' Wordx(i) = Int(Rnd * 32766)
' Next
kx = 0
For xi = 1 To 5
Call DecBinX(Wordx(xi), OTX(xi))
For jx = 1 To 16
x(kx + jx - 1) = CBool(Mid$(OTX(xi), 17 - jx, 1))
Next jx
kx = kx + 16
Next xi
For i = 0 To 7
Bar1(i).Value = Rnd * i + 60
Next
LED1.LEDSTRING = Format(Date) + " " + Format(Time)
'---------------------------顯示處理----------
'--------------------------------------------
向PLC寫數(shù)據(jù)塊
標(biāo)準(zhǔn)語法:WriteDeviceBlock (Device batch-write)
(1) Applicable ACT controls
This function is available for all ACT controls.
(2) Feature
Batch-writes data to devices.
(3) Format
Visual Basic , VBA : lRet = object.WriteDeviceBlock(szDevice, lSize, lData(0))
LonglRetReturned valueOutput
StringszDeviceDevice nameInput
LonglSizeNumber of write pointsInput
LonglData(n)Device values to be writtenInput
Visual C++ : lRet = object.WriteDeviceBlock(szDevice, lSize, lplData)
LonglRetReturned valueOutput
CStringszDeviceDevice nameInput
LonglSizeNumber of write pointsInput
lplDataDevice values to be writtenInput
Long
VBScript : varRet = object.WriteDeviceBlock(varDevice, varSize, varData)
VARIANTvarRetReturned value(LONG type)Output
VARIANTvarDeviceDevice name(character string type)Input
VARIANTvarSizeNumber of write points(LONG type)Input
Device values to be written
VARIANTvarData Input
(LONG array type)
(4) Explanation
* The device values for lSize(varSize) are batch-written to the devices, beginning
with the device specified in szDevice(varDevice).
* Store the device values to be written in lData (lplData or varData).
* Reserve an array of lSize (varSize) or more for lData (lplData or varData).
樣例程序:
For i = 0 To 5
If Val(Text1(i + 4)) = 0 Then
MsgBox "參數(shù)錯誤!請重新輸入"
Else
IData(220 + i) = Val(Text1(i + 4))
End If
Next i
IRet = frmMain.Q1.WriteDeviceBlock("D220", 6, IData(220))
Text1(10) = Val(Text1(4)) / Val(Text1(5))
Text1(11) = Val(Text1(6)) / Val(Text1(7))
Text1(12) = Val(Text1(8)) / Val(Text1(9))
生成數(shù)據(jù)報(bào)表。
由于生成報(bào)表個性化等特點(diǎn),更適合中小型監(jiān)控設(shè)備。
結(jié)束語:目前系統(tǒng)已經(jīng)運(yùn)行穩(wěn)定2年多。采用了三菱提供的控件編寫上位機(jī)器程序,省去了煩瑣冗長的通訊部分,開發(fā)人員只編寫流程處理和工業(yè)工程程序模塊。
我們可以說,用VB+MX-Component控件構(gòu)件中小型監(jiān)控系統(tǒng)是一種經(jīng)濟(jì)實(shí)用的方案。
參考文獻(xiàn):1.《MX Component Programming Manual》
------------------------三菱電機(jī)
2.《Visal Basic6 數(shù)據(jù)庫開發(fā)》-----E.winemiller,J.Roff
-----------------------清華大學(xué)出版