Click here to Skip to main content
15,888,351 members
Articles / Hosted Services / Serverless
Tip/Trick

Communication Library Files - UDP Broadcast

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
5 Apr 2015GPL34 min read 14.6K   640   8   4
Communication library files - UDP broadcast

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 partSICKlib 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...)

Screenshot

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)

VB.NET
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

VB.NET
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:

VB.NET
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:

VB.NET
'Adding a multithreaded handler for the new data received event
AddHandler UDPBroadCast.NewDataAvailable, AddressOf NewDataRecieved

a simple sub as event handler:

VB.NET
Public Sub NewDataRecieved()
    'Read the string that came available
    Recievedstring = UDPBroadCast.Readavailable_string()

   'Send it to a multithreaded "Add to listbox" handler
    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

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Help desk / Support
Netherlands Netherlands
Working at a help desk for more then 10 years now, last few years being more into programming examples for industrial sensors and camera's. Expertise Vision sensors and camera's, last two years trying to explore the possibilities of laser scanning devices in industrial applications.

Comments and Discussions

 
QuestionFile missing Pin
Talley Ouro16-Apr-15 9:55
Talley Ouro16-Apr-15 9:55 
AnswerRe: File missing Pin
Chris Jorna17-Apr-15 22:36
Chris Jorna17-Apr-15 22:36 
GeneralRe: File missing Pin
Talley Ouro2-Jun-15 10:56
Talley Ouro2-Jun-15 10:56 
GeneralMy vote of 5 Pin
Marco Giovanni7-Apr-15 9:50
Marco Giovanni7-Apr-15 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.