An example use case in C#.NET
This tutorial will guide you through the basics of using LCM .NET port. As the .NET port is basically a transcription of the original Java library, it tries to be functionally equivalent while maintaining C#.NET naming conventions and other platform specifics. All sample code is written in C# (as well as the port itself), but the principles are applicable to any of the languages supported by the .NET Framework.
The tutorial doesn't cover the very basics of the LCM (message transmision principles, message definition format etc.) - please see the rest of the documentation before further reading.
To demonstrate basic functionality, this tutorial will use the same message format and application logic as the Java Tutorial to accent similarities and differences between Java and .NET ports. Let's have the following type specification, saved to a file named
In order to obtain C#.NET-specific handler class, we need to call
lcm-gen with the
lcm-gen utility accepts the following .NET-specific options:
|–csharp-path||C#.NET file destination directory|
|–csharp-mkdir||1||Make C#.NET source directories automatically|
|–csharp-strip-dirs||0||Do not generate folders for default and root namespace - unlike Java sources, the .NET source files' directory structure does not have to be analogic to their namespace structure. It is often advantageous to omit the top-most directories common to all generated files to simplify the directory layout.|
|–csharp-decl||: LCM.LCM.LCMEncodable||String added to class declarations - similar to Java option |
|–csharp-root-nsp||Root C#.NET namespace (wrapper) added before LCM package name - this comes handy when you want to place generated .NET bindings into a specific part of your .NET namespace hierarchy and do not want to affect other languages by embedding the wrapper namespace directly into the source |
|–csharp-default-nsp||LCMTypes||Default NET namespace if the LCM type has no package defined|
As with all tutorials, we will publish and subscribe to the "EXAMPLE" channel.
There are at least two ways how to use the .NET port of LCM:
Main classes of the library are put in the LCM.LCM namespace (while helper code is in LCM.Util). This results in quite funny fully qualified name of the master class - LCM.LCM.LCM (its constructor is even funnier - LCM.LCM.LCM.LCM() :-) ). It's logical to use the
'using LCM.LCM' statement to shorten calls to the library, but the naming scheme (chosen to agree with the Java variant) makes it a little difficult - you cannot use bare 'LCM' as class name - the compiler considers it to be the namespace. Instead, you need to write LCM.LCM to denote the main class.
Generated message handlers are placed in the
LCMTypes namespace by default (you can change this by specifying lcm-gen option
LCM itself has a mechanism to maintain single instance of the main class - static property LCM.Singleton:
You can also instantiate the class and take care of the object's singularity by yourself:
In situations where the default connection URL (fetched by LCM from the environment variable
LCM_DEFAULT_URL or defined by the constant
"udpm://184.108.40.206:7667" when the former is empty) is not suitable, the constructor can accept variable number of parameters specifying individual connection strings.
For detailed information on the LCM .NET API please see the .NET API reference.
In order to use LCM types, you can either build an assembly containing generated classes (needed when using LCM from other .NET language then C#), or include the classes directly to application project. Utilization of the generated classes is then fairly straightforward:
The data are simply assigned to appropriate fields inside the message container. Passing the message object to the
Publish method of the LCM object places it to specified channel of the communication bus (channel "EXAMPLE" here).
In order to receive messages, you have two options:
This tutorial exploits the former option - the class
SimpleSubscriber is defined as internal inside the demo application class:
The class instance is then passed to LCM method
SubscribeAll that passes all received messages to our subscriber class. When selective subscription is needed (i.e. in almost all real-world cases as we usually don't to listen to all channels), method
Subscribe that takes the channel name pattern as an argument is to be used.
Distribution of the LCM library includes a directory of examples. One of them is a couple of programs implementing all described features. Please go to
examples/csharp/ to find Visual Studio solution ready to be built.
The complete example transmitter application:
The complete example receiver application:
The tutorial has provided a basic working demonstration of the LCM library .NET port. For further information, please see the LCM documentation.