S7 Datum von VB.NET nach SPS...

paddy_cmd

Level-1
Beiträge
16
Reaktionspunkte
0
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo,
ich habe wiedermal ein Problem mit meinen libnodave Funktionen.

Nach dem erfolgreichen schreiben von strings in die SPS muss ich nun ein datum in die SPS schreiben.

Dazu habe ich mir mit der Funktion DateDiff die Tage zwischen dem zu schreibenden Datum un 1.1.1990 berechnen lassen und versucht damit zu schreiben:!

Code:
Public Function SetVal(ByVal nDBnumber As Integer, ByVal Datetype As String, _
                           ByVal nStart As Integer, ByVal nVal2Write As String) As Boolean

        Dim res As Integer
        Dim buf(1000) As Byte
        Dim nArea As Integer
        Dim temp As Integer
        Dim sValue As String
        Dim test() As Integer
        Dim nBytes2Write As Integer
        Dim datum As UShort


        If (nDBnumber >= 0) Then     ' Wenn der Wert aus der combobox >= 0 ist suche nach dem DB!

            nArea = libnodave.daveDB
        Else
            Return True 'Ansonsten gebe den Wert "nUll" Wieder!
        End If


        'Try
        If Not _CONNECTEION_STATUS_OK Then
            MsgBox("keine Verbindung")
            Exit Function
        End If

        Select Case Datetype
            Case "Byte"
                nBytes2Write = 1
                temp = CByte(nVal2Write) 'CShort(nVal2Write)
                buf = GetBytes(temp)
            Case "Integer"
                nBytes2Write = 2
                temp = libnodave.daveSwapIed_16(CInt(nVal2Write)) 'CShort(nVal2Write)
                buf = GetBytes(temp)
            Case "Float"
                nBytes2Write = 4
                temp = libnodave.daveToPLCfloat(CSng(nVal2Write))
                buf = GetBytes(temp)
            Case "String"
                nBytes2Write = 8
                buf = stringToByteArray(nVal2Write)
            Case "Longint"
                nBytes2Write = 8
                temp = CInt(nVal2Write)
                buf = GetBytes(temp)
            Case "Date"
                nBytes2Write = 2
                datum = CUShort(nVal2Write)
                buf = GetBytes(datum)

            Case "Bit"

                Return Nothing
            Case Else
                Return Nothing
        End Select

        res = dc.writeBytes(nArea, nDBnumber, nStart, nBytes2Write, buf)
        If res = 0 Then
            Return True
        Else
            Return False
        End If

    End Function
Das zu schreibende datum ist der 1.1.2000 und geschreiben wird der 13.09.2034

Ich weis net woran das liegt!
Hoffe von euch kann mir jemand helfen!

Vielen Dank im voraus!
 
Hast du vielleicht auch noch den Auruf der Funktion und wo soll das auf die SPS geschrieben werden?

Ein Bytedreher ist es auf jeden Fall schonmal nicht.
Hast du schonmal mit dem Debugger geschaut, ob DateDiff 3652 oder 16323 herausgibt?
Den Bereich, in der das Datum geschrieben wird vor dem Funktionsaufruf (Haltepunkt setzen) mal mit Nullen überschrieben und dann geschaut, ob das dann nach dem Funktionsaufruf drinnen steht oder vielleicht wo anders?
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Hallo,
warum schreibst du das Datum nicht direkt in die SPS?? Also Datentyp Date_And_Time ??
Falls es in der SPS ein Date ist könnte es möglicherweise doch ein Bytedreher sein?!

Gruß Ronnie
 
Zuletzt bearbeitet:
01.01.2000 = 0x0E44 = 3652
13.09.2034 = 0x3FC6 = 16326
08.06.2159 = 0xF1BC = -3652

Wird IMHO kein Byte-Dreher sein und falschherum gerechnet wahrscheinlich auch nicht! :rolleyes:
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Also meine Funktion

_date = DateDiff(DateInterval.Day, #1/1/1990#, CDate(Format(CDate(binReader.ReadString), "dd/MM/yyyy")))

liefert mir 3652 für datum 1.1.2000

und aufgerufen wird das ganze hiermit:

com.SetVal(cas.DBOffset + index, "Date", 62, CStr(_date))

cas.DBoffset= 2102
index = 0
Date ist bytes to write=2
62 ist die adresse
_date der wert = 3652
 
Zuviel Werbung?
-> Hier kostenlos registrieren
Also so aus dem Bauch heraus würde ich mal sagen, dass du das auf die richtige Adresse schreibst.

Und jetzt nimmst du am besten mal deinen Debugger zur Hand:

Code:
Public Function SetVal(ByVal nDBnumber As Integer, ByVal Datetype As String, _
                           ByVal nStart As Integer, ByVal nVal2Write As String) As Boolean

        Dim res As Integer
        Dim buf(1000) As Byte
        Dim nArea As Integer
        Dim temp As Integer
        Dim sValue As String
        Dim test() As Integer
        Dim nBytes2Write As Integer
        Dim datum As UShort


        If (nDBnumber >= 0) Then     ' Wenn der Wert aus der combobox >= 0 ist suche nach dem DB!

            nArea = libnodave.daveDB
        Else
            Return True 'Ansonsten gebe den Wert "nUll" Wieder!
        End If


        'Try
        If Not _CONNECTEION_STATUS_OK Then
            MsgBox("keine Verbindung")
            Exit Function
        End If

        Select Case Datetype
            Case "Byte"
                nBytes2Write = 1
                temp = CByte(nVal2Write) 'CShort(nVal2Write)
                buf = GetBytes(temp)
            Case "Integer"
                nBytes2Write = 2
                temp = libnodave.daveSwapIed_16(CInt(nVal2Write)) 'CShort(nVal2Write)
                buf = GetBytes(temp)
            Case "Float"
                nBytes2Write = 4
                temp = libnodave.daveToPLCfloat(CSng(nVal2Write))
                buf = GetBytes(temp)
            Case "String"
                nBytes2Write = 8
                buf = stringToByteArray(nVal2Write)
            Case "Longint"
                nBytes2Write = 8
                temp = CInt(nVal2Write)
                buf = GetBytes(temp)
            Case "Date"
                nBytes2Write = 2
                datum = CUShort(nVal2Write) //in dieser Zeile den Haltepunkt setzen schauen, was in nVal2Write steht, Einzelschritt vor
                buf = GetBytes(datum) // dann stehst du hier und schaust mal, was in Datum drinsteht, Einzelschritt vor und dann schaust du was in deinem buf drinsteht!

            Case "Bit" 

                Return Nothing
            Case Else
                Return Nothing
        End Select

        res = dc.writeBytes(nArea, nDBnumber, nStart, nBytes2Write, buf)
        If res = 0 Then
            Return True
        Else
            Return False
        End If

    End Function

Und wenn du das so machst siehst du was für Werte wo stehen und dann müsstest du auch sehen was da schiefläuft!
 
Du kannst es auch mal damit versuchen:

Code:
[FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]Public [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Shared [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Function[/COLOR][/COLOR] DateTimeToS7DateTime([COLOR=#0000ff][COLOR=#0000ff]ByVal[/COLOR][/COLOR] DatumUhrzeit [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Date[/COLOR][/COLOR]) [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR]()[/FONT]
[COLOR=#008000][FONT=Arial][COLOR=#008000]' Kleinste Zeitauflsung 1 Sekunde[/COLOR][/FONT]
[/COLOR][COLOR=#008000][FONT=Arial][COLOR=#008000]' kleinere Auflsung bisher noch nicht programmiert[/COLOR][/FONT]
[/COLOR][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]Try[/COLOR][/FONT]
[/COLOR][FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] Bytefeld(7) [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][/FONT][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]Byte[/COLOR][/FONT]
[/COLOR][FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] AndMask [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Integer[/COLOR][/COLOR] = &HFF[/FONT]
[FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]With[/COLOR][/COLOR] DatumUhrzeit[/FONT]
[FONT=Arial]Bytefeld(0) = [COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR](IntegerToS7BCD(.Year) [COLOR=#0000ff][COLOR=#0000ff]And[/COLOR][/COLOR] AndMask, [COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR])[/FONT]
[FONT=Arial]Bytefeld(1) = [COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR](IntegerToS7BCD(.Month), [COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR])[/FONT]
[FONT=Arial]Bytefeld(2) = [COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR](IntegerToS7BCD(.Day), [COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR])[/FONT]
[FONT=Arial]Bytefeld(3) = [COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR](IntegerToS7BCD(.Hour), [COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR])[/FONT]
[FONT=Arial]Bytefeld(4) = [COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR](IntegerToS7BCD(.Minute), [COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR])[/FONT]
[FONT=Arial]Bytefeld(5) = [COLOR=#0000ff][COLOR=#0000ff]CType[/COLOR][/COLOR](IntegerToS7BCD(.Second), [COLOR=#0000ff][COLOR=#0000ff]Byte[/COLOR][/COLOR])[/FONT]
[COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]End [/COLOR][/FONT][/COLOR][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]With[/COLOR][/FONT]
[/COLOR][FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]Return[/COLOR][/COLOR] Bytefeld[/FONT]
[FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]Catch[/COLOR][/COLOR] ex [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] Exception[/FONT]
[COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]Return[/COLOR][/FONT][/COLOR][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]Nothing[/COLOR][/FONT]
[/COLOR][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]End[/COLOR][/FONT][/COLOR][COLOR=#0000ff][FONT=Arial][COLOR=#0000ff]Try[/COLOR][/FONT]
[/COLOR][FONT=Arial][COLOR=#0000ff][COLOR=#0000ff]End[/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Function[/COLOR][/COLOR][/FONT]
[FONT=Arial] [/FONT]
[SIZE=3][COLOR=#0000ff][SIZE=3][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]Public [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Shared [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Function[/COLOR][/COLOR] IntegerToS7BCD([COLOR=#0000ff][COLOR=#0000ff]ByVal[/COLOR][/COLOR] Wert [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Integer[/COLOR][/COLOR]) [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][/SIZE][/FONT][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/FONT]
[/COLOR][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE][/FONT]
[/COLOR][FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] s [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]String[/COLOR][/COLOR] = Wert.ToString[/SIZE][/FONT]
[FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] FeldIndex [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Integer[/COLOR][/COLOR] = s.Length - 1[/SIZE][/FONT]
[FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] c(FeldIndex) [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][/SIZE][/FONT][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Char[/COLOR][/SIZE][/FONT]
[/COLOR][FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]Dim[/COLOR][/COLOR] d(FeldIndex) [COLOR=#0000ff][COLOR=#0000ff]As [/COLOR][/COLOR][/SIZE][/FONT][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/FONT]
[/COLOR][FONT=Arial][SIZE=2]s.CopyTo(0, c, 0, s.Length)[/SIZE][/FONT]
[FONT=Arial][SIZE=2]Array.Reverse(c)[/SIZE][/FONT]
[FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]For[/COLOR][/COLOR] i [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR][COLOR=#0000ff][COLOR=#0000ff]Integer[/COLOR][/COLOR] = 0 [COLOR=#0000ff][COLOR=#0000ff]To[/COLOR][/COLOR] FeldIndex[/SIZE][/FONT]
[FONT=Arial][SIZE=2]d(i) = Val(c(i))[/SIZE][/FONT]
[FONT=Arial][SIZE=2]IntegerToS7BCD = IntegerToS7BCD [COLOR=#0000ff][COLOR=#0000ff]Or[/COLOR][/COLOR] (d(i) << i * 4)[/SIZE][/FONT]
[COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][/FONT]
[/COLOR][FONT=Arial][SIZE=2][COLOR=#0000ff][COLOR=#0000ff]Catch[/COLOR][/COLOR] ex [COLOR=#0000ff][COLOR=#0000ff]As[/COLOR][/COLOR] Exception[/SIZE][/FONT]
[COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][/FONT]
[/COLOR][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Try[/COLOR][/SIZE][/FONT]
[/COLOR][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][/FONT][/COLOR][COLOR=#0000ff][FONT=Arial][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][/FONT]
[SIZE=3][/SIZE][/COLOR][/COLOR][/SIZE][/COLOR][/SIZE]

Hatte ich mir mal für VB.NET geschrieben.

VG, Manni
 
Zurück
Oben