public async Task Initialize() { CancellationTokenSource tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5)); try { await Task.Factory.StartNew(() => TcpserverEx.net_port_initial(), tcs.Token); await Task.Delay(1200); this.serverStarted = true; } catch (TaskCanceledException) { throw new TaskCanceledException("TcpserverEx.net_port_initial Failed, task cancelled due to timeout"); } catch (Exception ex) { throw new Exception("TcpserverEx.net_port_initial Failed", ex); } this.robot = TcpserverEx.get_robot(this.id); int attempts = 0; bool isSucccess = false; while (attempts++ < 3) { isSucccess = robot.is_connected(); if (isSucccess) { break; } await Task.Delay(1000); } if (!isSucccess) { throw new Exception("Could not connect to Z-Arm with id: " + this.id); } isSucccess = false; int returnCode = -1; while (attempts++ < 3) { returnCode = robot.initial(generation, verticalReach); await Task.Delay(1000); } if (returnCode != 1) { throw new Exception($"Could not initialize Z-Arm Id {this.id}, Code: {returnCode}, Message: {this.GetDebugMessage(returnCode)}"); } robot.unlock_position(); robot.set_arm_length(armLength, armLength); robot.set_catch_or_release_accuracy(0.5f); }
public async Task Initialize(bool goHome = true) { Console.WriteLine("Initializing Server"); CancellationTokenSource tcs = new CancellationTokenSource(TimeSpan.FromSeconds(5)); try { await Task.Factory.StartNew(() => TcpserverEx.net_port_initial(), tcs.Token); } catch (TaskCanceledException) { throw new TaskCanceledException("TcpserverEx.net_port_initial Failed, task cancelled due to timeout"); } catch (Exception ex) { throw new Exception("TcpserverEx.net_port_initial Failed", ex); } Console.WriteLine("Connecting to ZArm"); this.robot = TcpserverEx.get_robot(this.id); Stopwatch watch = Stopwatch.StartNew(); while (!robot.is_connected()) { if (watch.ElapsedMilliseconds > 5000) { throw new TimeoutException("Could not connect to Z-Arm"); } Console.Write("."); await Task.Delay(50); } Console.WriteLine($"Connected in {watch.ElapsedMilliseconds}ms"); if (goHome) { await this.GoHome(TimeSpan.FromSeconds(10)); } Console.WriteLine("Initialize ZArm, unlock position, and set arm length and accuracy"); robot.initial(this.generation, this.verticalReach); robot.set_arm_length(armLength, armLength); robot.set_catch_or_release_accuracy(0.5f); robot.unlock_position(); }
private async void buttonConnect_Click(object sender, EventArgs e) { robot = TcpserverEx.get_robot(zArmId); bool isConnected = false; while (!isConnected) { isConnected = await Task.Factory.StartNew(() => robot.is_connected()); debugBox.AppendText(string.Format("Attempting to connect to Z-Arm {0}... {1}\n", zArmId, isConnected)); await Task.Delay(1000); } debugBox.AppendText(string.Format("IsConnected: {0}\n", robot.is_connected())); //debugBox.AppendText($"initial_finish: {robot.initial_finish}, communicate_success: {robot.communicate_success}\n"); int ret = -1; while (ret != 1) { ret = await Task.Factory.StartNew(() => robot.initial(1, 310)); debugBox.AppendText($"ret: {ret}\n"); await Task.Delay(1000); } if (ret != 1) { debugBox.AppendText(string.Format("Could not initialize Z Arm id {0}, return value: {1}\n", zArmId, ret)); } else { robot.unlock_position(); robot.set_arm_length(200, 200); robot.set_catch_or_release_accuracy(0.5f); debugBox.AppendText(string.Format("Z Arm id: {0} connected!\n", zArmId)); } }
private async void connectButton_Click(object sender, EventArgs e) { try { Task task = Task.Factory.StartNew(() => TcpserverEx.net_port_initial()); await task; if (task.IsFaulted) { debugTextbox.AppendText("No net port initial\r\n"); } if (task.IsCanceled) { debugTextbox.AppendText("Cancelled\r\n"); } if (task.IsCompleted) { debugTextbox.AppendText("Net port initial completed\r\n"); } if (task.Exception != null) { throw task.Exception; } } catch (Exception ex) { debugTextbox.AppendText(ex.Message); return; } // Wait for net_port_initial to complete await Task.Delay(1200); debugTextbox.AppendText("Finding Z-Arm. \r\n"); bool found = false; try { for (int i = 0; i < 256; i++) { if (TcpserverEx.card_number_connect(i) == 1) { zArmId = i; found = true; debugTextbox.AppendText($"Id: {i}\r\n"); break; } } } catch (Exception ex) { debugTextbox.AppendText(ex.Message); return; } if (!found) { debugTextbox.AppendText("No arm found. Retry.\r\n"); return; } // Connect to the Z-Arm robot = TcpserverEx.get_robot(zArmId); int retryCount = 0; bool isConnected = false; while (!isConnected) { isConnected = await Task.Factory.StartNew(() => robot.is_connected()); debugTextbox.AppendText($"Attempting to connect to Z-Arm {zArmId}: {isConnected}\r\n"); if (++retryCount > 3) { break; } await Task.Delay(1000); } if (retryCount >= 3) { debugTextbox.AppendText($"Could not connect. Retry.\r\n"); return; } int ret = -1; retryCount = 0; while (ret != 1) { ret = await Task.Factory.StartNew(() => robot.initial(generation, verticalReach)); debugTextbox.AppendText($"Initializing: {ret} = {initialDebugMap[ret]}\r\n"); if (++retryCount > 3) { break; } await Task.Delay(1000); } if (ret != 1) { debugTextbox.AppendText($"Could not initialize Z-Arm Id {zArmId}, return value: {ret}\r\n"); return; } else { robot.unlock_position(); robot.set_arm_length(armLength, armLength); robot.set_catch_or_release_accuracy(0.5f); debugTextbox.AppendText($"Z Arm Id {zArmId} Initialized!\r\n"); SetManualControls(true); } }