static void Main(string[] args) { // Lokomotive: 74 854 DB int locToTrigger = 1004; int maxSpeed = 40; int s88id = 102; // S88 module uint s88pinRight = 16; // S88 module pin: FB3 uint s88pinLeft = 13; // S88 module pin: FBL4 int waitDelay = 5 * 1000; Locomotive locItem = null; S88 s88Item = null; bool isStarted = false; bool isDrivingBackward = true; List <string> cmdsToSend = null; var ws = new WsConnect(); ws.WsData += (sender, dp) => { if (dp.GetObjectBy(s88id) is S88 localS88Item) { s88Item = localS88Item; } if (dp.GetObjectBy(locToTrigger) is Locomotive localLocItem) { locItem = localLocItem; } if (s88Item == null) { return; } if (locItem == null) { return; } var stateRight = (s88Item?.Pin(s88pinRight)).Value; var stateLeft = (s88Item?.Pin(s88pinLeft)).Value; if (!isStarted) { // drive from right -> left, i.e. backward if (stateRight && !stateLeft) { isDrivingBackward = true; } // drive from left -> right, i.e. forward if (stateLeft && !stateRight) { isDrivingBackward = false; } locItem.ChangeDirection(isDrivingBackward); locItem.ChangeSpeed(maxSpeed, true, true); cmdsToSend = locItem.GetCommands() as List <string>; ws.SendCommands(cmdsToSend); cmdsToSend?.Clear(); Console.WriteLine("Started!"); isStarted = true; } else { if (isDrivingBackward) { if (stateLeft) { Console.WriteLine("Stop left!"); locItem.Stop(); locItem.ChangeDirection(false); isDrivingBackward = false; cmdsToSend = locItem.GetCommands() as List <string>; ws.SendCommands(cmdsToSend); cmdsToSend?.Clear(); Console.WriteLine($"Wait {waitDelay} msecs!"); Thread.Sleep(waitDelay); Console.WriteLine("Start to right!"); locItem.ChangeSpeed(maxSpeed, true, true); cmdsToSend = locItem.GetCommands() as List <string>; ws.SendCommands(cmdsToSend); cmdsToSend?.Clear(); } } else { if (stateRight) { Console.WriteLine("Stop right!"); locItem.Stop(); locItem.ChangeDirection(true); isDrivingBackward = true; cmdsToSend = locItem.GetCommands() as List <string>; ws.SendCommands(cmdsToSend); cmdsToSend?.Clear(); Console.WriteLine($"Wait {waitDelay} msecs!"); Thread.Sleep(waitDelay); Console.WriteLine("Start to left!"); locItem.ChangeSpeed(maxSpeed, true, true); cmdsToSend = locItem.GetCommands() as List <string>; ws.SendCommands(cmdsToSend); cmdsToSend?.Clear(); } } } }; var res = ws.ConnectTo("ws://127.0.0.1:10050", new[] { "enableS88", "enableLocomotives" }); if (res) { Console.WriteLine("Wait for data..."); } Console.CancelKeyPress += (sender, eArgs) => { QuitEvent.Set(); eArgs.Cancel = true; }; QuitEvent.WaitOne(); }