Class Z21CommandStation
public class Z21CommandStation : IZ21CommandStation, ICommandStation, ILocoControl, IAccessoryControl, ITrackPowerControl, ISystemInfoProvider, IProgrammingControl, IFeedbackControl, IFastClockControl, IDisposable
- Inheritance
-
Z21CommandStation
- Implements
- Inherited Members
Constructors
Z21CommandStation(ITransport, Z21ResponseHandler, IZ21CommandFactory, Z21Options, ILocoInfoResponseHandler, ITurnoutInfoResponseHandler, IExtAccessoryInfoResponseHandler, ISystemStateDataChangedResponseHandler, IFirmwareVersionResponseHandler, IStatusChangedResponseHandler, ITrackPowerOnResponseHandler, ITrackPowerOffResponseHandler, ICvResultResponseHandler, ICvNackResponseHandler, ICvNackShortCircuitResponseHandler, IRmBusDataChangedResponseHandler, IFastClockDataResponseHandler, ILogger<Z21CommandStation>?)
public Z21CommandStation(ITransport transport, Z21ResponseHandler dispatcher, IZ21CommandFactory commands, Z21Options options, ILocoInfoResponseHandler locoInfoResponseHandler, ITurnoutInfoResponseHandler turnoutInfoResponseHandler, IExtAccessoryInfoResponseHandler extAccessoryInfoResponseHandler, ISystemStateDataChangedResponseHandler systemStateResponseHandler, IFirmwareVersionResponseHandler firmwareVersionResponseHandler, IStatusChangedResponseHandler statusChangedResponseHandler, ITrackPowerOnResponseHandler trackPowerOnResponseHandler, ITrackPowerOffResponseHandler trackPowerOffResponseHandler, ICvResultResponseHandler cvResultResponseHandler, ICvNackResponseHandler cvNackResponseHandler, ICvNackShortCircuitResponseHandler cvNackShortCircuitResponseHandler, IRmBusDataChangedResponseHandler rmBusDataChangedResponseHandler, IFastClockDataResponseHandler fastClockDataResponseHandler, ILogger<Z21CommandStation>? logger = null)
Parameters
transportITransportdispatcherZ21ResponseHandlercommandsIZ21CommandFactoryoptionsZ21OptionslocoInfoResponseHandlerILocoInfoResponseHandlerturnoutInfoResponseHandlerITurnoutInfoResponseHandlerextAccessoryInfoResponseHandlerIExtAccessoryInfoResponseHandlersystemStateResponseHandlerISystemStateDataChangedResponseHandlerfirmwareVersionResponseHandlerIFirmwareVersionResponseHandlerstatusChangedResponseHandlerIStatusChangedResponseHandlertrackPowerOnResponseHandlerITrackPowerOnResponseHandlertrackPowerOffResponseHandlerITrackPowerOffResponseHandlercvResultResponseHandlerICvResultResponseHandlercvNackResponseHandlerICvNackResponseHandlercvNackShortCircuitResponseHandlerICvNackShortCircuitResponseHandlerrmBusDataChangedResponseHandlerIRmBusDataChangedResponseHandlerfastClockDataResponseHandlerIFastClockDataResponseHandlerloggerILogger<Z21CommandStation>
Exceptions
- PlatformNotSupportedException
Thrown when system architecture is not little-endian.
Fields
MaxUdpPayload
IPv4 safe MTU for payload according to specification.
public const int MaxUdpPayload = 1472
Field Value
Properties
Commands
Factory for building raw Z21 commands to pass to SendCommandsAsync(params IZ21Command[]).
public IZ21CommandFactory Commands { get; }
Property Value
IsConnected
public bool IsConnected { get; }
Property Value
Methods
ConnectAsync()
public Task ConnectAsync()
Returns
DisconnectAsync()
public Task DisconnectAsync()
Returns
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
DriveAsync(ushort, DccSpeedMode, DrivingDirection, ushort)
public Task DriveAsync(ushort locoAddress, DccSpeedMode speedMode, DrivingDirection direction, ushort speed)
Parameters
locoAddressushortspeedModeDccSpeedModedirectionDrivingDirectionspeedushort
Returns
EmergencyStopAllAsync()
Stops all locomotives while leaving the track voltage on.
public Task EmergencyStopAllAsync()
Returns
EmergencyStopAsync(ushort)
public Task EmergencyStopAsync(ushort locoAddress)
Parameters
locoAddressushort
Returns
LogOnAsync()
protected virtual Task LogOnAsync()
Returns
PurgeAsync(ushort)
public Task PurgeAsync(ushort locoAddress)
Parameters
locoAddressushort
Returns
ReadCvAsync(ushort)
public Task ReadCvAsync(ushort cvAddress)
Parameters
cvAddressushort
Returns
ReadCvAsync(ushort, TimeSpan)
Reads a CV on the programming track, retrying while the decoder does not acknowledge, and returns the value. A missing acknowledgement that never clears (for example an absent or unreadable decoder) is reported as a timeout rather than a distinct error. Do not call the fire-and-forget CV methods concurrently with this one on the same station.
public Task<byte> ReadCvAsync(ushort cvAddress, TimeSpan timeout)
Parameters
Returns
Exceptions
- CvOperationTimeoutException
No result arrived within
timeout.- CvShortCircuitException
The command station reported a short circuit.
- ArgumentOutOfRangeException
timeoutis not a positive, in-range duration.
ReadPomCvAsync(ushort, ushort, TimeSpan)
Reads a CV of a locomotive decoder on the main track (POM), retrying while the decoder does not acknowledge, and returns the value. Requires RailCom; without it (or for an absent decoder) the read can only ever time out. The result is correlated by CV address only — the protocol's POM result carries no loco address — so do not run other CV operations on this station concurrently.
public Task<byte> ReadPomCvAsync(ushort locoAddress, ushort cvAddress, TimeSpan timeout)
Parameters
Returns
Exceptions
- CvOperationTimeoutException
No result arrived within
timeout.- CvShortCircuitException
The command station reported a short circuit.
- ArgumentOutOfRangeException
timeoutis not a positive, in-range duration.
ReadPomCvBitAsync(ushort, ushort, byte, TimeSpan)
Reads a single bit of a CV of a locomotive decoder on the main track (POM) by reading the CV byte back (so it requires RailCom) and returning the value of the target bit.
public Task<bool> ReadPomCvBitAsync(ushort locoAddress, ushort cvAddress, byte bitPosition, TimeSpan timeout)
Parameters
locoAddressushortcvAddressushortbitPositionbyteThe 0-based bit position within the CV (0–7).
timeoutTimeSpan
Returns
Exceptions
- CvOperationTimeoutException
No result arrived within
timeout.- CvShortCircuitException
The command station reported a short circuit.
- ArgumentOutOfRangeException
bitPositionexceeds 7, ortimeoutis not a positive, in-range duration.
RequestExtAccessoryInfoAsync(ushort)
public Task RequestExtAccessoryInfoAsync(ushort accessoryAddress)
Parameters
accessoryAddressushort
Returns
RequestFeedbackAsync(byte)
public Task RequestFeedbackAsync(byte groupIndex)
Parameters
groupIndexbyte
Returns
RequestFirmwareVersionAsync()
public Task RequestFirmwareVersionAsync()
Returns
RequestLocoInfoAsync(ushort)
public Task RequestLocoInfoAsync(ushort locoAddress)
Parameters
locoAddressushort
Returns
RequestModelTimeAsync()
public Task RequestModelTimeAsync()
Returns
RequestStatusAsync()
public Task RequestStatusAsync()
Returns
RequestSystemStateAsync()
public Task RequestSystemStateAsync()
Returns
RequestTurnoutInfoAsync(ushort)
public Task RequestTurnoutInfoAsync(ushort accessoryAddress)
Parameters
accessoryAddressushort
Returns
SendCommandsAsync(params IZ21Command[])
Sends one or more raw commands in a single UDP packet.
public Task SendCommandsAsync(params IZ21Command[] commands)
Parameters
commandsIZ21Command[]
Returns
SetExtAccessoryAsync(ushort, byte)
public Task SetExtAccessoryAsync(ushort accessoryAddress, byte payload)
Parameters
Returns
SetFunctionAsync(ushort, ushort, FunctionToggleType)
public Task SetFunctionAsync(ushort locoAddress, ushort functionIndex, FunctionToggleType toggleType)
Parameters
locoAddressushortfunctionIndexushorttoggleTypeFunctionToggleType
Returns
SetModelTimeAsync(ModelTime)
public Task SetModelTimeAsync(ModelTime time)
Parameters
timeModelTime
Returns
SetTurnoutAsync(ushort, AccessoryOutput, AccessoryState, bool)
public Task SetTurnoutAsync(ushort accessoryAddress, AccessoryOutput output, AccessoryState state, bool executeImmediately)
Parameters
accessoryAddressushortoutputAccessoryOutputstateAccessoryStateexecuteImmediatelybool
Returns
StartModelTimeAsync()
public Task StartModelTimeAsync()
Returns
StopModelTimeAsync()
public Task StopModelTimeAsync()
Returns
TrackPowerOffAsync()
public Task TrackPowerOffAsync()
Returns
TrackPowerOnAsync()
public Task TrackPowerOnAsync()
Returns
WriteCvAsync(ushort, byte)
public Task WriteCvAsync(ushort cvAddress, byte value)
Parameters
Returns
WriteCvAsync(ushort, byte, TimeSpan)
Writes a CV on the programming track, retrying while the decoder does not acknowledge, and
completes once the command station acknowledges the write (LAN_X_CV_RESULT). A missing
acknowledgement that never clears is reported as a timeout. Do not call the fire-and-forget CV
methods concurrently with this one on the same station.
public Task WriteCvAsync(ushort cvAddress, byte value, TimeSpan timeout)
Parameters
Returns
Exceptions
- CvOperationTimeoutException
The write was not acknowledged within
timeout.- CvShortCircuitException
The command station reported a short circuit.
- ArgumentOutOfRangeException
timeoutis not a positive, in-range duration.
WritePomCvAsync(ushort, ushort, byte, TimeSpan)
Writes a CV of a locomotive decoder on the main track (POM). Because a POM write returns no acknowledgement, this verifies by reading the CV back and retrying until the read-back matches the written value (so it requires RailCom). A decoder that never reads back the written value is reported as a timeout. Do not run other CV operations on this station concurrently.
public Task WritePomCvAsync(ushort locoAddress, ushort cvAddress, byte value, TimeSpan timeout)
Parameters
Returns
Exceptions
- CvOperationTimeoutException
The write could not be confirmed within
timeout.- CvShortCircuitException
The command station reported a short circuit.
- ArgumentOutOfRangeException
timeoutis not a positive, in-range duration.
WritePomCvBitAsync(ushort, ushort, byte, bool, TimeSpan)
Writes a single bit of a CV of a locomotive decoder on the main track (POM). Because a POM write returns no acknowledgement, this verifies by reading the CV byte back and retrying until the target bit matches the written value (so it requires RailCom). A bit that never reads back is reported as a timeout. Do not run other CV operations on this station concurrently.
public Task WritePomCvBitAsync(ushort locoAddress, ushort cvAddress, byte bitPosition, bool bitValue, TimeSpan timeout)
Parameters
locoAddressushortcvAddressushortbitPositionbyteThe 0-based bit position within the CV (0–7).
bitValuebooltimeoutTimeSpan
Returns
Exceptions
- CvOperationTimeoutException
The write could not be confirmed within
timeout.- CvShortCircuitException
The command station reported a short circuit.
- ArgumentOutOfRangeException
bitPositionexceeds 7, ortimeoutis not a positive, in-range duration.
Events
ConnectionChanged
public event EventHandler<ConnectionChangedEventArgs>? ConnectionChanged
Event Type
CvProgrammingFailed
public event EventHandler<CvProgrammingError>? CvProgrammingFailed
Event Type
CvReadCompleted
public event EventHandler<CvValue>? CvReadCompleted
Event Type
ExtAccessoryInfoReceived
public event EventHandler<ExtAccessoryInfo>? ExtAccessoryInfoReceived
Event Type
FeedbackChanged
public event EventHandler<FeedbackData>? FeedbackChanged
Event Type
FirmwareVersionReceived
public event EventHandler<FirmwareVersion>? FirmwareVersionReceived
Event Type
LocoInfoReceived
public event EventHandler<LocoInfoData>? LocoInfoReceived
Event Type
ModelTimeChanged
public event EventHandler<ModelTime>? ModelTimeChanged
Event Type
StatusChanged
public event EventHandler<CentralState>? StatusChanged
Event Type
SystemStateReceived
public event EventHandler<SystemState>? SystemStateReceived
Event Type
TrackPowerChanged
Raised when track power is switched on (true) or off (false).
public event EventHandler<bool>? TrackPowerChanged
Event Type
TurnoutInfoReceived
public event EventHandler<TurnoutInfo>? TurnoutInfoReceived