Author Topic: Properly Closing EthernetIPforSLCMicroCom Connections  (Read 1687 times)

sds5150

  • Newbie
  • *
  • Posts: 14
    • View Profile
Properly Closing EthernetIPforSLCMicroCom Connections
« on: January 18, 2017, 09:34:57 AM »

I have created a DLL that does the PLC work and is called from a "Main" application.
I feel like I need to close or dispose the  EthernetIPforSLCMicroCom PLC driver.
I tried using "USING/End USING" instead of Dim to create "EthernetCommsSLCL3" and also "EthernetCommsSLCL3.CloseConnection()" but doth seemed to stop the communications permanently.
Do you have any advice? (Sorry for the lengthy code, I wanted to be sure everything was there for clarification)
Thanks!!!
Also, is there a way to format my code when I post it?
------------------------------------------------------------------------------



    'L3  Variables
    Dim tmrL3 As New System.Timers.Timer
    Dim PLCAddressL3 As String = "10.124.233.16"
    'Dim SLCBitToReadL3 As String = "B21:2/4"
    Dim SLCIntToReadL3 As String = "N7:200"
    Dim SLCBitToWrite1L3 As String = "B21:2/5"
    Dim SLCBitToWrite2L3 As String = "B21:2/6"
    Dim SLCHeartbeatBitL3 As String = "B21:2/7"
    'PLCAddress-PLC IP ADDRESS
    'SLCBitToRead-Label Applied
    'SLCBitToWrite1-Stop RollFormer
    'SLCBitToWrite2-Start RollFormer
    'SLCHeartbeatBit-Monitor Communications B/W APP and PLC
    Dim MachineRunning As Boolean
    Dim MachineNotRunning As Boolean
    Public BitWrittenStartL3 As Boolean
    Public BitWrittenStopL3 As Boolean
    Public MachineStatusL3 As Int32 = 0
    Dim MachineStatusChangedL3 As Int32 = 0
    Dim HeartbeatL3 As Boolean

_____________________________________________________________
    Public Sub initializeTimerL3()
        tmrL3.Interval = 1500
        tmrL3.Enabled = True
        AddHandler tmrL3.Elapsed, AddressOf tmr_elapsedL3

        Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                                    System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
            writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & "Status:L3 DLL initialized by main app")
            writer.Close()
        End Using

    End Sub
______________________________________________________________

    'These functions will be called from the print application to start and stop the machine. If the variables are in the correct state, the PLC
    'tag will be written And the VB variable immediately turned off.
    'If the variables are "stuck" on for some reason, they are turned off and the correct variable set
    'The "returned" variable is feedback to the main application.
    Public Function MachineOn() As Boolean

        'NOTE:This is how to call this function from another class
        'mainObj.MachineOn()

        Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                                     System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
            writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & "Status:L3 Machine Started Function Active in DLL")
            writer.Close()
        End Using

        'Checks and sets appropriate variables in preperation for writing the "ON" signal to the PLC
        If BitWrittenStartL3 = False And BitWrittenStopL3 = False Then
            BitWrittenStartL3 = True
            MachineRunning = True
        Else
            'Resets variables
            BitWrittenStartL3 = False
            BitWrittenStopL3 = False
            'Sets correct state
            BitWrittenStartL3 = True
            MachineRunning = True
        End If
        Return MachineRunning


    End Function
_________________________________________________________

    Public Function MachineOff() As Boolean

        Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                                       System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
            writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & "Status:L3 Machine Stopped Function Active in DLL")
            writer.Close()
        End Using

        'Checks and sets appropriate variables in prep for writing the "OFF" signal to the PLC
        If BitWrittenStopL3 = False And BitWrittenStartL3 = False Then
            BitWrittenStopL3 = True
            MachineNotRunning = True
        Else
            'Resets variables
            BitWrittenStopL3 = False
            BitWrittenStartL3 = False
            'Sets correct state
            BitWrittenStopL3 = True
            MachineNotRunning = True
        End If
        Return MachineNotRunning

    End Function
_________________________________________________________


'This code runs continuously on a pulse after the DLL is started to update PLC info
    Public Sub tmr_elapsedL3()

        Try

            Dim EthernetCommsSLCL3 As New AdvancedHMIDrivers.EthernetIPforSLCMicroCom
            EthernetCommsSLCL3.IPAddress = PLCAddressL3
            EthernetCommsSLCL3.PollRateOverride = 1000


            'If Application Then sets BitWrittenStart To True, PLC will be updated With True Else False
            'BitWrittenStart set True will start the Rollformer
            If BitWrittenStartL3 = True Then
                EthernetCommsSLCL3.Write(SLCBitToWrite1L3, 1)

                Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                   System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
                    writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & "Status:PLC START bit written to 1")
                    writer.Close()
                End Using

                BitWrittenStartL3 = False
            Else
                EthernetCommsSLCL3.Write(SLCBitToWrite1L3, 0)
            End If

            'If application sets BitWrittenStop to True, PLC will be updated with True else False
            'BitWrittenStop set True will stop the Rollformer
            If BitWrittenStopL3 = True Then
                EthernetCommsSLCL3.Write(SLCBitToWrite2L3, 1)

                Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
                    writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & "Status:PLC STOP bit written to 1")
                    writer.Close()
                End Using

                BitWrittenStopL3 = False
            Else
                EthernetCommsSLCL3.Write(SLCBitToWrite2L3, 0)
            End If

            'Machine Status value from PLC for reference in Print Application
            'TRIGGERED EVERY TIME THE TIMER COMPLETES
            MachineStatusL3 = EthernetCommsSLCL3.Read(SLCIntToReadL3)

            'If the machine status value changes, log that value to the diagnostic file
            If MachineStatusChangedL3 <> MachineStatusL3 Then
                MachineStatusChangedL3 = MachineStatusL3

                Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
                    writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & "Status:PLC status value changed to " & MachineStatusChangedL3 & vbCrLf _
                             & "(10=started     20=Stopped     30=Print App bypassed)")
                    writer.Close()
                End Using

            End If
            'Handshake to verify good communications between PLC and Application
            'TRIGGERED EVERY TIME THE TIMER COMPLETES
            HeartbeatL3 = EthernetCommsSLCL3.Read(SLCHeartbeatBitL3)
            If HeartbeatL3 = True Then
                EthernetCommsSLCL3.Write(SLCHeartbeatBitL3, 0)
            End If

            'EthernetCommsSLCL3.CloseConnection()



        Catch ex As Exception

            'create error/Status log and document exception or status with date and time
            'File is named with today's date to keep file sizes small
            Using writer As New StreamWriter(Application.StartupPath & "\SLC_DLL Diagnostic Log" &
                                    System.DateTime.Now.ToString("yyyyMMdd") & ".log", True, System.Text.Encoding.ASCII)
                writer.WriteLine(vbCrLf & DateAndTime.Now & vbCrLf & ex.ToString)
                writer.Close()
            End Using

        End Try

    End Sub


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Properly Closing EthernetIPforSLCMicroCom Connections
« Reply #1 on: January 19, 2017, 08:29:12 AM »
Your better off using an instance that is declared outside of a routine and let it keep the connection open. It takes quite a bit of overhead to establish an Ethernet/IP connection and a few packets to close it. After approximately 20 seconds, if there is no activity, the connection will automatically close. Also when the driver is disposed, it will perform a proper closing of the connection.