Need some help understanding some code reguarding Dynamic Data Exchange(DDE)
-
יום שני 16 אפריל 2012 12:03
Hello forum,
I have been using Excel 2010 to work with PLCs for a while now. I just recently found that using some VBA that I could write back to the PLC, not just only read from it. I have got the code to work for my specific setup, but I do not fully understand what is going on in the code so I was hoping some of you all could help me out.
First, I got the code from here: http://www.plcdev.com/connecting_controllogix_excel So all credit for this code goes to John Schop.
Here is the code modified for my use:
'CT1, Send data copied bit----------------------------------------- Private Function OpenRSLinx_CT1() On Error Resume Next 'Open the connection to RSLinx OpenRSLinx = DDEInitiate("RSLINX", "CYL_1") 'Check if the connection was made If Err.Number <> 0 Then MsgBox "Error Connecting to topic", vbExclamation, "Error" OpenRSLinx = 0 'Return false if there was an error End If End Function Sub DataCopied_CT1() rslinx = OpenRSLinx_CT1() 'Open connection to RSlinx i = 0 dintdata = DDERequest(rslinx, "EXCEL_TEST[" & i & "],L1,C1") If TypeName(dintdata) = "Error" Then If MsgBox("Error reading tag EXCEL_TEST[" & i & "]. " & _ "Continue with write?", vbYesNo + vbExclamation, _ "Error") = vbNo Then Exit Sub Else DDEPoke rslinx, "EXCEL_TEST[" & i & "]", Cells(13, 5) End If DDETerminate rslinx End SubWhat I am having a problem with understanding is the DDERequest method and the DDEPoke method.
Also, the code from the website was written for writing to an array for multiple values. However, in my situation I only need to write back a zero after an event occurs. I imagine the code could be changed to run better for this but as I do not fully understand what is going on, I have not properly editied the code.
Thanks for your help everyone!
Auburn University Student IT/MIS Intern War Eagle!
כל התגובות
-
יום שני 16 אפריל 2012 13:02
Did you check the MSDN library webpages? The DDE method sets up a client/server connection between two databases (excel is sometimes considered a database). It is not really Access or Excel that are doing the connection but an underlying driver like the Microsoft Jet engine that is really making the connection.
It looks like from the example you posted the code makes refference to a workbook ""CYL_1" and a worksheet "EXCEL_TEST[" & i & "]. I guess the workbook has sheets like "Excel_Test[1]", "Excel_Test[2]".
See this webpage
http://msdn.microsoft.com/en-us/library/aa221181(v=office.11).aspx
All the DDE methods are on the left side of the page.
jdweng
-
יום שני 16 אפריל 2012 13:30
Thanks!
I read over the pages but I still don't have a good grip on what is going on in the code.
I know that CYL_1 is the topic name in the PLC and EXCEL_TEST is the array variable in the PLC that I am writing back to but there are some other things I am not sure about.
At the end of this line there is a L1 and a C1:
dintdata = DDERequest(rslinx, "EXCEL_TEST[" & i & "],L1,C1")
I do not know what the L1 or the C1 represent or what they do as there is nothing in the example that was provided on the website I listed early in cells L1 or C1. It may have to do with length or channel number but I don't know. Because the code is looking at the PLC at this point in the code, I think it has something to do with the variable EXCEL_TEST.
In the Excel example provided on the site, John uses the L1,C1 part for both arrays of REAL type and DINT type.
Also, does "DDETerminate rslinx" only terminate the connection to the specific channel/topic opened or does it terminate all connections to RSlinx channels?
Sorry for all the questions and uncertainies!
Auburn University Student IT/MIS Intern War Eagle!
-
יום שני 16 אפריל 2012 13:41
First, L1 and C1 are cell on the worksheet.
1) Now lets go back to basic Class methods. The function below is a class. Everytime a new is used another copy of this class is created.
Private Function OpenRSLinx_CT1()
'your code is here
End Function
2) The statment below calls the class constructor
rslinx = OpenRSLinx_CT1() 'Open connection to RSlinx
3) The statement below terminates the instance of the class create in 2 above.
DDETerminate rslinx
jdweng
- סומן כתשובה על-ידי Bruce SongModerator יום שני 23 אפריל 2012 07:07