public async Task Send() { PresentViewModel.Device.CurrentMode = CommModes.SendMode; CNCMessage message0 = new CNCMessage() { Message = CNCFileContentArray[0] }; CNCMessage answer = new CNCMessage(); CNCMessage posrequmessage = new CNCMessage(); CNCMessage posmessage = new CNCMessage(); CNCMessage rmessage = new CNCMessage() { Message = string.Empty }; CNC_Device Device = PresentViewModel.Device; CNCInterface Interface = Device.Interface; long count = 0; long maxcount = CNCFileContentArray.Count; int okcount = 0; float[] r = new float[3]; int wcount = 0; int recd = 0; await Task.Run(async() => { foreach (var item in CNCFileContentArray) { // Cancel Routine if (CancelSend) { CancelSend = false; StreamProgress = 1; PresentViewModel.Device.CurrentMode = CommModes.DefaultMode; return; } message0.Message = item; if (item.Contains("G4")) { MatchCollection match = Regex.Matches(item, @"\b(G4)\s*(P[0-9]*)"); if (match.Count > 0) { try { string debugstring = Regex.Matches(Regex.Matches(Regex.Matches(item, @"\b(G4)\s*(P[0-9]*)")[0].Value, @"(P)([0-9]*)")[0].Value, @"[0-9]*\B([0-9])")[0].Value; int waittime = Convert.ToInt32(debugstring); Thread.Sleep(waittime * 1000); continue; } catch (Exception) { throw; } } } PresentViewModel.Device.Interface.SendMessage(message0, false); okcount++; posrequmessage = PresentViewModel.Device.Protokoll.GetCurrentXYZMessage(); Interface.SendMessage(posrequmessage, false); okcount++; while (okcount > 0) { if (CancelSend) { CancelSend = false; StreamProgress = 1; PresentViewModel.Device.CurrentMode = CommModes.DefaultMode; await PresentViewModel.Device.SendSoftReset(); Thread.Sleep(1000); await PresentViewModel.Device.KillAlarm(); return; } rmessage = new CNCMessage(); rmessage = Interface.ReceiveMessage(100, false); if (rmessage.Message.Contains("ok") || rmessage.Message.Contains("error")) { okcount--; } else if (rmessage.Message.Contains("WPos")) { try { r[0] = Convert.ToSingle(Regex.Match(rmessage.Message, @"(WPos:([-0-9]+.[0-9]+),([-0-9]+.[0-9]+),([-0-9]+.[0-9]))").Groups[2].Value, CultureInfo.InvariantCulture); r[1] = Convert.ToSingle(Regex.Match(rmessage.Message, @"(WPos:([-0-9]+.[0-9]+),([-0-9]+.[0-9]+),([-0-9]+.[0-9]))").Groups[3].Value, CultureInfo.InvariantCulture); r[2] = Convert.ToSingle(Regex.Match(rmessage.Message, @"(WPos:([-0-9]+.[0-9]+),([-0-9]+.[0-9]+),([-0-9]+.[0-9]))").Groups[4].Value, CultureInfo.InvariantCulture); PresentViewModel.Device.CurrentX = r[0]; PresentViewModel.Device.CurrentY = r[1]; PresentViewModel.Device.CurrentZ = r[2]; } catch (Exception ex) { r[0] = (float)PresentViewModel.CurrentX; r[1] = (float)PresentViewModel.CurrentY; r[2] = (float)PresentViewModel.CurrentZ; } recd++; } if (wcount > 2 && recd != 0) { Interface.SendMessage(posrequmessage, false); okcount++; wcount = 0; recd = 0; } wcount++; } StreamProgress = (double)count / (double)maxcount; count++; } }); StreamProgress = 1; PresentViewModel.Device.CurrentMode = CommModes.DefaultMode; CancelSend = false; }
/// <summary> /// This Method gets called every time a static method is changed. Be !careful!, this method only gets called if the /// value actually change. If the new value is the same as before, than it not get changed. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PresentViewModel_StaticPropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case nameof(PresentViewModel.CurrentSelectedPortName): if (Device != null && Device.Interface.Portname == CurrentSelectedPortName) { Device.Interface.CloseConnection(); Device.RefreshInterval = 0; _Device = null; return; } //Delete old device and, most importantly, close old connection if (Device != null) { Device.Interface.CloseConnection(); Device.RefreshInterval = 0; _Device = null; } // Make new device CNCInterface iface = new SerialGRBLInterface(CurrentSelectedPortName, CurrentSelectedBaudRate); CNCProtokoll protokoll = new GRBLProtokoll(); _Device = new CNC_Device(iface, protokoll); (iface as SerialGRBLInterface).PortOpened += (s, k) => { ToolbarViewModel.IsConnected = true; }; (iface as SerialGRBLInterface).OpenPortFailed += (s, k) => { ToolbarViewModel.IsConnected = false; }; (iface as SerialGRBLInterface).PortClosed += (s, k) => { ToolbarViewModel.IsConnected = false; Device.RefreshInterval = 0; }; Device.PropertyChanged += Device_PropertyChanged; (iface as SerialGRBLInterface).FirePortOpened(); Device.SendReceiveBuffer.CollectionChanged += (s, k) => { if (k.NewItems == null) { return; } foreach (var item in k.NewItems) { ConsoleViewModel.AppendLineToConsole(item as string); } }; // TODO: Add Refresh Rate Textbox to ToolbarPresenter and bind it! Device.RefreshInterval = 100; break; case nameof(Device): break; default: break; } }