diff --git a/LaserGRBL/ComWrapper/LaserWebESP8266.cs b/LaserGRBL/ComWrapper/LaserWebESP8266.cs index 9dc05c5c6..ae2b70ac2 100644 --- a/LaserGRBL/ComWrapper/LaserWebESP8266.cs +++ b/LaserGRBL/ComWrapper/LaserWebESP8266.cs @@ -24,24 +24,21 @@ public void Configure(params object[] param) public void Open() { - if (cln == null) - { - if (string.IsNullOrEmpty(mAddress)) - throw new MissingFieldException("Missing HostName"); - else if (mPort == 0) - throw new MissingFieldException("Missing Port"); + if (cln != null) + Close(true); - buffer.Clear(); - cln = new WebSocketSharp.WebSocket(string.Format("ws://{0}:{1}", mAddress, mPort)); - Logger.LogMessage("OpenCom", "Open {0}:{1}", mAddress, mPort); - cln.OnMessage += cln_OnMessage; - cln.Connect(); - } - else - { - throw new InvalidOperationException("Port already opened"); - } + if (string.IsNullOrEmpty(mAddress)) + throw new MissingFieldException("Missing HostName"); + else if (mPort == 0) + throw new MissingFieldException("Missing Port"); + + buffer.Clear(); + cln = new WebSocketSharp.WebSocket(string.Format("ws://{0}:{1}", mAddress, mPort)); + Logger.LogMessage("OpenCom", "Open {0}:{1}", mAddress, mPort); + cln.OnMessage += cln_OnMessage; + cln.Connect(); + } public void Close(bool auto) @@ -51,12 +48,13 @@ public void Close(bool auto) try { Logger.LogMessage("CloseCom", "Close {0} [{1}]", mAddress, auto ? "CORE" : "USER"); - cln.Close(); cln.OnMessage -= cln_OnMessage; - buffer.Clear(); + + cln.Close(); } catch { } + buffer.Clear(); cln = null; } } @@ -76,6 +74,7 @@ public void WriteLine(string text) { if (IsOpen) cln.Send(text + "\r\n"); + //System.Threading.Thread.Sleep(10); } void cln_OnMessage(object sender, MessageEventArgs e) diff --git a/LaserGRBL/GrblEmulator/GrblEmulator.cs b/LaserGRBL/GrblEmulator/GrblEmulator.cs index ccf8d2802..40f83c56a 100644 --- a/LaserGRBL/GrblEmulator/GrblEmulator.cs +++ b/LaserGRBL/GrblEmulator/GrblEmulator.cs @@ -12,17 +12,24 @@ class GrblEmulator private static WebSocketServer srv; public static void Start() { - ConsoleAPI.HaveConsole = true; - srv = new WebSocketServer("ws://127.0.0.1:81"); - srv.AddWebSocketService("/"); - Console.WriteLine("Run Grbl emulator"); - srv.Start(); + if (srv == null) + { + ConsoleAPI.HaveConsole = true; + Console.WriteLine("ESP266 Grbl emulator (127.0.0.1:81)"); + + srv = new WebSocketServer("ws://127.0.0.1:81"); + srv.AddWebSocketService("/"); + srv.Start(); + } } public static void Stop() { if (srv != null) + { srv.Stop(); + srv = null; + } } @@ -68,33 +75,14 @@ private void SendConnected() protected override void OnMessage(MessageEventArgs e) { - if (e.IsBinary) - { - if (e.RawData.Length == 1) - { - if (e.RawData[0] == 24) - GrblReset(); - else if (e.RawData[0] == 63) - SendStatus(); - else - PrintArray(e.RawData); - } - else - { - PrintArray(e.RawData); - } - } - else if (e.IsText) - { - if (e.Data == "?") - SendStatus(); - else if (e.Data == "version\n") - ; - else if (e.Data == "{fb:n}\n") - ; - else - EnqueueCommand(e); - } + if (e.Data == "?") + SendStatus(); + else if (e.Data == "version\n") + ; + else if (e.Data == "{fb:n}\n") + ; + else + EnqueueCommand(e); } private void EnqueueCommand(MessageEventArgs e) @@ -137,27 +125,32 @@ private void ManageQueue() { if (buffer.Count > 0) { - string line = buffer.Dequeue(); + try + { + string line = buffer.Dequeue(); + + LaserGRBL.GrblCommand C = new GrblCommand(line); - LaserGRBL.GrblCommand C = new GrblCommand(line); + if (C.IsAbsoluteCoord) + absolute = true; + else if (C.IsRelativeCoord) + absolute = false; - if (C.IsAbsoluteCoord) - absolute = true; - else if (C.IsRelativeCoord) - absolute = false; + if (C.TrueMovement(x, y, absolute)) + { + x = C.X != null ? C.X.Number : x; + y = C.Y != null ? C.Y.Number : y; + //z = C.Z != null ? C.Z.Number : z; + System.Threading.Thread.Sleep(30); + } - if (C.TrueMovement(x,y, absolute)) + Console.WriteLine(C.Command.Trim("\r\n".ToCharArray())); + Send("OK\r\n"); + } + catch (Exception ex) { - x = C.X != null ? C.X.Number : x; - y = C.Y != null ? C.Y.Number : y; - //z = C.Z != null ? C.Z.Number : z; - System.Threading.Thread.Sleep(30); } - - Console.WriteLine(C.Command.Trim("\r\n".ToCharArray())); - - Send("OK\r\n"); } } } diff --git a/LaserGRBL/MainForm.Designer.cs b/LaserGRBL/MainForm.Designer.cs index ad28cc49a..eb2420fb3 100644 --- a/LaserGRBL/MainForm.Designer.cs +++ b/LaserGRBL/MainForm.Designer.cs @@ -31,6 +31,9 @@ private void InitializeComponent() this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); this.splitContainer1 = new System.Windows.Forms.SplitContainer(); + this.ConnectionForm = new LaserGRBL.ConnectLogForm(); + this.JogForm = new LaserGRBL.JogForm(); + this.PreviewForm = new LaserGRBL.PreviewForm(); this.StatusBar = new System.Windows.Forms.StatusStrip(); this.TTLLines = new System.Windows.Forms.ToolStripStatusLabel(); this.TTTLines = new System.Windows.Forms.ToolStripStatusLabel(); @@ -72,6 +75,7 @@ private void InitializeComponent() this.MNGerman = new System.Windows.Forms.ToolStripMenuItem(); this.MNDanish = new System.Windows.Forms.ToolStripMenuItem(); this.MNBrazilian = new System.Windows.Forms.ToolStripMenuItem(); + this.russianToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem(); this.helpOnLineToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.autoUpdateToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -79,10 +83,7 @@ private void InitializeComponent() this.openSessionLogToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem7 = new System.Windows.Forms.ToolStripSeparator(); this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.russianToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.ConnectionForm = new LaserGRBL.ConnectLogForm(); - this.JogForm = new LaserGRBL.JogForm(); - this.PreviewForm = new LaserGRBL.PreviewForm(); + this.grblEmulatorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); this.splitContainer1.SuspendLayout(); @@ -106,6 +107,21 @@ private void InitializeComponent() this.splitContainer1.Panel2.Controls.Add(this.PreviewForm); this.splitContainer1.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.splitContainer1_SplitterMoved); // + // ConnectionForm + // + resources.ApplyResources(this.ConnectionForm, "ConnectionForm"); + this.ConnectionForm.Name = "ConnectionForm"; + // + // JogForm + // + resources.ApplyResources(this.JogForm, "JogForm"); + this.JogForm.Name = "JogForm"; + // + // PreviewForm + // + resources.ApplyResources(this.PreviewForm, "PreviewForm"); + this.PreviewForm.Name = "PreviewForm"; + // // StatusBar // this.StatusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -389,6 +405,12 @@ private void InitializeComponent() this.MNBrazilian.Name = "MNBrazilian"; this.MNBrazilian.Click += new System.EventHandler(this.MNBrazilian_Click); // + // russianToolStripMenuItem + // + resources.ApplyResources(this.russianToolStripMenuItem, "russianToolStripMenuItem"); + this.russianToolStripMenuItem.Name = "russianToolStripMenuItem"; + this.russianToolStripMenuItem.Click += new System.EventHandler(this.russianToolStripMenuItem_Click); + // // toolStripMenuItem4 // this.toolStripMenuItem4.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -396,6 +418,7 @@ private void InitializeComponent() this.autoUpdateToolStripMenuItem, this.toolStripMenuItem5, this.openSessionLogToolStripMenuItem, + this.grblEmulatorToolStripMenuItem, this.toolStripMenuItem7, this.aboutToolStripMenuItem}); this.toolStripMenuItem4.Name = "toolStripMenuItem4"; @@ -438,26 +461,11 @@ private void InitializeComponent() resources.ApplyResources(this.aboutToolStripMenuItem, "aboutToolStripMenuItem"); this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); // - // russianToolStripMenuItem - // - resources.ApplyResources(this.russianToolStripMenuItem, "russianToolStripMenuItem"); - this.russianToolStripMenuItem.Name = "russianToolStripMenuItem"; - this.russianToolStripMenuItem.Click += new System.EventHandler(this.russianToolStripMenuItem_Click); - // - // ConnectionForm - // - resources.ApplyResources(this.ConnectionForm, "ConnectionForm"); - this.ConnectionForm.Name = "ConnectionForm"; - // - // JogForm - // - resources.ApplyResources(this.JogForm, "JogForm"); - this.JogForm.Name = "JogForm"; - // - // PreviewForm + // grblEmulatorToolStripMenuItem // - resources.ApplyResources(this.PreviewForm, "PreviewForm"); - this.PreviewForm.Name = "PreviewForm"; + this.grblEmulatorToolStripMenuItem.Name = "grblEmulatorToolStripMenuItem"; + resources.ApplyResources(this.grblEmulatorToolStripMenuItem, "grblEmulatorToolStripMenuItem"); + this.grblEmulatorToolStripMenuItem.Click += new System.EventHandler(this.grblEmulatorToolStripMenuItem_Click); // // MainForm // @@ -538,6 +546,7 @@ private void InitializeComponent() private System.Windows.Forms.ToolStripMenuItem MNDanish; private System.Windows.Forms.ToolStripMenuItem MNBrazilian; private System.Windows.Forms.ToolStripMenuItem russianToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem grblEmulatorToolStripMenuItem; } } diff --git a/LaserGRBL/MainForm.cs b/LaserGRBL/MainForm.cs index 8b4195ab9..7e25454db 100644 --- a/LaserGRBL/MainForm.cs +++ b/LaserGRBL/MainForm.cs @@ -377,5 +377,10 @@ private void russianToolStripMenuItem_Click(object sender, EventArgs e) { SetLanguage(new System.Globalization.CultureInfo("ru")); } + + private void grblEmulatorToolStripMenuItem_Click(object sender, EventArgs e) + { + LaserGRBL.GrblEmulator.Start(); + } } } diff --git a/LaserGRBL/MainForm.resx b/LaserGRBL/MainForm.resx index 086e8109e..5daacdcc1 100644 --- a/LaserGRBL/MainForm.resx +++ b/LaserGRBL/MainForm.resx @@ -148,7 +148,7 @@ ConnectionForm - LaserGRBL.ConnectLogForm, LaserGRBL, Version=2.6.10.36741, Culture=neutral, PublicKeyToken=null + LaserGRBL.ConnectLogForm, LaserGRBL, Version=2.6.12.32096, Culture=neutral, PublicKeyToken=null splitContainer1.Panel1 @@ -175,7 +175,7 @@ JogForm - LaserGRBL.JogForm, LaserGRBL, Version=2.6.10.36741, Culture=neutral, PublicKeyToken=null + LaserGRBL.JogForm, LaserGRBL, Version=2.6.12.32096, Culture=neutral, PublicKeyToken=null splitContainer1.Panel1 @@ -217,7 +217,7 @@ PreviewForm - LaserGRBL.PreviewForm, LaserGRBL, Version=2.6.10.36741, Culture=neutral, PublicKeyToken=null + LaserGRBL.PreviewForm, LaserGRBL, Version=2.6.12.32096, Culture=neutral, PublicKeyToken=null splitContainer1.Panel2 @@ -719,7 +719,7 @@ - 152, 22 + 118, 22 English @@ -737,7 +737,7 @@ - 152, 22 + 118, 22 Italian @@ -755,7 +755,7 @@ - 152, 22 + 118, 22 Spanish @@ -773,7 +773,7 @@ - 152, 22 + 118, 22 French @@ -790,7 +790,7 @@ - 152, 22 + 118, 22 German @@ -807,7 +807,7 @@ - 152, 22 + 118, 22 Danish @@ -829,7 +829,7 @@ - 152, 22 + 118, 22 Brazilian @@ -845,7 +845,7 @@ - 152, 22 + 118, 22 Russian @@ -877,6 +877,12 @@ Open session log + + 164, 22 + + + Grbl Emulator + 161, 6 @@ -1004,9 +1010,6 @@ AD//4AD//+AB///gAf//4AH///AD///wA/8= - - NoControl - 4, 4, 4, 4 @@ -1247,6 +1250,12 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + russianToolStripMenuItem + + + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + toolStripMenuItem4 @@ -1289,10 +1298,10 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - russianToolStripMenuItem + + grblEmulatorToolStripMenuItem - + System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 diff --git a/LaserGRBL/Program.cs b/LaserGRBL/Program.cs index 616682c7b..b1e030968 100644 --- a/LaserGRBL/Program.cs +++ b/LaserGRBL/Program.cs @@ -19,16 +19,9 @@ static void Main(string[] args) System.Globalization.CultureInfo ci = (System.Globalization.CultureInfo)Settings.GetObject("User Language", null); if (ci != null) System.Threading.Thread.CurrentThread.CurrentUICulture = ci; - foreach (string arg in args) - { - if (arg == "WsEmu") - LaserGRBL.GrblEmulator.Start(); - } - Application.Run(new MainForm()); LaserGRBL.GrblEmulator.Stop(); - Logger.Stop(); } }