ParkSquare.LocoNetSharp 1.0.2

dotnet add package ParkSquare.LocoNetSharp --version 1.0.2
NuGet\Install-Package ParkSquare.LocoNetSharp -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ParkSquare.LocoNetSharp" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ParkSquare.LocoNetSharp --version 1.0.2
#r "nuget: ParkSquare.LocoNetSharp, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ParkSquare.LocoNetSharp as a Cake Addin
#addin nuget:?package=ParkSquare.LocoNetSharp&version=1.0.2

// Install ParkSquare.LocoNetSharp as a Cake Tool
#tool nuget:?package=ParkSquare.LocoNetSharp&version=1.0.2

C# LocoNetSharp Library

Build Status

Background

LocoNetSharp is a C# library for communicating with, controlling and programming DCC model railway (railroad) trains, points (turnouts), signals and other accessories via the LocoNet® protocol. Compatible with command stations via USB, TCP/IP or UDP.

Targets .Net Standard 2.0 for compatability with .Net Framework, .Net Core and .Net 6.

Based on a .Net Core port of Loconet® Toolbox by Chris Sharp, Modelspoorgroep Venlo and Ewout Prangsma.

Getting Started

Create a LocoBuffer object, depending on which method of communicating with your control station you prefer (USB via COM port, TCP/IP or UDP over your network).

TCP/IP over network

using var lb = new TcpLocoBuffer("10.10.0.219", 5550);

UDP over network

using var lb = new UdpLocoBuffer("10.10.0.219", 5550);

USB / COM port

using var lb = new SerialPortLocoBuffer("COM5", BaudRate.Rate57K);

Note that you need to select 'LocoNet(r) over TCP/IP Binary' when using command stations such as the Digikeijs DR5000 and others.

Once you have instantiated your LocoBuffer class of choice, create a LocoNet object which represents the entire LocoNet system, and maintains state:

using var loconet = new LocoNet(lb);

Send Commands

Some well-known commands are native to the library, such as Global Power On and Global Power Off.

var on = new GlobalPowerOn();
on.Execute(lb);

Console.ReadKey();

var off = new GlobalPowerOff();
off.Execute(lb);

Console.WriteLine("Goodbye");

Don't forget to wrap your LocoBuffer and LocoNet objects in 'using' statements so they are properly tidied up. Also, call the Close() method on the LocoBuffer object before exiting your program:

lb.Close();

Events

You can react to events, for example you can preview a message sent to the system by another device, detect idle state, and others:

lb.SendMessage += OnSendMessage;
lb.PreviewMessage += OnPreviewMessage;

var lnState = loconet.State;
lnState.StateChanged += LnStateStateChanged;
lnState.Idle += LnStateIdle;

private static bool OnPreviewMessage(byte[] message, Message decoded)
{
	if (decoded != null)
	{
		Console.WriteLine($"PREVIEW: {decoded}");
		Console.WriteLine(Message.ToString(message));
	}
	else
	{
		Console.WriteLine($"PREVIEW: {Message.ToString(message)}");
	}

	return true;
}

private static void LnStateIdle(object? sender, EventArgs e)
{
	Console.WriteLine("IDLE");
}

private static void LnStateStateChanged(object? sender, StateMessage e)
{
	Console.WriteLine($"STATE CHANGED {e.State.Address}");
}

private static bool OnSendMessage(byte[] message, Message decoded)
{
	Console.WriteLine($"SEND {decoded}");
	return true;
}

Some well-known messages can be decoded into their constituent parts by the library, this will be known in these events when 'decoded' is not null.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 625 8/8/2023
1.0.1 633 8/5/2023
1.0.0 585 8/4/2023