private async Task Connect() { cts = new CancellationTokenSource(); IsCancellationRequested = false; InputFrame receivedFrame = null; isInitialized = true; // Connect with the robot try { Task.Run(async() => { receivedFrame = await rsiAdapter.Connect(Config.Port); }).Wait(cts.Token); } catch (OperationCanceledException) { rsiAdapter.Disconnect(); return; } lock (generatorSyncLock) { generator.Initialize(receivedFrame.Position); } lock (receivedDataSyncLock) { position = receivedFrame.Position; HomePosition = receivedFrame.Position; } // Send first response OutputFrame response = new OutputFrame() { Correction = RobotVector.Zero, IPOC = receivedFrame.IPOC }; rsiAdapter.SendData(response); Initialized?.Invoke(this, EventArgs.Empty); // Start loop for receiving and sending data while (!IsCancellationRequested) { long IPOC = await ReceiveDataAsync(); SendData(IPOC); } }
private void Connect() { IsCancellationRequested = false; InputFrame receivedFrame = new InputFrame { Position = HomePosition, AxisPosition = RobotAxisVector.Zero, IPOC = 0 }; generator.Initialize(receivedFrame.Position); lock (receivedDataSyncLock) { position = receivedFrame.Position; HomePosition = receivedFrame.Position; } isInitialized = true; Initialized?.Invoke(this, EventArgs.Empty); // Start loop for receiving and sending data try { while (!IsCancellationRequested) { long IPOC = ReceiveDataAsync(); SendData(IPOC); Thread.Sleep(4); } } catch (Exception e) { var args = new ErrorOccuredEventArgs { RobotIp = ToString(), Exception = e }; ErrorOccured?.Invoke(this, args); } isInitialized = false; Uninitialized?.Invoke(this, EventArgs.Empty); }