Skip to content

Commit

Permalink
fix for #76
Browse files Browse the repository at this point in the history
Fix #76 and add emulator config ability
  • Loading branch information
arkypita committed Aug 21, 2017
1 parent 25d34da commit d9f6e94
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 11 deletions.
37 changes: 30 additions & 7 deletions LaserGRBL/GrblCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,35 @@ public void ExportConfig(string filename)

try
{
Tools.AutoResetTimer WaitResponseTimeout = new Tools.AutoResetTimer(TimeSpan.FromSeconds(10), true);

while (cmd.Status == GrblCommand.CommandStatus.WaitingResponse) //resta in attesa della risposta
;
//resta in attesa dell'invio del comando e della risposta
while (cmd.Status == GrblCommand.CommandStatus.Queued || cmd.Status == GrblCommand.CommandStatus.WaitingResponse)
if (WaitResponseTimeout.Expired)
throw new TimeoutException("No response received from grbl!");
else
System.Threading.Thread.Sleep(10);

if (cmd.Status != GrblCommand.CommandStatus.ResponseGood)
{
System.Windows.Forms.MessageBox.Show("Error exporting config!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
if (cmd.Status == GrblCommand.CommandStatus.ResponseGood)
{
//attendi la ricezione di tutti i parametri
long tStart = Tools.HiResTimer.TotalMilliseconds;
long tLast = tStart;
int counter = mSentPtr.Count;

//finché l'ultima risposta è più recente di 1s e non sono passati più di 10s totali
while (Tools.HiResTimer.TotalMilliseconds - tLast < 1000 && Tools.HiResTimer.TotalMilliseconds - tStart < 10000)
{
if (mSentPtr.Count != counter)
{
tLast = Tools.HiResTimer.TotalMilliseconds;
counter = mSentPtr.Count;
}
else
{
System.Threading.Thread.Sleep(10);
}
}

int msg = 0;
foreach (IGrblRow row in mSentPtr)
Expand All @@ -233,6 +252,10 @@ public void ExportConfig(string filename)
sw.Close();
System.Windows.Forms.MessageBox.Show(String.Format("{0} Config exported with success!", msg), "Success", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
}
else
{
System.Windows.Forms.MessageBox.Show("Error exporting config!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
Expand Down
55 changes: 53 additions & 2 deletions LaserGRBL/GrblEmulator/GrblEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,66 @@ protected override void OnMessage(MessageEventArgs e)
else if (e.Data == "{fb:n}\n")
;
else if (e.Data == "!")
{mPaused = true; SendStatus();}
{ mPaused = true; SendStatus(); }
else if (e.Data == "~")
{ mPaused = false; SendStatus(); }
{ mPaused = false; SendStatus(); }
else if (e.RawData.Length == 1 && e.RawData[0] == 24)
GrblReset();
else if (e.Data == "$$\n")
SendConfig();
else if (IsSetConf(e.Data))
SetConfig(e.Data);
else
EnqueueRX(e);
}

System.Text.RegularExpressions.Regex confRegEX = new System.Text.RegularExpressions.Regex(@"^[$](\d+) *= *(\d+\.?\d*)");
private bool IsSetConf(string p)
{return confRegEX.IsMatch(p);}

private void SetConfig(string p)
{
try
{
System.Text.RegularExpressions.MatchCollection matches = confRegEX.Matches(p);
int key = int.Parse(matches[0].Groups[1].Value);

if (configTable.Keys.Contains(key))
{
if (configTable[key] is int)
configTable[key] = int.Parse(matches[0].Groups[2].Value, System.Globalization.CultureInfo.InvariantCulture);
else if (configTable[key] is decimal)
configTable[key] = decimal.Parse(matches[0].Groups[2].Value, System.Globalization.CultureInfo.InvariantCulture);
else if (configTable[key] is bool)
configTable[key] = int.Parse(matches[0].Groups[2].Value) == 0 ? false : true;

ImmediateTX("ok");
}
else
ImmediateTX("error");
}
catch(Exception ex)
{
ImmediateTX("error");
}
}

private Dictionary<int, object> configTable = new Dictionary<int, object> { { 0, 10 }, { 1, 25 }, { 2, 0 }, { 3, 0 }, { 4, false }, { 5, false }, { 6, false }, { 10, 1 }, { 11, 0.010m }, { 12, 0.002m }, { 13, false }, { 20, false }, { 21, false }, { 22, false }, { 23, 0 }, { 24, 25.000m }, { 25, 500.000m }, { 26, 250 }, { 27, 1.000m }, { 30, 1000.0m }, { 31, 0.0m }, { 32, false }, { 100, 250.000m }, { 101, 250.000m }, { 102, 250.000m }, { 110, 500.000m }, { 111, 500.000m }, { 112, 500.000m }, { 120, 10.000m }, { 121, 10.000m }, { 122, 10.000m }, { 130, 200.000m }, { 131, 200.000m }, { 132, 200.000m } };

private void SendConfig()
{
ImmediateTX("ok");
foreach (KeyValuePair<int, object> kvp in configTable)
{
if (kvp.Value is decimal)
ImmediateTX(string.Format(System.Globalization.CultureInfo.InvariantCulture, "${0}={1:0.000}", kvp.Key, kvp.Value));
else if (kvp.Value is bool)
ImmediateTX(string.Format(System.Globalization.CultureInfo.InvariantCulture, "${0}={1}", kvp.Key, ((bool)kvp.Value) ? 1 : 0));
else
ImmediateTX(string.Format(System.Globalization.CultureInfo.InvariantCulture, "${0}={1}", kvp.Key, kvp.Value));
}
}

private void EnqueueRX(MessageEventArgs e)
{
lock (rxBuf)
Expand Down
1 change: 1 addition & 0 deletions LaserGRBL/LaserGRBL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@
<Compile Include="SplashScreenForm.Designer.cs">
<DependentUpon>SplashScreenForm.cs</DependentUpon>
</Compile>
<Compile Include="Tools\Cronometro.cs" />
<Compile Include="Tools\HiResTimer.cs" />
<Compile Include="Tools\MathHelper.cs" />
<Compile Include="Tools\ThreadClass.cs" />
Expand Down
2 changes: 1 addition & 1 deletion LaserGRBL/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ void MnExportConfigClick(object sender, EventArgs e)

if (filename != null)
{Core.ExportConfig(filename);}

}

void MnImportConfigClick(object sender, EventArgs e)
{
string filename = null;
Expand Down
2 changes: 1 addition & 1 deletion LaserGRBL/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build
// utilizzando l'asterisco (*) come descritto di seguito:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("2.7.4")]
[assembly: AssemblyVersion ("2.7.5")]
[assembly: NeutralResourcesLanguageAttribute("en")]
139 changes: 139 additions & 0 deletions LaserGRBL/Tools/Cronometro.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tools
{
public class TimingBase
{

private static long mStartup;

static TimingBase()
{
mStartup = HiResTimer.TotalNano;
}

public static TimeSpan TimeFromApplicationStartup()
{
// Un singolo Tick rappresenta cento nanosecondi
return TimeSpan.FromTicks((HiResTimer.TotalNano - mStartup) / 100);
}

}

public class OneShootCrono
{
//necessario xè non è sufficiente usare starttime
private bool started = false;

private TimeSpan starttime = TimeSpan.Zero;
public void Start()
{
starttime = TimingBase.TimeFromApplicationStartup();
started = true;
}

public TimeSpan TempoTrascorso
{
get
{
//non inizializzato
if (!started)
{
return TimeSpan.Zero;
}
else
{
return TimingBase.TimeFromApplicationStartup() - starttime;
}
}
}

public virtual void Reset()
{
starttime = TimeSpan.Zero;
started = false;
}

public bool Running
{
get { return started; }
}

}

public class Cronometro : OneShootCrono
{


private System.Collections.Generic.List<TimeSpan> times = new System.Collections.Generic.List<TimeSpan>();
public void SalvaIntermedio()
{
times.Add(TempoTrascorso);
}

public override void Reset()
{
base.Reset();
times.Clear();
}

public System.Collections.Generic.List<TimeSpan> Intermedi
{
get { return times; }
}

}

public class AutoResetTimer
{
private TimeSpan m_period;
private OneShootCrono crono;

private long m_periodcount;
public AutoResetTimer(TimeSpan Period, bool start = false)
{
m_period = Period;
crono = new OneShootCrono();

if (start)
Start();
}

public void Start()
{
crono.Start();
}

public bool Running
{
get { return crono.Running; }
}

public TimeSpan Period
{
get { return m_period; }
}

public bool Expired
{
get
{
bool rv = false;
long newperiod = crono.TempoTrascorso.Ticks / Period.Ticks;
rv = newperiod > m_periodcount;
m_periodcount = newperiod;
return rv;
}
}

public long NumPeriod
{
get { return m_periodcount; }
}

}

}

0 comments on commit d9f6e94

Please sign in to comment.