In the automation industry we are constantly looking for ways to improve our efficiency, streamline processes, and minimize errors to keep up with today’s expanding digital world. Part of our solution is to eliminate repetitive tasks and automate testing. One helpful tool is to automate reading and writing PLC tags via Excel.
There are a number of different uses for a tool like this including:
This blog will demonstrate how to use Microsoft Excel and Rockwell Automation’s RSLinx Classic software to “read” and “write” a Rockwell PLC’s tags. This process works for BOOL, DINT, INT, STRING, and FLOAT data types.
Let’s get started.
From RSLinx, click the DDE/OPC tab located in the navigation bar and click Topic Configuration in the dropdown option.
Select the newly created topic and navigate to the processor on the right-hand side of the window and click Apply. A popup will appear to confirm the update to the topic. Select Yes to complete the configuration of the topic in RSLinx.
Open Microsoft Excel and create a sheet named “REFS.” This sheet will contain the topic name (or names) that were setup in RSLinx as well as some data formatting information that will be used later for the PLC writes. In this example, enter the name of the configured topic, above, in cell A2 (“Excel_Topic”). Put the data formatting placeholders in the cells shown below as well.
Create a second sheet in the Excel workbook named “DATA” with columns for “Tag,” “Value”, and “New Value.” Next, enter the controller tag names (including the extensions for UDT/AOI types) in the “Tag” column as shown below.
Note: For program-scoped tags, use the syntax: "Program:<program_name>.<tagname>".
The Read button will execute a script that pulls the value of a tag into Excel. In this example, the script will refresh the data in the value column, by querying the tag names in the first column.
Private Sub ReadData_Click()
Dim row, rowLast, connResult As Integer
Dim tagName, topic As String
'Open the Connection to the PLC
topic = Worksheets("REFS").Range("A2")
connResult = DDEInitiate("RSLinx", topic)
'Verify that the Connection was made
If Err.Number <> 0 Then
MsgBox "Error Connecting to PLC", vbExclamation, "Error"
connResult = 0
End If
'Determine the number of the last row in column 'A' of the I/O List Sheet
rowLast = Cells(Rows.Count, "A").End(xlUp).row
'Scroll through the list of tags
For row = 2 To rowLast
tagName = Me.Cells(row, 1).Value
Cells(row, 2) = DDERequest(connResult, tagName + ",L1,C1" + Chr$(34) + ")")
If Err.Number <> 0 Then
MsgBox "Error Reading from PLC", vbExclamation, "Error"
connResult = 0
End If
Next row
'Sever the PLC Connection
DDETerminate (connResult)
End Sub
The Write button
Private Sub WriteData_Click()
Dim row, rowLast, connResult As Integer
Dim tagName, topic As String
'Open the Connection to the PLC
topic = Worksheets("REFS").Range("A2")
connResult = DDEInitiate("RSLinx", topic)
'Verify that the Connection was made
If Err.Number <> 0 Then
MsgBox "Error Connecting to PLC", vbExclamation, "Error"
connResult = 0
End If
'Determine the number of the last row in column 'A' of the I/O List Sheet
rowLast = Cells(Rows.Count, "A").End(xlUp).row
'Scroll through the list of tags
For row = 2 To rowLast
tagName = Me.Cells(row, 1).Value
Data = Me.Cells(row, 3).Value
'When the macro runs, the DATA tag resolves to a specific data type
' (Integer, float, string, etc).
'To use mix types, evaluate the info being passed, and write the value
' to a tag of the appropriate data type.
If IsNumeric(Data) Then
If Int(Data) = Data Then
XmitInt = CLng(Data)
Worksheets("REFS").Cells(2, 4) = Data
DDEPoke connResult, tagName, Worksheets("REFS").Cells(2, 4)
Else
XmitReal = CSng(Data)
Worksheets("REFS").Cells(3, 4) = Data
DDEPoke connResult, tagName, Worksheets("REFS").Cells(3, 4)
End If
Else
XmitString = CStr(Data)
Worksheets("REFS").Cells(4, 4) = Data
DDEPoke connResult, tagName, Worksheets("REFS").Cells(4, 4)
End If
If Err.Number <> 0 Then
MsgBox "Error Writing to PLC", vbExclamation, "Error"
connResult = 0
End If
Next row
'Sever the PLC Connection
DDETerminate (connResult)
End Sub
When the setup and configuration process is complete, press the “Read” button to display the values of the PLC tag in the “Value” column.
On the same “DATA” sheet, enter the desired value in the "New Value" column for each tag in column A. Press the “Write” button to update the tag values in the PLC.
Note: For BOOL tags, use '1' and ‘0’ and not 'True' and 'False'.
The example above is functional and may suit your needs. Since the connection is being made with Excel, there are many opportunities to customize the formatting, scripting, and layout to fit your use case.
While you are here, check out our other blog on how to use excel to create ladder logic within for your Rockwell Logix controller.