Skip to content

Commit

Permalink
Improve fix/workaround for CH340 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Settimi committed Dec 6, 2024
1 parent 6a8895e commit 78dd682
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions LaserGRBL/Core/GrblCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,9 @@ public void OpenCom()
{
try
{
FixCH340 = false;
FixCH340_exception = FixCH340_goodread = 0;

mAutoBufferSize = DEFAULT_BUFFER_SIZE; //reset to default buffer size
SetStatus(MacStatus.Connecting);
connectStart = Tools.HiResTimer.TotalMilliseconds;
Expand Down Expand Up @@ -2746,11 +2749,14 @@ protected void ManageCommandResponse(string rline)
}
}

int good, ioex;

bool FixCH340 = false;
UInt32 FixCH340_goodread = 0;
UInt32 FixCH340_exception = 0;
protected static char[] trimarray = new char[] { '\r', '\n', ' ' };
private string WaitComLineOrDisconnect()
{
if (!com.IsOpen || !HasIncomingData()) return null;
if (FixCH340 && (!com.IsOpen || !HasIncomingData())) return null;

string rv = null;

Expand All @@ -2764,13 +2770,14 @@ private string WaitComLineOrDisconnect()
tmp = tmp.TrimEnd(trimarray); //rimuovi ritorno a capo
tmp = tmp.Trim(); //rimuovi spazi iniziali e finali
if (tmp.Length > 0) rv = tmp;
good++;
FixCH340_goodread++;
}
}
catch (System.IO.IOException ex)
{
ioex++;
if (ioex % 10 == 0) System.Threading.Thread.Sleep(1);
FixCH340 = true; //self fix for CH340, use HasIncomingData to check if there is data, this reduce the number of exceptions
FixCH340_exception++;
if (FixCH340_exception % 10 == 0) System.Threading.Thread.Sleep(1);
}
catch
{
Expand Down

0 comments on commit 78dd682

Please sign in to comment.