Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1952to1956
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

TCP/IP Kommunikation

TCP/IP Kommunikation
24.11.2023 16:54:06
MWA
Hallo zusammen,

ich möchte gerne Werte von Messgebern einlesen. Ich kann nun einen Befehl senden und laut Wireshark kommen auch Werte zurück. Jedoch sind die Bytes, welche ich einlese leer bzw. 0. Kann mir hier jemand helfen?

Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

#If VBA7 Then
Declare PtrSafe Function WSAStartup Lib "ws2_32.dll" (ByVal wVersionRequested As Integer, ByRef lpWSAData As wsaData) As Long
Declare PtrSafe Function socket Lib "ws2_32.dll" (ByVal af As Long, ByVal socktype As Long, ByVal protocol As Long) As Long
Declare PtrSafe Function connect Lib "ws2_32.dll" (ByVal s As Long, ByRef name As SOCKADDR, ByVal namelen As Long) As Long
Declare PtrSafe Function send Lib "ws2_32.dll" (ByVal s As Long, ByVal buf As String, ByVal len1 As Long, ByVal flags As Long) As Long
Declare PtrSafe Function closesocket Lib "ws2_32.dll" (ByVal s As Long) As Long
Declare PtrSafe Function WSACleanup Lib "ws2_32.dll" () As Long
#Else
Declare Function WSAStartup Lib "ws2_32.dll" (ByVal wVersionRequested As Integer, ByRef lpWSAData As wsaData) As Long
Declare Function socket Lib "ws2_32.dll" (ByVal af As Long, ByVal socktype As Long, ByVal protocol As Long) As Long
Declare Function connect Lib "ws2_32.dll" (ByVal s As Long, ByRef name As SOCKADDR, ByVal namelen As Long) As Long
Declare Function send Lib "ws2_32.dll" (ByVal s As Long, ByVal buf As String, ByVal len1 As Long, ByVal flags As Long) As Long
Declare Function closesocket Lib "ws2_32.dll" (ByVal s As Long) As Long
Declare Function WSACleanup Lib "ws2_32.dll" () As Long
#End If
#If VBA7 Then
Private Declare PtrSafe Function inet_addr_impl Lib "ws2_32.dll" (ByVal ipAddress As String) As Long
Public Declare Function inet_addr Lib "ws2_32" (ByVal s As String) As Long
#Else
Private Declare Function inet_addr_impl Lib "ws2_32.dll" (ByVal ipAddress As String) As Long
#End If
#If VBA7 Then
Private Declare PtrSafe Function recv Lib "ws2_32.dll" (ByVal s As LongPtr, ByVal buf As String, ByVal len2 As Long, ByVal flags As Long) As Long
#Else
Private Declare Function recv Lib "ws2_32.dll" (ByVal s As Long, ByVal buf As String, ByVal len2 As Long, ByVal flags As Long) As Long
#End If

Type wsaData
wVersion As Integer
wHighVersion As Integer
szDescription(0 To 255) As Byte
szSystemStatus(0 To 128) As Byte
iMaxSockets As Integer
iMaxUdpDg As Integer
lpVendorInfo As Long
End Type

Type SOCKADDR
sin_family As Integer
sin_port As Integer
sin_addr As Long
sin_zero(0 To 7) As Byte
End Type

Sub TCPIPCommunication()
Dim wsaData As wsaData
Dim clientSocket As Long
Dim serverAddress As SOCKADDR
Dim result As Long
Dim commandString As String
commandString = ""
Dim commandBytes() As Byte
Dim BytesSent As Long
commandBytes = "M0" 'StrConv(commandString, vbUnicode)

'WinSock initialisieren
result = WSAStartup(2.2, wsaData)
If result > 0 Then
MsgBox "Error initializing Winsock!"
Exit Sub
End If

'Socket erstellen
clientSocket = socket(2, 1, 0) ' AF_INET, SOCK_STREAM, IPPROTO_TCP
If clientSocket = -1 Then
MsgBox "Error creating socket!"
WSACleanup
Exit Sub
Else
Debug.Print "socket created"
End If

serverAddress.sin_family = 2 ' AF_INET
serverAddress.sin_port = htons(81) ' Port number
serverAddress.sin_addr = inet_addr("4.2.0.1")

' Connect
result = connect(clientSocket, serverAddress, Len(serverAddress))
If result = 0 Then
Debug.Print "connected"
End If
If result = -1 Then
MsgBox "Error connecting to server!"
closesocket clientSocket
WSACleanup
Exit Sub
End If


BytesSent = send(clientSocket, commandBytes(0), UBound(commandBytes) + 1, 0)
If Not BytesSent = UBound(commandBytes) + 1 Then
MsgBox ("Fehler beim Senden")
Else
Debug.Print "bytes sent : " & BytesSent
End If


Sleep (20)

ReceiveData (clientSocket)

closesocket clientSocket
WSACleanup
End Sub
Sub ReceiveData(socketHandle As Long)
Dim bufferSize As Long
Dim buffer() As Byte
Dim bytesRead As Long
Dim receivedData As Long
Dim idx As Long
On Error GoTo ende
bufferSize = 11
ReDim buffer(bufferSize)
Do
Debug.Print "Wert socket : " & socketHandle
bytesRead = recv(socketHandle, buffer(0), UBound(buffer), 0)
Debug.Print "Anzahl gelesene Bytes : " & bytesRead
If bytesRead = -1 Then
Debug.Print "Fehler beim Empfangen : " & Err.LastDllError
ElseIf bytesRead > 0 Then
'receivedData = receivedData + BytesToHex(buffer)
'Debug.Print receivedData
receivedData = receivedData + BytesToString(buffer)
Debug.Print "tmpstr = " & receivedData
End If
Sleep (50)
Loop Until bytesRead = 11

ende:
closesocket socketHandle

End Sub




Function htons(value As Integer) As Integer
If htons = 0 Then
htons = (value And &HFF) * 256 + ((value And &HFF00) \ 256)
End If
End Function



Function BytesToString(bytes() As Byte) As String
Dim i As Long
Dim result As String
Debug.Print "BytesToString"
Dim k As Long
For k = LBound(bytes) To UBound(bytes)
Debug.Print "Byte" & k & ": " & bytes(k)
Sleep (30)
Next k
result =StrConv(bytes, vbFromUnicode)
For i = 1 To Len(result)
If AscW(Mid(result, i, 1)) > 127 Then
Mid(result, i, 1) = "?"


End If
Next i
BytesToString = result
End Function






Laut Wireshark kommen 11 Bytes zurueck.


4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: TCP/IP Kommunikation
26.11.2023 03:58:42
MWA
Wirklich niemand? Ich bin neu hier, reicht der Code den ich gepostet habe aus?
ja reicht völlig.....
26.11.2023 12:38:29
ralf_b
.. is vielleicht sogar etwas zu viel des Guten für mal eben Drübergucken und den Logik- oder Schreibfehler finden.
Ich glaube das sich dieses Konstrukt kaum Einer genauer ansehen wird. Zu speziell, deshalb haben Wenige damit Erfahrung. Und man benötigt so ein Gerät zum Testen und Fehler finden.
Für kontiniuierliches Einlesen von Messwerten hatte ich vor einer Weile mal den Microsoft Datastreamer for Excel ausprobiert.
Ich glaube der geht aber nur mittels COM Port Anbindung.
AW: ja reicht völlig.....
26.11.2023 16:08:34
MWA
Danke,

ich glaube nicht mal, dass es ein Schreibfehler ist. Möglicherweise geht das unter 64Bit auch nicht oder strconv ist hier der falsche Ansatz.
Leider gibt es zu dem Thema so gut wie nichts. In C# ist das Ganze in 15min erledigt. Aber meine Ehre verlangt, dass es auch in VBA funktioniert :)
Anzeige
AW: ja reicht völlig.....
28.11.2023 02:17:51
MWA
Hallo,

ich habe es jetzt geschafft. Hatte noch einen tollen Ex-Arbeitskollegen, der noch ein Beispiel hatte.
Mein Fehler war, dass ich die Daten in ein Bytearray einlesen wollte und anschließend zu einem string konvertieren.
Wenn ich einfach den Befehl und die Antwort als string behandle funktioniert es.
Zudem hatte ich aus Versehen received Data als Long statt string deklariert.
Wie auch immer, trotzdem vielen Dank für den Fall, dass es sich jemand angesehen hat.

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige