|
VBA WITH AB PLC
|
|
Source:Internet Author:Unknow Pubdate:2008-04-14
|
|
RAGEE (Electrical)
10 Jan 03 7:41
Does anyone have some sample macro's that they can share with me help me get started
klysitt (Electrical)
10 Jan 03 9:00
Hey RAGEE.
I designed a VB 6.0 HMI which interfaces with a SLC. A PLC-5 would be the same. Do a search in your RsLogix help for DDE (Dynamic Data Exchange). This is what you use to exchange data between your PLC and VB. There is example VB code in the help topics which tell you how to write(LinkPoke) data to a PLC or read (LinkRequest) from your PLC. The catch is that you need the full version of RSLinx OEM (about $1000) to do it. Linx Lite that comes with Logix won't support DDE. Also, if you are exchanging tons of data, DDE causes a noticable drag on your HMI also.
Their is also another option. http://www.plcdriver.com/ . These guys have an Allen Bradley OPC (OLE for Process Control) Server that is much faster and about $400 cheaper. If you download their ActiveX control it all but eliminates the VB code. Their help files are quite useful, suprisingly , and I learned everything I needed to know from them. 字串2
Hope this helps you out. It pretty much came down to whether the data was worth the money. The OPC Server has a free download that has full functionality but runs for only 2 hours. Check it out and see what you think. Good Luck
chakorules (Electrical)
11 Jan 03 16:13
When you say VBA, I assuming you are asking for an Excel Macro
Here is one I wrote:
Sub Start()
Dim lngRow As Long Dim varCycle As Variant Dim varLogging As Variant Dim varResults As Variant On Error GoTo Error 'opens a COLD DDE link RSIchan = DDEInitiate("RSLinx", "M1138") 'assign PLC bit values to VB variant varibles varLogging = DDERequest(RSIchan, "B3/163") varCycle = DDERequest(RSIchan, "B3/161") 字串4 'close COLD DDE link DDETerminate (RSIchan) 'check to see if the Cycle bit went to "1" if it did, excute read data If varCycle(1) = "1" And varLogging(1) = "1" Then
'starts at row 3 of sheet lngRow = 3 If Range("INDATA!A3").Value > 3 Then 'look up last cell and change position lngRow = Range("INDATA!A3").Value End If 'check until end of sheet 字串2 For lngRow = lngRow To 65500 'look for next empty cell If Cells(lngRow, 1) = "" Then Exit For 'write current cell location to sheet INDATA 'rather than writing a loop to search on 'every cycle, by the time the log is at row 21,500 'it could take a long time to search the rows... Range("INDATA!A3").Value = lngRow + 1 字串1 'add 1 to row "x" to check next row Next 'opens a COLD DDE link RSIchan = DDEInitiate("RSLinx", "M1138") 'there might be a better way to do this like 'using this somehow ???????? but I don't know how 'data = DDERequest(RSIchan, "f11:0,L7,C7") 'Range("[M1138.xls]LOG!R[x]C1:R[x]C7").Value = data 字串6 'read word F8:10 and load into a VB variant variable f810data = DDERequest(RSIchan, "F8:10") 'read word F8:11 and load into a VB variant variable f811data = DDERequest(RSIchan, "F8:11") 'read word F8:12 and load into a VB variant variable f812data = DDERequest(RSIchan, "F8:12") 'read word F8:16 and load into a VB variant variable f816data = DDERequest(RSIchan, "F8:16") 'read word F8:18 and load into a VB variant variable f818data = DDERequest(RSIchan, "F8:18") 字串7 'read word F8:17 and load into a VB variant variable f817data = DDERequest(RSIchan, "F8:17") 'read word F8:20 force check #1 and load into a VB variant variable f820data = DDERequest(RSIchan, "F8:20") 'read word F8:21 force check #2 and load into a VB variant variable f821data = DDERequest(RSIchan, "F8:21") 'read word F8:22 force check #3 and load into a VB variant variable f822data = DDERequest(RSIchan, "F8:22") 'read word F8:23 force check #4 and load into a VB variant variable f823data = DDERequest(RSIchan, "F8:23") 字串9 'read word F8:24 max force and load into a VB variant variable f824data = DDERequest(RSIchan, "F8:24") 'read word F8:25, get PASS or FAIL status from PLC and load into a VB variant variable varResults = DDERequest(RSIchan, "F8:25") 'close COLD DDE link DDETerminate (RSIchan) 'write all values to cells Cells(lngRow, 1).Value = f810data Cells(lngRow, 2).Value = f811data Cells(lngRow, 3).Value = f812data 字串4 Cells(lngRow, 4).Value = f816data Cells(lngRow, 5).Value = f818data Cells(lngRow, 6).Value = f817data Cells(lngRow, 7).Value = f820data Cells(lngRow, 8).Value = f821data Cells(lngRow, 9).Value = f822data Cells(lngRow, 10).Value = f823data Cells(lngRow, 11).Value = f824data Range("INDATA!A4").Value = varResults If Range("INDATA!A4").Value = 0 Then 字串6 Cells(lngRow, 12).Value = "PASS" 'copies the conditional format to the next cell Rows(lngRow + 1).Select Selection.Copy Rows(lngRow + 2).Select ActiveSheet.Paste Application.CutCopyMode = False Range("L" & lngRow + 2).Select 字串1 End If If Range("INDATA!A4").Value = 1 Then Cells(lngRow, 12).Value = "HEIGHT FAIL" 'copies the conditional format to the next cell Rows(lngRow + 1).Select Selection.Copy Rows(lngRow + 2).Select 字串3 ActiveSheet.Paste Application.CutCopyMode = False Range("L" & lngRow + 2).Select End If If Range("INDATA!A4").Value = 2 Then Cells(lngRow, 12).Value = "PARALLEL FAIL" 'copies the conditional format to the next cell 字串8 Rows(lngRow + 1).Select Selection.Copy Rows(lngRow + 2).Select ActiveSheet.Paste Application.CutCopyMode = False Range("L" & lngRow + 2).Select End If If Range("INDATA!A4").Value = 3 Then 字串2 Cells(lngRow, 12).Value = "FORCE FAIL" 'copies the conditional format to the next cell Rows(lngRow + 1).Select Selection.Copy Rows(lngRow + 2).Select ActiveSheet.Paste Application.CutCopyMode = False Range("L" & lngRow + 2).Select 字串5 End If If Range("INDATA!A4").Value > 3 Then Cells(lngRow, 12).Value = "ERROR!" End If 'capture time and date stamp into column 8 Cells(lngRow, 13).Value = Now() 'scrolls the screen down automatically 'Range("A" & lngRow + 1).Select 字串3 End If
GoTo Done: Error: frmError.Hide frmError.Show 'trying a timed form box, so disabled the old MESSAGE BOX here for now 'MsgBox ("Communications to PLC have been lost! Or Log Sheet is FULL! Can not log data anymore. Check PLC connection and wiring.") Application.Run Macro:="ChkTime"
Done:
'check for date change, if so then save as new sheet Application.Run Macro:="ChkTime" End Sub
You can download the whole program here:
http://www.mrplc.com/dl/index.php?action=view&view=entry&entryid=74
Or if you want a sample VB 6.0 AB to DDE program you can donwload a sample program I never did finish, but the DDE works in the program.
http://www.mrplc.com/dl/index.php?action=view&view=entry&entryid=58
字串7 The code for the above program looks a little bit different than VBA code, so make sure you get the right sample.
Good Luck! Chris Elston Automation & Controls Engineer http://www.mrplc.com Download Sample PLC Ladder Logic Code at MrPLC.com
(Click:)
|
| Previous:RS232 Control VB Script? Next:HMI aplications using RSlogix &VBA |
|
[ Add TO Favorites]
[TOP] [PRINT]
[CLOCE WINDOWS] |
|
|
|