Introduction
This example shows how to use the UDP part of the communication library that a colleague and myself made for demonstration purposes on SICK A.G. Lidar devices (F.i. LMS5xx).
The total library itself enables the user to generate a 3D on screen Lidar image within 15 lines of code (without error handling, etc.) The total library will be posted on a general download page for easy maintenance.
This tip will handle the UDP Broadcast part of the library showing a simple serverless ChatClient
based on broadcast messages on a local network (LAN).
Why am I posting this? My goal is to create an easy, open platform for Lidar devices as they are more commonly used. If you see anything that should be different, feel free to change the source code and post the result as a comment.
Background
As mentioned, this tip only describes part of the total communication library (green block) which in its turn is part of a bigger framework (all blocks together) of libraries I internally use to demonstrate Lidar devices. The library is used for approximately 2 years now and works perfectly for our demos and field tests.
All the smaller individual parts that can be used to convert SICK Lidar data into 3D pointclouds, LAS, CSV, bitmap or 3D pointclouds with the least of effort gives you the power to generate awesome pointclouddata, 3D representation or bitmaps out of these devices.
This example shows you how to create a UDP broadcast message. For the LIDAR devices, these kind of messages are used for identifying new devices on the local network because UDP doesn't require an active connection. UDP is ideal for identifying devices that aren't on the same subnet. They can "chat" and negotiate the correct IP-settings without ever being connected "normally" (TCP wise).
In a previous article, I described the TCP part of this same library, so if you got that source code just use the sample as described below.
Later articles will describe the use of the Serial and USB part of the library. Also, I will post an article on the NetTools
class which will enable you to match IPs to subnetmask, check out what's on your network and retrieve MAC-adresses of connected devices.
Screenshot of the sample: (you need two PCs to chat of course...)
Using the Code
Basically, the code should "look and feel" the same as the TCP example (find it here) so the same rules apply. The code is made to work as broadcast and not as a unicasting (single destination) code. Although you can use it as the example describes, it was meant to be used as identifier for the Lidar devices.
Instantiate the UDP broadcaster and open a connection on same send/receive ports (30718 in example)
UDPBroadCast = New SICK_Communication.Ethernet.UDP()
UDPBroadCast.UDP_open(30718, 30718)
The same send and receive port is used to make this example. Using the same port causes echo on the receive telegrams that will require filtering (see example).
Sending a telegram
UDPBroadCast.Send_BroadcastMessage(NickName & " joined the chat")
Sending bytes is also supported as overloaded function, in this case we send a variable and a fixed string
.
Checking for new received data
Single threaded just can check with f.i
a timer the availability of data like this:
If UDPBroadCast.available > 0 Then
UDPBroadCast.Readavailable_string()
End If
It's also possible to subscribe to the NewData
event for multithreaded support like the:
AddHandler UDPBroadCast.NewDataAvailable, AddressOf NewDataRecieved
a simple sub
as event handler:
Public Sub NewDataRecieved()
Recievedstring = UDPBroadCast.Readavailable_string()
Listbox_RecieveLB_ItemAdd(Recievedstring)
End Sub
That's it, sending and receiving explained.
Points of Interest
In the communication class, there is a reference to the structure library SICK_Work
. This library is just a bunch of classes with little functionality but they form the pillar for a complete application (as the illustration shows). Because of this library, it's possible to use each library as an individual part as the interconnection between those libraries is not implemented within that same library itself.
The reference files are the pre-build library files which are needed for the samples to work.
The communication class is also usable as a simple terminal program (like hercules setup from HW group, which I think is a very great tool) where you can set your own limitations. And with a little effort, you can make a keyboard-wedge program out of it. All in all, I think it's nice to make this publicly available.
Remember to share!!
History
The program itself is licensed as GPL software. So far, this is the latest adaptation to the library by myself and my colleague referred to as the V4-library. Improvements are always welcome, hope you enjoy using it.
Other Tips
TCP client/server example: http://www.codeproject.com/Tips/892813/Communication-Library-Files