Skip to content

Commit

Permalink
websocket and emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
arkypita committed Jun 25, 2017
1 parent 81795a3 commit 926d82e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 112 deletions.
35 changes: 17 additions & 18 deletions LaserGRBL/ComWrapper/LaserWebESP8266.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}
Expand All @@ -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)
Expand Down
87 changes: 40 additions & 47 deletions LaserGRBL/GrblEmulator/GrblEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GrblWebSocketEmulator>("/");
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<GrblWebSocketEmulator>("/");
srv.Start();
}
}

public static void Stop()
{
if (srv != null)
{
srv.Stop();
srv = null;
}
}


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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");
}
}
}
Expand Down
55 changes: 32 additions & 23 deletions LaserGRBL/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions LaserGRBL/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Loading

0 comments on commit 926d82e

Please sign in to comment.