Use of UdpSocket example

Please share your functions/code-snips

Use of UdpSocket example

Postby Henning » Mon Mar 30, 2009 2:24 pm

Hi,

Thought I should share an example of how to use the UdpSocket control (thx Bernd).

Code: Select all
Begin Form *
    DockTitleVisible = True
    DrawOnPaint = True
    GlobalX = 0
    GlobalY = 0
    Height = 354
    LoadedHeight = 358
    LoadedWidth = 699
    LoadedX = 1
    LoadedY = 55
    MouseTracking = True
    Resizable = True
    SQLDelete = True
    SQLInsert = True
    SQLUpdate = True
    Visible = True
    Width = 699
    X = 1
    Y = 55


  Begin UdpSocket UdpSocket1
    DrawOnPaint = True
    Enabled = True
    GlobalX = 0
    GlobalY = 0
    Height = 20
    LoadedHeight = 20
    LoadedWidth = 200
    LoadedX = 25
    LoadedY = 257
    ParentControl = ""
    TabIndex = 0
    Width = 200
    X = 489
    Y = 298
  End

  Begin CommandButton CommandButton1
    Caption = "Send"
    DrawOnPaint = True
    Enabled = True
    GlobalX = 0
    GlobalY = 0
    Height = 23
    LoadedHeight = 23
    LoadedWidth = 110
    LoadedX = 34
    LoadedY = 117
    ParentControl = ""
    TabIndex = 0
    Visible = True
    Width = 110
    X = 34
    Y = 117
  End

  Begin TextBox TextBox1
    DrawOnPaint = True
    Enabled = True
    Flat = True
    GlobalX = 0
    GlobalY = 0
    Height = 30
    IntegerMaximum = 0
    LoadedHeight = 30
    LoadedWidth = 320
    LoadedX = 35
    LoadedY = 75
    ParentControl = ""
    StringValue = "Hello World"
    TabIndex = 0
    TableViewVisible = True
    Visible = True
    Width = 320
    X = 35
    Y = 75
  End

  Begin TextBox TextBox2
    DrawOnPaint = True
    Enabled = True
    Flat = True
    GlobalX = 0
    GlobalY = 0
    Height = 30
    IntegerMaximum = 0
    LoadedHeight = 30
    LoadedWidth = 320
    LoadedX = 359
    LoadedY = 75
    ParentControl = ""
    StringValue = ""
    TabIndex = 0
    TableViewVisible = True
    Visible = True
    Width = 320
    X = 359
    Y = 75
  End

  Begin Label Label1
    Caption = "This exmaple sends data using a UDP socket on the local host."
    DrawOnPaint = True
    Enabled = True
    FontBold = True
    GlobalX = 0
    GlobalY = 0
    Height = 34
    LoadedHeight = 34
    LoadedWidth = 361
    LoadedX = 37
    LoadedY = 30
    ParentControl = ""
    TabIndex = 0
    TableViewVisible = True
    Visible = True
    Width = 361
    WordWrap = True
    X = 37
    Y = 30
  End

  Begin CheckBox CheckBox1
    Caption = "CheckBox"
    DrawOnPaint = True
    Enabled = True
    GlobalX = 0
    GlobalY = 0
    Height = 30
    LoadedHeight = 30
    LoadedWidth = 120
    LoadedX = 38
    LoadedY = 285
    ParentControl = ""
    TabIndex = 0
    TableViewVisible = True
    Visible = False
    Width = 120
    X = 38
    Y = 285
  End

  Begin UdpSocket UdpSocket2
    DrawOnPaint = True
    Enabled = True
    GlobalX = 0
    GlobalY = 0
    Height = 20
    ParentControl = ""
    TabIndex = 0
    Width = 200
    X = 490
    Y = 321
  End

End Form

Dim Pkt[18] As Byte

Private Sub Form_OnOpen()
  UdpSocket1.Host = "192.168.0.3"
  UdpSocket1.Port = 8101
  UdpSocket1.Open()
  UdpSocket2.Bind("192.168.0.1", 8100)
 
 ' CheckBox1.Value = UdpSocket1.Open()
 'Packet to turn off all outputs in Unit 1, System 2
   Pkt(0) = &H01  'Packet Type 1 = Order
   Pkt(1) = &H00  'Packet ID high
   Pkt(2) = &H01  'Packet ID low
   Pkt(3) = &H02  'System# (1 - 254)
   Pkt(4) = &H01  'Unit# (1 - 254)
   Pkt(5) = &H02  'Unit Port#
   Pkt(6) = &H00  'DataByte 7
   Pkt(7) = &H00  'DataByte 6
   Pkt(8) = &H00  'DataByte 5
   Pkt(9) = &H00  'DataByte 4
   Pkt(10) = &H00 'DataByte 3
   Pkt(11) = &H00 'DataByte 2
   Pkt(12) = &H00 'DataByte 1
   Pkt(13) = &H55 'DataByte 0
   Pkt(14) = &H1F 'ReplyToUDPPort high (8100)
   Pkt(15) = &HA4 'ReplyToUDPPort low
   Pkt(16) = &H00 'CRC high
   Pkt(17) = &H00 'CRC low
 
End Sub


Private Sub UdpSocket2_OnEvent()
  Dim rx As String
  Dim i As Integer
 
  rx = UdpSocket2.ReadData()
  TextBox2.Value = ""
  For i = 1 To Len(rx)
    TextBox2.Value = TextBox2.Value & Right("00" & Hex(Asc(Mid(rx,i,1))), 2) & ","
  Next
End Sub



Private Sub CommandButton1_OnEvent()
  Dim tx As String
  Dim i As Integer
  Dim x As Integer
  Dim crc As Short

  For i = 0 To 15      'Calculate CRC
    crc = crc + Pkt[i]
  Next

  Pkt[16] = (crc \ 256) And &HFF
  Pkt[17] = crc And &HFF

  For i = 0 To 17
    tx = tx & Chr(Pkt[i])
  Next
 
  TextBox1.Value = ""
  For i = 1 To Len(tx)
    TextBox1.Value = TextBox1.Value & Right("00" & Hex(Asc(Mid(tx,i,1))), 2) & ","
  Next
 
  UdpSocket1.WriteData(tx)
End Sub



/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: Use of UdpSocket example

Postby berndnoetscher » Tue Mar 31, 2009 8:53 am

Thanks for sharing your code. :D
berndnoetscher
Site Admin
 
Posts: 1059
Joined: Tue Sep 25, 2007 9:37 am


Return to User-Functions

cron