Libnodave open a connection - S7 200 (PPI) - VB

lan12

Level-1
Beiträge
35
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo, ich schaffe es nicht in VB mit libdonave eine Verbidung zu meiner S7 CPU 224 (PPI Verbindung) zu öffnen. Als Basis habe ich das Progr. SimpleMPI aus dem Ordner dot.net / VB in Libnodave verwendet. Das geänderte Programm sieht wie folgt aus:

Code:
Class test
        Public Shared Function Main(ByVal args() As String) As Integer

        Dim localPPI As Integer = 0, plcPPI As Integer = 2

            Dim fds As libnodave.daveOSserialType
            Dim di As libnodave.daveInterface
            Dim dc As libnodave.daveConnection
            Dim res As Integer
            Dim a, b, c As Integer, d As Single
            Dim buf(1000) As Byte
        Dim s As String

        MsgBox("Hello World!")
        fds.rfd = libnodave.setPort(args(0), "9600", AscW("0"))  ' step 1, open a connection
            fds.wfd = fds.rfd
            If fds.rfd > 0 Then       ' if step 1 is ok
            di = New libnodave.daveInterface(fds, "My Interface 1", localPPI, libnodave.daveProtoPPI, libnodave.daveSpeed187k)
                di.setTimeout(1000000)  ' Make this longer if you have a very long response time
                res = di.initAdapter
                '
                If res = 0 Then       ' init Adapter is ok
                dc = New libnodave.daveConnection(di, plcPPI, 0, 0)  ' rack amd slot don't matter in case of MPI
                    res = dc.connectPLC()
                    If res = 0 Then
                        res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, buf)
                        If res = 0 Then
                            a = dc.getS32
                            b = dc.getS32
                            c = dc.getS32
                            d = dc.getFloat
                        MsgBox("FD0:  {0:d}", a)
                        MsgBox("FD4:  {0:d}", b)
                        MsgBox("FD8:  {0:d}", c)
                        MsgBox("FD12: {0:f}", d)
                        Else
                        MsgBox("Error {0:d}={1:s} in readBytes.", res, libnodave.daveStrerror(res))
                        End If
                        dc.disconnectPLC()
                    Else
                    MsgBox("Error {0:d}={1:s} in connectPLC.", res, libnodave.daveStrerror(res))
                    End If
                    di.disconnectAdapter()  ' End connection to adapter
                Else
                MsgBox("Error {0:d}={1:s} in initAdapter.", res, libnodave.daveStrerror(res))
                End If
                libnodave.closePort(fds.rfd)    ' Clean up
            Else
            MsgBox("Couldn't open serial port {0:s}", args(0))
                Return -1
            End If 'fds.rfd >0
        End Function 'Main
    End Class
... wobei für mich momentan lediglich die Zeilen

Public Shared Function Main(ByVal args() As String) As Integer

.... und ....

fds.rfd = libnodave.setPort(args(0), "9600", AscW("0"))

von Bedeutung sind. So stellt sich beispielsweise die Frage wie rufe ich die Public Shared Function über einen Button (Start) richtig auf?

Mit ...

Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click

Dim a As Integer
Dim b(1) As String
a = test.Main(b)

End Sub


...lässt sich die Funktion zwar aufrufen, liefert aber nicht das gewünschte Ergebniss, sprich eine Verbindung und das wahrscheinlich zu Recht. Welche Werte muss ich der Funktion beim Aufruf mit auf den Weg geben ?

Vielen Dank im Voraus,
Lan
 
Hallo
Hallo, ich schaffe es nicht in VB mit libdonave eine Verbidung zu meiner S7 CPU 224 ....
....Als Basis habe ich das Progr. SimpleMPI
Ich gehe davon aus Du hast ein einfaches PPI-Kabel/Adapter zur CPU224
und nimmst aber simple_MPI ?(was für die 300/400 gedacht ist). Du musst das das PPI_test (simplePPI wenns sowas gibt als Grundlage nehmen.
Dann gehts. PPI und MPI sind etwas unterschiedlich, und zumindest ohne den MPI-Adapter dazwischen (damit gibts einen Aufbau wo das evtl. geht) gibts da keine Connection.

... mit der dot.net DLL ist ja witzig ... werd ich in Delphi mal probieren vielleicht gehts denn in D2005 ja :rolleyes:
Gruss
tobias
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo Tobias, ja ich weis MPI und PPI sind 2 unterschiedliche Dinge. Für VB + PPI gibt es leider keine Vorlage, die grundsätzliche Struktur des Programmes sollte aber sehr ähnlich sein. Ich habe nun versucht die MPI Vorlage auf PPI umzubauen, zumal die testppi ja problemlos läuft und mich dazu ermutigt hat auf der Schiene weiterzuarbeiten.
 
Hallo
das 'Protokoll' bei MPI und PPI ist (zumindest da wo es die in 200 & 3-400 selben Bereiche tangiert) auch nicht gross unterschiedlich, nur den 'testMPI's ist die jeweilige Adapterinitiation vorgeschaltet.
Aber die scheint bei dir ja schon weggekürzt zu sein, jedenfalls was ich eben so sah.
Wenn Du am ComPort die Ausgangsbytes 'abliest' muss das bei PPI irgendwie immer mit 0x68, 2 x Lämngenbytes, 0x68, ... losgehen (war glaube ich so und wird ja bei testPPI.exe -d denn auch vollständig angezeigt) bei MPI gehts da irgendwie (je nach Adaptertype) mit 0x25 los, ... - da legt die CPU dann auf und bricht die Verbindung ab.
Gut - wenns das nicht ist stimmt die PPI-adr. ? Das wird ja (neuerdings) irgendwo mit übergeben oder ist default '2'
Ich weiss aber von VB kaum noch was, letzte Versuche waren in Visual Studio 6 annodunnemals.
Original siehts in 'c' ja so aus:
fds.rfd=setPort(argv[adrPos],"9600",'E');
fds.wfd=fds.rfd;
if (fds.rfd>0) {
di =daveNewInterface(fds, "IF1", localPPI, daveProtoPPI, daveSpeed187k);
dc =daveNewConnection(di, plcPPI, 0, 0);
daveConnectPLC(dc);

Dein:
fds.rfd = libnodave.setPort(args(0), "9600", AscW("0"))
machts vielleicht ja richtig, aber bei dem 'AscW("0")' kann ich nicht beurteilen worauf das 'abzielt' -- nicht das das die PPIadr. oder 'E' als Parität plattbügelt :confused:
War nur so ein Unterschied der mir eben visuell auffiel, ich nehme zumeist die Vorgängerversionen, da war das alles im Quelltext 'fixiert' und übergebe die Parameter seltener über die Kommandozeile. Da muss ich denn auch jedesmal probieren wodrauf das noch gerichtet war :rolleyes:
Gruss
tobias
 
Hallo Tobias, ja das war sozusagen meine zweite Vorlage

Code:
/*
 Very simple program to read from a 200 PLC via PPI cable.
 This shall provide users with a simple starting point for their own
 applications.
 
 Part of Libnodave, a free communication libray for Siemens S7 200/300/400
  
 (C) Thomas Hergenhahn (thomas.hergenhahn@web.de) 2005

 Libnodave is free software; you can redistribute it and/or modify
 it under the terms of the GNU Library General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 Libnodave is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU Library General Public License
 along with Libnodave; see the file COPYING.  If not, write to
 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
*/

using System;

class test {
    static libnodave.daveOSserialType fds;
    static libnodave.daveInterface di;
    static libnodave.daveConnection dc;
    static int localPPI=0;
    static int plcPPI=2;
    public static int Main (string[] args)
    {
    int i,a=0,j,res,b=0,c=0;
    float d=0;

        fds.rfd=libnodave.setPort(args[0],"9600",'E');
    fds.wfd=fds.rfd;
        if (fds.rfd>0) { 
        di =new libnodave.daveInterface(fds, "IF1", localPPI, libnodave.daveProtoPPI, libnodave.daveSpeed187k);
            di.setTimeout(1000000);
//        res=di.initAdapter();    // does nothing in PPI. But call it to keep your programs indpendent of protocols
//        if(res==0) {
        dc = new libnodave.daveConnection(di,plcPPI, 0, 0);
        if (0==dc.connectPLC()) {
            res=dc.readBytes(libnodave.daveFlags, 0, 0, 16, null);
            if (res==0) {
                a=dc.getS32();    
                b=dc.getS32();
                c=dc.getS32();
            d=dc.getFloat();
            Console.WriteLine("FD0: " + a);
            Console.WriteLine("FD4: " + b);
            Console.WriteLine("FD8: " + c);
            Console.WriteLine("FD12: " + d);
            } else 
            Console.WriteLine("error "+res+" "+libnodave.daveStrerror(res));
        }
        dc.disconnectPLC();
//        }        
//        di.disconnectAdapter();    // does nothing in PPI. But call it to keep your programs indpendent of protocols
        libnodave.closePort(fds.rfd);
    } else {
        Console.WriteLine("Couldn't open serial port "+args[0]);
        return -1;
    }    
    return 0;
    }
}
Ich habe also als Vorlage einen C# Code für PPI und einen VB Code für MPI, meine Aufgabe ist es nun daraus einen VB code für PPI zu erstellen. :rolleyes:
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Hier noch die Vorlage VB / MPI

Code:
' Very simple program to read from a 300/400 PLC via MPI adapter.
' This shall provide users wit a simple starting point for their own
' applications.
' 
' Part of Libnodave, a free communication libray for Siemens S7 200/300/400
'  
' (C) Thomas Hergenhahn (thomas.hergenhahn@web.de) 2005
'
' Libnodave is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2, or (at your option)
' any later version.
'
' Libnodave is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Libnodave; see the file COPYING.  If not, write to
' the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
'

Imports System
Imports Microsoft.VisualBasic

Class test
    Public Shared Function Main(args() As String) As Integer

    Dim localMPI As Integer = 0, plcMPI As Integer = 2
    
    Dim fds As libnodave.daveOSserialType
    Dim di as libnodave.daveInterface
    Dim dc as libnodave.daveConnection
    Dim res As Integer
    Dim a,b,c As Integer, d As Single
    Dim buf(1000) as byte
    Dim s As string
    
    Console.WriteLine("Hello World!")
        fds.rfd=libnodave.setPort(args(0), "38400", AscW("O"))    ' step 1, open a connection
    fds.wfd=fds.rfd
        if fds.rfd>0 then        ' if step 1 is ok
        di =new libnodave.daveInterface(fds, "My Interface 1", localMPI, libnodave.daveProtoMPI, libnodave.daveSpeed187k)
            di.setTimeout(1000000)    ' Make this longer if you have a very long response time
        res = di.initAdapter
'
        if res=0 then        ' init Adapter is ok
        dc = new libnodave.daveConnection(di, plcMPI, 0, 0)  ' rack amd slot don't matter in case of MPI
        res=dc.connectPLC()
        if res=0 then
            res=dc.readBytes(libnodave.daveFlags, 0, 0, 16, buf)
            if res=0 then
                a=dc.getS32
                b=dc.getS32
                c=dc.getS32
            d=dc.getFloat
            Console.WriteLine("FD0:  {0:d}", a)
            Console.WriteLine("FD4:  {0:d}", b)
            Console.WriteLine("FD8:  {0:d}", c)
            Console.WriteLine("FD12: {0:f}", d)
            else
            Console.WriteLine("Error {0:d}={1:s} in readBytes.", res, libnodave.daveStrerror(res))
            end if
            dc.disconnectPLC()
        else
            Console.WriteLine("Error {0:d}={1:s} in connectPLC.", res, libnodave.daveStrerror(res))
        end if
        di.disconnectAdapter()    ' End connection to adapter
        else
        Console.WriteLine("Error {0:d}={1:s} in initAdapter.", res, libnodave.daveStrerror(res))
        end if
        libnodave.closePort(fds.rfd)    ' Clean up
    else
        Console.WriteLine("Couldn't open serial port {0:s}", args(0))
        return -1
    end if 'fds.rfd >0
    end Function 'Main
END class


Der Code eingangs ist mein Versuch das Ganze auf PPI umzubauen
 
fds.rfd=libnodave.setPort(args[0],"9600",'E');
der meine ich ist richtig - oder sprich: Stimmt mit dem überein was ich benutze.
Die Parität müsste 'E' ==even sein, die Baudrate ja wie eingestellt.
'Ein Vordruck weiter' ist die Parity allerdings auf 'O' (odd) umgemünzt. (Ich hatte letztesmal noch '0==Null' gelesen. 'AscW("O")' wird dann aber die Parität in odd überschreiben und da steht die Kiste
fds.rfd=libnodave.setPort(args(0), "38400", AscW("O"))
Abgesehen davon dass die PPI_speed auf 9600, 19200 und 187500 'eingegrenzt' ist, meine ich da hängts am 'odd':confused:
Wir hatten das in grauer Vorzeit bei uns im setport.c mal durchgetestet und da ging nur 'even'.
Den Rest hätte ich auch wohl in etwa so 'umzubauen' angefangen - bei VB hatte ich allerdings früher schon mal einige Schwierigkeiten auch im Timing zur Schnittstelle. Geräte die wundersamerweise in QBasic bestens funktionierten starben in VB umcompilert plötzlich den Timeouttod. Null Ahnung aber wie das 'heutzutage ist' - nur bei Delphi war es in der/ einer dot.net Version auch so. Bei D5 lief das wie geschmiert, in D2005 plötzlich stand alles still:rolleyes:
 
Hallo Tobias, hey der Tipp mit dem "even" war gut, es läuft yipi !

Code:
 fds.rfd = libnodave.setPort("COM1", "9600", AscW("e"))  ' step 1, open a connection
            fds.wfd = fds.rfd
Wenn ich das Programm starte bekomme ich die Meldungen :

Hello World !
FD0 : {0:d}
FD4 : {0:d}
FD8 : {0:d}
FD12 : {0:d}

anstatt "Couldn't open serial port ...."

das ist doch schon mal was :D

Hier der gesamte Code

Code:
Imports System
Imports Microsoft.VisualBasic

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        Dim a As Integer
        Dim b(1) As String
        a = test.Main(b)
    End Sub
End Class

    Class test
        Public Shared Function Main(ByVal args() As String) As Integer

        Dim localPPI As Integer = 0, plcPPI As Integer = 2

            Dim fds As libnodave.daveOSserialType
            Dim di As libnodave.daveInterface
            Dim dc As libnodave.daveConnection
            Dim res As Integer
            Dim a, b, c As Integer, d As Single
            Dim buf(1000) As Byte
        ' Dim s As String

        MsgBox("Hello World!")
        fds.rfd = libnodave.setPort("COM1", "9600", AscW("e"))  ' step 1, open a connection
            fds.wfd = fds.rfd
            If fds.rfd > 0 Then       ' if step 1 is ok
            di = New libnodave.daveInterface(fds, "My Interface 1", localPPI, libnodave.daveProtoPPI, libnodave.daveSpeed187k)
                di.setTimeout(1000000)  ' Make this longer if you have a very long response time
                res = di.initAdapter
                '
                If res = 0 Then       ' init Adapter is ok
                dc = New libnodave.daveConnection(di, plcPPI, 0, 0)  ' rack amd slot don't matter in case of MPI
                    res = dc.connectPLC()
                    If res = 0 Then
                        res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, buf)
                        If res = 0 Then
                            a = dc.getS32
                            b = dc.getS32
                            c = dc.getS32
                            d = dc.getFloat
                        MsgBox("FD0:  {0:d}", a)
                        MsgBox("FD4:  {0:d}", b)
                        MsgBox("FD8:  {0:d}", c)
                        MsgBox("FD12: {0:f}", d)
                        Else
                        MsgBox("Error {0:d}={1:s} in readBytes.", res, libnodave.daveStrerror(res))
                        End If
                        dc.disconnectPLC()
                    Else
                    MsgBox("Error {0:d}={1:s} in connectPLC.", res, libnodave.daveStrerror(res))
                    End If
                    di.disconnectAdapter()  ' End connection to adapter
                Else
                MsgBox("Error {0:d}={1:s} in initAdapter.", res, libnodave.daveStrerror(res))
                End If
                libnodave.closePort(fds.rfd)    ' Clean up
            Else
            MsgBox("Couldn't open serial port {0:s}", args(0))
                Return -1
            End If 'fds.rfd >0
        End Function 'Main
    End Class
Genug für heute, weitergehts im neuen Jahr. In diesem Sinn Frohe Weihnachten und einen Guten Rutsch!
 
Zurück
Oben