1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| Imports System.Net
Imports System.Net.Sockets
Imports System.text
Public Sub ReadDM(ByVal Word As Integer, ByVal NbAcq As Integer)
Dim MySocket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim AdresseIP As IPAddress = IPAddress.Parse("192.168.0.200")
Dim RemoteIpEndPoint As New IPEndPoint(AdresseIP, 9600)
Dim x As Integer = 0
Dim available As Integer
Dim ErrCmpt As Integer
'Connexion à l'automate
MySocket.Connect(RemoteIpEndPoint)
Dim msg As Byte() = Encoding.UTF8.GetBytes("@00RD0000000157*" & vbCr)
Reprise:
MySocket.Send(msg, msg.Length, SocketFlags.None)
Try
MySocket.Poll(1000000, SelectMode.SelectRead)
Try
available = MySocket.Available()
Catch ex As Exception
MsgBox("Problème lors de la réception des données")
Exit Sub
End Try
If available = 0 Then
ErrCmpt += 1
If ErrCmpt = 5 Then GoTo CommunicationError
GoTo Reprise
End If
Dim NewBuffer() As Byte = New Byte(available) {}
Dim tempRemoteEP As EndPoint = RemoteIpEndPoint
MySocket.ReceiveFrom(NewBuffer, available, 0, tempRemoteEP)
MsgBox(NewBuffer)
Catch ex As Exception
ErrCmpt += 1
If ErrCmpt = 5 Then GoTo CommunicationError
GoTo Reprise
End Try
MySocket.Shutdown(SocketShutdown.Both)
MySocket.Close()
Exit Sub
CommunicationError:
MySocket.Shutdown(SocketShutdown.Both)
MySocket.Close()
End Sub |
Partager