Donate Share

libnodave

Subscribe

LIBNODAVE AND CP5611 AND CP5612

  1. nobody

    2009-10-28 13:20:35 UTC

    Hello. Very good stuff. THANK YOU very much. I have CP5611(PCI Card) and CP5612(PCIMCIA Card). I use S7ONLINE protocol to make connection with S7-300 315-2DP by MPI. I found example in internet, and little change it for my needs. When I use CP5612 everything works great, but when I use CP5611 I cannot close connection. It looks like closing instruction didn't work. Below is my code: ////////////////////////////////////////////////////////////////////////////// Public Class Form1 Public Value As Integer = 0 Dim localMPI As Integer = 0, plcMPI As Integer = 2 Dim fds As libnodave.daveOSserialType Dim di As libnodave.daveInterface Dim dc As libnodave.daveConnection

    Private Sub btnAufbau_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnAufbau.Click
        Dim Rack As Integer = 0
        Dim Slot As Integer = 0
        PLCConnect_NEW(2, 0, 2)
    End Sub
    
    Private Function PLCConnect_NEW(ByVal MPIAddress As String, Optional ByVal PLCRack As Integer = 0, Optional ByVal PLCSlot As Integer = 2) As Boolean
        Dim ConnectionOpened As Boolean
        Dim initSuccess As Integer = 0
        Dim i As Integer
        Try
            ListBox1.Items.Clear()
            dc.disconnectPLC()
            di.disconnectAdapter()
            fds.rfd = libnodave.closeS7online(fds.rfd)
    
            For i = 0 To 3
                fds.rfd = libnodave.openS7online("S7ONLINE")
                fds.wfd = fds.rfd
                If fds.rfd > 0 Then
                    Exit For
                End If
            Next
            If (fds.rfd > 0) Then
                di = New libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoS7online, libnodave.daveSpeed187k)
                di.setTimeout(10000)
                Dim j As Integer
                For j = 0 To 2
                    If (0 = di.initAdapter) Then
                        initSuccess = 1
                        Exit For
                    Else
                        di.disconnectAdapter()
                    End If
                Next
                ListBox1.Items.Clear()
                If initSuccess <> 1 Then
                    ListBox1.Items.Add("Couldn't connect to Adapter!")
                    Exit Function
                End If
                dc = New libnodave.daveConnection(di, MPIAddress, PLCRack, PLCSlot)
                Dim ErrorCode As Integer = dc.connectPLC()
                If ErrorCode = 0 Then
                    ListBox1.Items.Add("Connection opened.")
                    ConnectionOpened = True
                    Timer2.Enabled = True
                Else
                    ListBox1.Items.Add("Error opening connection. Check rack & slot." & " = " & libnodave.daveStrerror(ErrorCode))
                    ConnectionOpened = False
                End If
            Else
                ListBox1.Items.Add("Error opening access point.")
                ConnectionOpened = False
            End If
            Return ConnectionOpened
    
        Catch ex As Exception
            ' Error ("Couldn't open connection to " PLC) 
            For i = 0 To 3
                fds.rfd = libnodave.openS7online("S7ONLINE")
                fds.wfd = fds.rfd
                If fds.rfd > 0 Then
                    Exit For
                End If
            Next
            If (fds.rfd > 0) Then
                di = New libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoS7online, libnodave.daveSpeed187k)
                di.setTimeout(10000)
                Dim j As Integer
                For j = 0 To 2
                    If (0 = di.initAdapter) Then
                        initSuccess = 1
                        Exit For
                    Else
                        di.disconnectAdapter()
                    End If
                Next
                ListBox1.Items.Clear()
                If initSuccess <> 1 Then
                    ListBox1.Items.Add("Couldn't connect to Adapter!")
                    Exit Function
                End If
                dc = New libnodave.daveConnection(di, MPIAddress, PLCRack, PLCSlot)
                Dim ErrorCode As Integer = dc.connectPLC()
                If ErrorCode = 0 Then
                    ListBox1.Items.Add("Connection opened.")
                    ConnectionOpened = True
                    Timer2.Enabled = True
                    'comm = 1
                Else
                    ListBox1.Items.Add("Error opening connection. Check rack & slot." & " = " & libnodave.daveStrerror(ErrorCode))
                    ConnectionOpened = False
                End If
            Else
                ListBox1.Items.Add("Error opening access point.")
                ConnectionOpened = False
            End If
            Return ConnectionOpened
        End Try
    End Function
    
    Private Sub PLCDisConnect()
        'btnAufbau.Show()
        'Button2.Hide()
        Timer2.Enabled = False
        'comm = 0
        ListBox1.Items.Clear()
        dc.disconnectPLC()
        di.disconnectAdapter()
        fds.rfd = libnodave.closeS7online(fds.rfd)
        ListBox1.Items.Add("Disconnected.")
    End Sub
    
    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim ReadOK As Boolean = True
        Dim ReceivedDataBytes(11) As Byte
        Dim SendDataBites(0) As Byte
        Dim RetVal As Integer
        SendDataBites(0) = 1
        ListBox1.Items.Clear()
        RetVal = dc.readBytes(132, 3, 0, 12, ReceivedDataBytes)
        If RetVal = 0 Then
            Dim i As Integer
            For i = 0 To ReceivedDataBytes.Length - 1
                ListBox1.Items.Add(ReceivedDataBytes(i))
            Next
        Else
            ReadOK = False
            ListBox1.Items.Add("Read error: " & RetVal & " = " & libnodave.daveStrerror(RetVal))
            PLCConnect_NEW(2, 0, 2)
        End If
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PLCDisConnect()
    End Sub
    

    End Class

    Thank You very much for any help.

< Previous | 1 | Next >

Add a Reply

You can use Markdown syntax in your reply.

Preview

Monitor this

Log in to monitor this topic. Not registered? Create an account to receive email updates when replies are posted to this topic. If you do not log in or register, your reply will be added anonymously.