/// <summary> /// Waits for a CNCMessage which contains <param name="containing"></param> /// /// throws: /// ApplicationException -- error message received /// </summary> /// <param name="timeout"></param> /// <param name="containing"></param> /// <param name="waittimout"></param> /// <param name="logging"></param> /// <returns></returns> public override CNCMessage WaitReceiveMessageContaining(int timeout, string containing, int waittimout, bool logging = true) { CNCMessage rmessage = new CNCMessage() { Message = "" }; LastMessageReceived = null; if (waittimout == 0) { try { SerialInterface.ReadTimeout = timeout; rmessage = ReceiveMessage(timeout, logging: logging); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); if (rmessage.Message.Contains("error")) { throw new ApplicationException(rmessage.Message); } } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } } else { Stopwatch sw = new Stopwatch(); sw.Start(); while (!rmessage.Message.Contains(containing)) { try { SerialInterface.ReadTimeout = timeout; rmessage = ReceiveMessage(timeout, logging: logging); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); if (rmessage.Message.Contains("error")) { throw new ApplicationException(rmessage.Message); } } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } if (sw.ElapsedMilliseconds >= waittimout) { return(rmessage); } } } return(rmessage); }
public async Task <CNCMessage> SendCustomMessage(CNCMessage message, int WaitTimeout = 1000) { CNCMessage tmp = new CNCMessage(); CNCMessage posrequmessage = new CNCMessage(); CNCMessage posmessage = new CNCMessage(); string regexstring = string.Empty; float[] r = new float[3]; try { await Task.Run(() => { Interface.SendMessage(message); tmp = Interface.WaitReceiveMessageContaining(100, "ok", WaitTimeout); }); } catch (ApplicationException ex) { return(new CNCMessage() { Message = ex.Message }); } return(tmp); }
public override void SendMessage(CNCMessage message, bool logging = true) { if (AutoPoll) { MessageBuffer.Add(message); OnMessageSent(this, message); } else { try { SerialInterface.WriteLine(message.Message); } catch (InvalidOperationException ex) { AddToSendReceiveBuffer(ex.Message); return; } } if (logging) { AddToSendReceiveBuffer(message.Message); } }
public void OnMessageSent(object sender, CNCMessage message) { MessageSent(sender, new CNCEventsArgs() { Message = message }); }
public async Task SendLine() { CNCMessage m = new CNCMessage() { Message = CustomLineContent }; await PresentViewModel.Device.SendCustomMessage(m); }
public void OnMessageReceived(object sender, CNCMessage message) { if (MessageReceived != null) { MessageReceived(sender, new CNCEventsArgs() { Message = message }); } }
public override async Task <CNCMessage> WaitReceiveMessageAsync(int TimeOut = 100, CNCMessage WaitForMessage = null, int WaitTimeout = 0) { CNCMessage rmessage = new CNCMessage() { Message = "" }; LastMessageReceived = null; if (WaitTimeout == 0) { try { SerialInterface.ReadTimeout = TimeOut; rmessage = await ReceiveMessageAsync(TimeOut); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } } else { Stopwatch sw = new Stopwatch(); sw.Start(); while (!rmessage.Message.Contains(WaitForMessage.Message)) { try { SerialInterface.ReadTimeout = TimeOut; rmessage = await ReceiveMessageAsync(TimeOut); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } if (sw.ElapsedMilliseconds >= WaitTimeout) { return(rmessage); } } } return(rmessage); }
public async Task <CNCMessage> JogZ(double Z, double Feed) { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetJogByYMessage(Z, Feed); Interface.SendMessage(message); tmp = Interface.ReceiveMessage(100); }); return(tmp); }
public async Task <CNCMessage> SendSetZero() { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetSetZeroMessage(); Interface.SendMessage(message); tmp = Interface.ReceiveMessage(100); }); return(tmp); }
public async Task <float[]> GetCurrentXYZ() { float[] r = new float[3]; await Task.Run(() => { CNCMessage message = Protokoll.GetCurrentXYZMessage(); if (ConsoleViewModel.Verbose) { Interface.SendMessage(message); } else { Interface.SendMessage(message, false); } CNCMessage output; CNCMessage outokerror; List <string> outarray = new List <string>(); outarray.Add("ok"); outarray.Add("error"); if (ConsoleViewModel.Verbose) { output = Interface.WaitReceiveMessageContaining(100, "WPos", 100); outokerror = Interface.WaitReceiveMessageContainingMultible(100, outarray, 100); } else { output = Interface.WaitReceiveMessageContaining(100, "WPos", 100, logging: false); outokerror = Interface.WaitReceiveMessageContainingMultible(100, outarray, 100, logging: false); } try { r[0] = Convert.ToSingle(Regex.Match(output.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(output.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(output.Message, @"(WPos:([-0-9]+.[0-9]+),([-0-9]+.[0-9]+),([-0-9]+.[0-9]))").Groups[4].Value, CultureInfo.InvariantCulture); } catch (Exception ex) { r[0] = CurrentX; r[1] = CurrentY; r[2] = CurrentZ; } }); return(r); }
/// <summary> /// Private Function which is deriving the current Z in the cnc-controller /// </summary> /// <returns></returns> public async Task <float> GetCurrentZ() { float r = 0; await Task.Run(() => { CNCMessage message = Protokoll.GetCurrentZMessage(); Interface.SendMessage(message); CNCMessage output = Interface.ReceiveMessage(100); var tmp = Regex.Match(output.Message, @"(WPos:([-0-9]+.[0-9]+),([-0-9]+.[0-9]+),([-0-9]+.[0-9]+))").Groups[4].Value; r = Convert.ToSingle(tmp, CultureInfo.InvariantCulture); }); return(r); }
public async Task <CNCMessage> MoveByXYZ(double X, double Y, double Z, double F) { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetMoveByXYZMessage(X, Y, Z, F); Interface.SendMessage(message); CNCMessage t = new CNCMessage() { Message = "ok" }; tmp = Interface.WaitReceiveMessage(100, t, 1000); }); return(tmp); }
public async Task <CNCMessage> MoveByX(double X, double F) { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetMoveByXMessage(X, F); Interface.SendMessage(message); CNCMessage t = new CNCMessage() { Message = "ok" }; tmp = (Interface as SerialGRBLInterface).WaitReceiveMessage(100, t, 1000); }); return(tmp); }
public async Task <CNCMessage> SetSpindleSpeed(double RPM, string Direction) { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetSpindelSetRPMMessage(RPM, Direction); Interface.SendMessage(message); CNCMessage t = new CNCMessage() { Message = "ok" }; tmp = Interface.WaitReceiveMessage(100, t, 1000); }); return(tmp); }
public async Task <CNCMessage> KillAlarm() { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetKillAlarmMessage(); Interface.SendMessage(message); CNCMessage t = new CNCMessage() { Message = "ok" }; tmp = Interface.WaitReceiveMessage(100, t, 1000); }); return(tmp); }
public async Task <CNCMessage> RelativeJogZ(double Z, double F) { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetRelativeJogByZMessage(Z, F); Interface.SendMessage(message); CNCMessage t = new CNCMessage() { Message = "ok" }; tmp = Interface.WaitReceiveMessage(100, t, 1000); }); return(tmp); }
public override async Task <CNCMessage> ReceiveMessageAsync(int timeout = 100) { CNCMessage rmessage = new CNCMessage(); LastMessageReceived = null; try { SerialInterface.ReadTimeout = timeout; var tmp = new byte[1]; byte recbyte = 0x00; string recstring = string.Empty; while (!recstring.Contains("\n") && !recstring.Contains("\r")) { await SerialInterface.BaseStream.ReadAsync(tmp, 0, 1); recstring += Encoding.ASCII.GetString(tmp); } rmessage.Message = BitConverter.ToString(tmp).Trim(); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; return(rmessage); } catch (InvalidOperationException ex) { rmessage.Message = ex.Message; CloseConnection(); return(rmessage); } catch (ArgumentOutOfRangeException ex) { rmessage.Message = ex.Message; return(rmessage); } AddToSendReceiveBuffer(rmessage.Message); return(rmessage); }
public async Task <CNCMessage> JogX(double X, double Feed) { CNCMessage tmp = null; await Task.Run(() => { CNCMessage message = Protokoll.GetJogByXMessage(X, Feed); CNCMessage t = new CNCMessage() { Message = "ok" }; Interface.SendMessage(message); tmp = Interface.WaitReceiveMessage(100, t, 1000); }); return(tmp); }
public async Task <CNCMessage> JogY(double Y, double Feed) { CNCMessage tmp = null; await Task.Run(() => { GRBLProtokoll pr = new GRBLProtokoll(); CNCMessage m = pr.GetCurrentFeedMessage(); //CNCMessage start = new CNCMessage() { Message = "Grbl 1.1g ['$' for help]" }; //var an = Interface.WaitReceiveMessage(100, start, 2000); CNCMessage message = Protokoll.GetJogByYMessage(Y, Feed); Interface.SendMessage(message); CNCMessage t = new CNCMessage() { Message = "ok" }; tmp = Interface.WaitReceiveMessage(100, t, 1000); }); return(tmp); }
/// <summary> /// Private Function which is deriving the current X in the cnc-controller /// </summary> /// <returns></returns> public async Task <float> GetCurrentX() { float r = 0; await Task.Run(() => { CNCMessage message = Protokoll.GetCurrentXMessage(); Interface.SendMessage(message); CNCMessage output = Interface.WaitReceiveMessageContaining(100, "WPos", 1000); var tmp = Regex.Match(output.Message, @"(WPos:([-0-9]+.[0-9]+),([-0-9]+.[0-9]+),([-0-9]+.[0-9]))").Groups[2].Value; try { r = Convert.ToSingle(tmp, CultureInfo.InvariantCulture); } catch (FormatException ex) { r = 0; } }); return(r); }
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; }
public abstract Task <CNCMessage> WaitReceiveMessageAsync(int TimeOut, CNCMessage WaitForMessage, int WaitTimout);
public abstract CNCMessage WaitReceiveMessage(int TimeOut, CNCMessage WaitForMessage, int WaitTimout, bool logging = true);
public abstract void SendMessage(CNCMessage message, bool logging = true);
public override CNCMessage ReceiveMessage(int TimeOut = 100, bool logging = true) { CNCMessage rmessage = new CNCMessage(); LastMessageReceived = null; try { if (SerialInterface.ReadTimeout != TimeOut) { SerialInterface.ReadTimeout = TimeOut; } if (SerialInterface.BytesToRead >= 0) { rmessage.Message = SerialInterface.ReadLine(); } LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; return(rmessage); } catch (InvalidOperationException ex) { rmessage.Message = ex.Message; CloseConnection(); if (logging) { AddToSendReceiveBuffer(rmessage.Message); } return(rmessage); } catch (ArgumentOutOfRangeException ex) { rmessage.Message = ex.Message; if (logging) { AddToSendReceiveBuffer(rmessage.Message); } return(rmessage); } catch (IndexOutOfRangeException ex) { // This happens if while this thread is reading bytes from SerialPort, a other // thread is reading the bytes from port. this can then result in an -1 data pointer // (SerialPort is not thread save). // The good news are; this only means the message was already read from port and nothing // else is to do. Debugger.Break(); rmessage.Message = "-"; return(rmessage); } catch (OverflowException ex) { // This happens if while this thread is reading bytes from SerialPort, a other // thread is reading the bytes from port. this can then result in an -1 data pointer // (SerialPort is not thread save). // The good news are; this only means the message was already read from port and nothing // else is to do. //Debugger.Break(); rmessage.Message = "-"; return(rmessage); } catch (IOException ex) { // The I/O operation has been aborted because of either a thread exit or an application request.\r\n Debugger.Break(); rmessage.Message = "-"; return(rmessage); } if (logging) { AddToSendReceiveBuffer(rmessage.Message); } return(rmessage); }
public override CNCMessage WaitReceiveMessageContainingMultible(int timeout, List <string> containing, int waittimout, bool logging = true) { if (containing is null) { throw new ArgumentNullException(nameof(containing)); } CNCMessage rmessage = new CNCMessage() { Message = "" }; LastMessageReceived = null; if (waittimout == 0) { try { if (SerialInterface.ReadTimeout != timeout) { SerialInterface.ReadTimeout = timeout; } rmessage = ReceiveMessage(timeout, logging: logging); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } } else if (waittimout < 0) { while (containing.Find(s => rmessage.Message.Contains(s)) == null ? true : false) { try { SerialInterface.ReadTimeout = timeout; rmessage = ReceiveMessage(timeout, logging: logging); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } } } else { Stopwatch sw = new Stopwatch(); sw.Start(); while (containing.Find(s => rmessage.Message.Contains(s)) == null ? true : false) { try { SerialInterface.ReadTimeout = timeout; rmessage = ReceiveMessage(timeout, logging: logging); LastMessageReceived = rmessage; OnMessageReceived(this, rmessage); } catch (TimeoutException ex) { rmessage.Message = "TIMEOUT"; } if (sw.ElapsedMilliseconds >= waittimout) { return(rmessage); } } } return(rmessage); }