Author Topic: Reading the CIP Identity of any Ethernet/IP device  (Read 4871 times)

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Reading the CIP Identity of any Ethernet/IP device
« on: November 30, 2020, 10:36:26 PM »
This is just an example code - hopefully accurate, combination of these wiki topics:

   https://advancedhmi.com/documentation/index.php?title=Reading_the_CIP_Identity_of_any_Ethernet/IP_device
   https://advancedhmi.com/documentation/index.php?title=Generic_Ethernet/IP_and_CIP_Technical_Information
   https://advancedhmi.com/documentation/index.php?title=Class_Codes_for_ControlLogix

See the attached picture of the MainForm which is reflective of the code.

Everything can be further simplified by:

   - removing all the buttons and moving their action codes to the MainForm's Load event
   - using only 1 textbox and appending the received values to the text

Code: [Select]
    Private AttributeReadDeviceProductCode As UInteger
    Private AttributeReadDeviceProcessorStatus As UInteger
    Private AttributeReadDeviceName As UInteger
    Private AttributeReadDeviceSerialNumber As UInteger

    Private Sub ButtonProcessorStatus_Click(sender As Object, e As EventArgs) Handles btnProcessorStatus.Click
        AttributeReadDeviceProcessorStatus = EthernetIPforCLXCom1.BeginGetAttributeSingle(1, 1, 5)
    End Sub

    Private Sub ButtonProductCode_Click(sender As Object, e As EventArgs) Handles btnProductCode.Click
        AttributeReadDeviceProductCode = EthernetIPforCLXCom1.BeginGetAttributeSingle(1, 1, 3)
    End Sub

    Private Sub ButtonDeviceName_Click(sender As Object, e As EventArgs) Handles btnDeviceName.Click
        AttributeReadDeviceName = EthernetIPforCLXCom1.BeginGetAttributeSingle(1, 1, 7)
    End Sub

    Private Sub ButtonSerialNumber_Click(sender As Object, e As EventArgs) Handles btnSerialNumber.Click
        AttributeReadDeviceSerialNumber = EthernetIPforCLXCom1.BeginGetAttributeSingle(1, 1, 6)
    End Sub

    Private Sub EthernetIPforCLXCom1_DataReceived(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles EthernetIPforCLXCom1.DataReceived
        If e.ErrorId = 0 AndAlso e.Values IsNot Nothing AndAlso e.Values.Count > 0 Then
            If e.TransactionNumber = AttributeReadDeviceProcessorStatus Then
                Dim bitString As String = Convert.ToString(CInt(e.Values(0)), 2).PadLeft(16, "0"c)
                Dim status As UInteger

                For i = 4 To 7
                    If bitString(15 - i) = "1" Then
                        status += 2 ^ (i - 4)
                    End If
                Next

                Select Case status
                    Case 0
                        TextBoxStatus.Text = "Self-Testing or Unknown"
                    Case 1
                        TextBoxStatus.Text = "Firmware Update in Progress"
                    Case 2
                        TextBoxStatus.Text = "Reserved"
                    Case 3
                        TextBoxStatus.Text = "Reserved"
                    Case 4
                        TextBoxStatus.Text = "Non-Volatile Configuration Bad"
                    Case 5
                        TextBoxStatus.Text = "Major Fault - either bit 10 or bit 11 is true (1)"
                    Case 6
                        TextBoxStatus.Text = "Programmable logic executing (Run)"
                    Case 7
                        TextBoxStatus.Text = "Programmable logic is not executing (Idle/Program Mode)"
                End Select

                If Val(bitString(8)) = 1 Then
                    TextBoxFault.Text = "Recoverable Minor Fault"
                ElseIf Val(bitString(9)) = 1 Then
                    TextBoxFault.Text = "Unrecoverable Minor Fault"
                ElseIf Val(bitString(10)) = 1 Then
                    TextBoxFault.Text = "Recoverable Major Fault"
                ElseIf Val(bitString(11)) = 1 Then
                    TextBoxFault.Text = "Unrecoverable Major Fault"
                End If

                Dim modeSwitch As UInteger

                For i = 12 To 13
                    If bitString(15 - i) = "1" Then
                        modeSwitch += 2 ^ (i - 12)
                    End If
                Next

                Select Case modeSwitch
                    Case 1
                        TextBoxKeyswitch.Text = "Keyswitch in Run Mode"
                    Case 2
                        TextBoxKeyswitch.Text = "Keyswitch in Program Mode"
                    Case 3
                        TextBoxKeyswitch.Text = "Keyswitch in Remote Mode"
                End Select

            ElseIf e.TransactionNumber = AttributeReadDeviceProductCode Then
                Dim ProductCode As UInteger = e.Values(0)

                Select Case ProductCode
                    Case 3
                        TextBoxProductCode.Text = "(Code 3) ControlLogix5550"
                    Case 15
                        TextBoxProductCode.Text = "(Code 15) SoftLogix5860 1789-L60"
                    Case 40
                        TextBoxProductCode.Text = "(Code 40) ControlLogix5550 1756-L1"
                    Case 41
                        TextBoxProductCode.Text = "(Code 41) FlexLogix5433"
                    Case 43
                        TextBoxProductCode.Text = "(Code 43) CompactLogix5320 1769-L20"
                    Case 44
                        TextBoxProductCode.Text = "(Code 44) CompactLogix5330 1769-L30"
                    Case 48
                        TextBoxProductCode.Text = "(Code 48) PowerFlex with DriveLogix5720"
                    Case 49
                        TextBoxProductCode.Text = "(Code 49) PowerFlex with DriveLogix5725"
                    Case 50
                        TextBoxProductCode.Text = "(Code 50) ControlLogix5553 1756-L53"
                    Case 51
                        TextBoxProductCode.Text = "(Code 51) ControlLogix5555 1756-L55"
                    Case 52
                        TextBoxProductCode.Text = "(Code 52) PowerFlex with DriveLogix5730"
                    Case 53
                        TextBoxProductCode.Text = "(Code 53) Studio 5000 Logix Emulate"
                    Case 54
                        TextBoxProductCode.Text = "(Code 54) ControlLogix5561 1756-L61"
                    Case 55
                        TextBoxProductCode.Text = "(Code 55) ControlLogix5562 1756-L62"
                    Case 56
                        TextBoxProductCode.Text = "(Code 56) ControlLogix5563 1756-L63"
                    Case 57
                        TextBoxProductCode.Text = "(Code 57) ControlLogix5564 1756-L64"
                    Case 64
                        TextBoxProductCode.Text = "(Code 64) CompactLogix5331 1769-L31"
                    Case 65
                        TextBoxProductCode.Text = "(Code 65) CompactLogix5335E 1769-L35E"
                    Case 67
                        TextBoxProductCode.Text = "(Code 67) GuardLogix 1756-L61S"
                    Case 68
                        TextBoxProductCode.Text = "(Code 68) GuardLogix 1756-L62S"
                    Case 69
                        TextBoxProductCode.Text = "(Code 69) GuardLogix 1756-LSP"
                    Case 72
                        TextBoxProductCode.Text = "(Code 72) CompactLogix 1768-L43"
                    Case Else
                        TextBoxProductCode.Text = String.Format("(Code {0}) Not Defined", ProductCode)
                End Select

            ElseIf e.TransactionNumber = AttributeReadDeviceName Then
                Dim StringLength As UInteger = e.Values(0)

                Dim index As Integer
                Dim Identity As String = ""
                While index < e.Values.Count - 1 AndAlso index < StringLength
                    Identity &= Chr(e.Values(index + 1))
                    index += 1
                End While

                TextBoxDeviceName.Text = Identity
            ElseIf e.TransactionNumber = AttributeReadDeviceSerialNumber Then
                Dim SerialNumber As UInteger = e.Values(0)

                TextBoxSerial.Text = SerialNumber
            End If
        End If
    End Sub

You could also try getting the device type/vendor name, should be attributes (1, 1, 1) and (1, 1, 2), but would have to check "device" and "vendor" listings here:

   https://github.com/dmroeder/pylogix/blob/master/pylogix/lgx_device.py

You could simplify these listings by only using the entries that are relevant to your setup.
The approach would be fairly similar to the "Product Code" section of the above code.
« Last Edit: January 15, 2021, 05:12:17 AM by Godra »