Skip to content

Commit 2cb8d52

Browse files
committed
allow printer connection to take a stream to print
1 parent 50fbc8b commit 2cb8d52

File tree

7 files changed

+98
-24
lines changed

7 files changed

+98
-24
lines changed

CSharpSerialPortWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CSharpSerialPortWrapper : IFrostedSerialPort
3636
{
3737
private SerialPort port;
3838

39-
internal CSharpSerialPortWrapper(string serialPortName)
39+
public CSharpSerialPortWrapper(string serialPortName)
4040
{
4141
if (FrostedSerialPortFactory.GetAppropriateFactory("RepRap").IsWindows)
4242
{

MatterControl.Printing/GCode/GCodeFile.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,12 @@ public static bool IsLayerChange(string lineString)
111111
|| lineString.StartsWith("; layer ");
112112
}
113113

114-
public static bool FileTooBigToLoad(string fileName)
114+
public static bool FileTooBigToLoad(Stream fileStream)
115115
{
116-
if (File.Exists(fileName)
117-
&& Is32Bit)
116+
if (Is32Bit)
118117
{
119-
FileInfo info = new FileInfo(fileName);
120118
// Let's make sure we can load a file this big
121-
if (info.Length > Max32BitFileSize)
119+
if (fileStream.Length > Max32BitFileSize)
122120
{
123121
// It is too big to load
124122
return true;
@@ -172,20 +170,20 @@ public static bool GetFirstStringAfter(string stringToCheckAfter, string fullStr
172170
return false;
173171
}
174172

175-
public static GCodeFile Load(string fileName,
173+
public static GCodeFile Load(Stream fileStream,
176174
Vector4 maxAccelerationMmPerS2,
177175
Vector4 maxVelocityMmPerS,
178176
Vector4 velocitySameAsStopMmPerS,
179177
Vector4 speedMultiplier,
180178
CancellationToken cancellationToken)
181179
{
182-
if (FileTooBigToLoad(fileName))
180+
if (FileTooBigToLoad(fileStream))
183181
{
184-
return new GCodeFileStreamed(fileName);
182+
return new GCodeFileStreamed(fileStream);
185183
}
186184
else
187185
{
188-
return GCodeMemoryFile.Load(fileName,
186+
return GCodeMemoryFile.Load(fileStream,
189187
maxAccelerationMmPerS2,
190188
maxVelocityMmPerS,
191189
velocitySameAsStopMmPerS,

MatterControlLib/Library/Export/GCodeExport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ private void ApplyStreamPipelineAndExport(string gcodeFilename, string outputPat
356356
this.ApplyStreamPipelineAndExport(
357357
new GCodeFileStream(
358358
GCodeFile.Load(
359-
gcodeFilename,
359+
new StreamReader(gcodeFilename).BaseStream,
360360
new Vector4(maxAcceleration, maxAcceleration, maxAcceleration, maxAcceleration),
361361
new Vector4(maxVelocity, maxVelocity, maxVelocity, maxVelocity),
362362
new Vector4(jerkVelocity, jerkVelocity, jerkVelocity, jerkVelocity),

MatterControlLib/PrinterCommunication/Io/GCodeSwitcher.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The views and conclusions contained in the software and documentation are those
2828
*/
2929

3030
using System.Collections.Generic;
31+
using System.IO;
3132
using System.Threading;
3233
using System.Threading.Tasks;
3334
using MatterControl.Printing;
@@ -44,7 +45,7 @@ public class GCodeSwitcher : GCodeStream
4445

4546
private string lastLine = "";
4647

47-
public GCodeSwitcher(string gcodeFilename, PrinterConfig printer, int startLine = 0)
48+
public GCodeSwitcher(Stream gcodeStream, PrinterConfig printer, int startLine = 0)
4849
: base(printer)
4950
{
5051
var settings = this.printer.Settings;
@@ -53,7 +54,7 @@ public GCodeSwitcher(string gcodeFilename, PrinterConfig printer, int startLine
5354
var jerkVelocity = settings.GetValue<double>(SettingsKey.jerk_velocity);
5455
var multiplier = settings.GetValue<double>(SettingsKey.print_time_estimate_multiplier) / 100.0;
5556

56-
var fileStreaming = GCodeFile.Load(gcodeFilename,
57+
var fileStreaming = GCodeFile.Load(gcodeStream,
5758
new Vector4(maxAcceleration, maxAcceleration, maxAcceleration, maxAcceleration),
5859
new Vector4(maxVelocity, maxVelocity, maxVelocity, maxVelocity),
5960
new Vector4(jerkVelocity, jerkVelocity, jerkVelocity, jerkVelocity),
@@ -164,7 +165,7 @@ public void SwitchTo(string gcodeFilename)
164165
var jerkVelocity = settings.GetValue<double>(SettingsKey.jerk_velocity);
165166
var multiplier = settings.GetValue<double>(SettingsKey.print_time_estimate_multiplier) / 100.0;
166167

167-
var switchToGCode = GCodeFile.Load(gcodeFilename,
168+
var switchToGCode = GCodeFile.Load(new StreamReader(gcodeFilename).BaseStream,
168169
new Vector4(maxAcceleration, maxAcceleration, maxAcceleration, maxAcceleration),
169170
new Vector4(maxVelocity, maxVelocity, maxVelocity, maxVelocity),
170171
new Vector4(jerkVelocity, jerkVelocity, jerkVelocity, jerkVelocity),

MatterControlLib/PrinterCommunication/PrinterConnection.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,6 +1926,12 @@ public void SetTargetHotendTemperature(int hotendIndex0Based, double temperature
19261926
private CancellationTokenSource printingCancellation;
19271927

19281928
public async Task StartPrint(string gcodeFilename, PrintTask printTaskToUse = null, bool allowRecovery = true)
1929+
{
1930+
var gcodeStream = new StreamReader(gcodeFilename);
1931+
await StartPrint(gcodeStream.BaseStream, gcodeFilename, printTaskToUse, allowRecovery);
1932+
}
1933+
1934+
public async Task StartPrint(Stream gcodeStream, string gcodeFileNameForTask = null, PrintTask printTaskToUse = null, bool allowRecovery = true)
19291935
{
19301936
if (!this.IsConnected || Printing)
19311937
{
@@ -1946,7 +1952,7 @@ public async Task StartPrint(string gcodeFilename, PrintTask printTaskToUse = nu
19461952
await Task.Run(() =>
19471953
{
19481954
// LoadGCodeToPrint
1949-
CreateStreamProcessors(gcodeFilename, this.RecoveryIsEnabled);
1955+
CreateStreamProcessors(gcodeStream, this.RecoveryIsEnabled);
19501956
});
19511957

19521958
// DoneLoadingGCodeToPrint
@@ -1968,7 +1974,8 @@ await Task.Run(() =>
19681974
activePrintItem.PrintItem.Commit();
19691975
}
19701976

1971-
if (activePrintTask == null
1977+
if (gcodeFileNameForTask !=null
1978+
&& activePrintTask == null
19721979
&& allowRecovery)
19731980
{
19741981
// TODO: Fix printerItemID int requirement
@@ -1978,7 +1985,7 @@ await Task.Run(() =>
19781985
PrinterId = this.Printer.Settings.ID.GetHashCode(),
19791986
PrintName = activePrintItem.PrintItem.Name,
19801987
PrintItemId = activePrintItem.PrintItem.Id,
1981-
PrintingGCodeFileName = gcodeFilename,
1988+
PrintingGCodeFileName = gcodeFileNameForTask,
19821989
PrintComplete = false
19831990
};
19841991

@@ -2171,7 +2178,7 @@ private void KeepTrackOfAbsolutePositionAndDestination(string lineBeingSent)
21712178
}
21722179
}
21732180

2174-
private void CreateStreamProcessors(string gcodeFilename, bool recoveryEnabled)
2181+
private void CreateStreamProcessors(Stream gcodeStream, bool recoveryEnabled)
21752182
{
21762183
secondsSinceUpdateHistory = 0;
21772184
lineSinceUpdateHistory = 0;
@@ -2181,9 +2188,9 @@ private void CreateStreamProcessors(string gcodeFilename, bool recoveryEnabled)
21812188

21822189
GCodeStream accumulatedStream = null;
21832190

2184-
if (gcodeFilename != null)
2191+
if (gcodeStream != null)
21852192
{
2186-
gCodeFileSwitcher = new GCodeSwitcher(gcodeFilename, Printer);
2193+
gCodeFileSwitcher = new GCodeSwitcher(gcodeStream, Printer);
21872194

21882195
if (this.RecoveryIsEnabled
21892196
&& activePrintTask != null) // We are resuming a failed print (do lots of interesting stuff).
@@ -2220,7 +2227,7 @@ private void CreateStreamProcessors(string gcodeFilename, bool recoveryEnabled)
22202227

22212228
accumulatedStream = new BabyStepsStream(Printer, accumulatedStream);
22222229

2223-
bool enableLineSpliting = gcodeFilename != null && Printer.Settings.GetValue<bool>(SettingsKey.enable_line_splitting);
2230+
bool enableLineSpliting = gcodeStream != null && Printer.Settings.GetValue<bool>(SettingsKey.enable_line_splitting);
22242231
accumulatedStream = maxLengthStream = new MaxLengthStream(Printer, accumulatedStream, enableLineSpliting ? 1 : 2000);
22252232

22262233
accumulatedStream = printLevelingStream = new PrintLevelingStream(Printer, accumulatedStream, true);

Tests/MatterControl.Tests/MatterControl.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
<Project>{2af30557-fc50-4de3-ad1c-7eb57131a9c5}</Project>
8585
<Name>MatterControl.Common</Name>
8686
</ProjectReference>
87+
<ProjectReference Include="..\..\MatterControl.csproj">
88+
<Project>{b2b001ee-a142-4e20-acf8-ae4a9cb984f8}</Project>
89+
<Name>MatterControl</Name>
90+
</ProjectReference>
8791
<ProjectReference Include="..\..\MatterControl.Printing\MatterControl.Printing.csproj">
8892
<Project>{97d5ade3-c1b4-4b46-8a3e-718a4f7f079f}</Project>
8993
<Name>MatterControl.Printing</Name>

Tests/MatterControl.Tests/MatterControl/GCodeStreamTests.cs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@ The views and conclusions contained in the software and documentation are those
2727
either expressed or implied, of the FreeBSD Project.
2828
*/
2929

30+
using System;
3031
using System.Collections.Generic;
3132
using System.Diagnostics;
33+
using System.IO;
34+
using System.Text;
35+
using System.Threading;
36+
using System.Threading.Tasks;
3237
using MatterControl.Printing;
3338
using MatterHackers.Agg;
3439
using MatterHackers.Agg.Platform;
@@ -37,6 +42,8 @@ The views and conclusions contained in the software and documentation are those
3742
using MatterHackers.MatterControl.PrinterCommunication.Io;
3843
using MatterHackers.MatterControl.SlicerConfiguration;
3944
using MatterHackers.MatterControl.Tests.Automation;
45+
using MatterHackers.PrinterEmulator;
46+
using MatterHackers.SerialPortCommunication.FrostedSerial;
4047
using MatterHackers.VectorMath;
4148
using NUnit.Framework;
4249

@@ -610,7 +617,7 @@ public void ToolChangeNoHeatNoExtrusion()
610617
}
611618

612619
[Test, Category("GCodeStream")]
613-
public void ToolChangeHeatNoExtrusion()
620+
public Task ToolChangeHeatNoExtrusion()
614621
{
615622
string[] inputLines = new string[]
616623
{
@@ -663,8 +670,9 @@ public void ToolChangeHeatNoExtrusion()
663670

664671
PrinterConfig printer = SetupToolChangeSettings();
665672

666-
var testStream = GCodeExport.GetExportStream(printer, new TestGCodeStream(printer, inputLines), true);
667-
ValidateStreamResponse(expected, testStream);
673+
ValidateStreamResponseWhilePrintingAsync(expected, printer, inputLines).GetAwaiter().GetResult();
674+
675+
return Task.CompletedTask;
668676
}
669677

670678
private static PrinterConfig SetupToolChangeSettings()
@@ -739,6 +747,62 @@ private static void ValidateStreamResponse(string[] expected, GCodeStream testSt
739747
}
740748
}
741749

750+
private static async System.Threading.Tasks.Task ValidateStreamResponseWhilePrintingAsync(string[] expected, PrinterConfig printer, string[] inputGCode)
751+
{
752+
// make sure we are not getting feedback we don't have in the expected results
753+
printer.Connection.MonitorPrinterTemperature = false;
754+
755+
// set up our serial port finding
756+
FrostedSerialPortFactory.GetPlatformSerialPort = (serialPortName) =>
757+
{
758+
return new Emulator();
759+
};
760+
761+
int expectedIndex = 0;
762+
763+
// register to listen to the printer responses
764+
printer.Connection.LineSent += (s, actualLine) =>
765+
{
766+
if (printer.Connection.Printing)
767+
{
768+
string expectedLine = expected[expectedIndex++];
769+
Assert.AreEqual(expectedLine, actualLine, "Unexpected response from testStream");
770+
}
771+
};
772+
773+
// set up the emulator
774+
printer.Settings.SetValue($"{Environment.MachineName}_com_port", "Emulator");
775+
// connect to the emulator
776+
printer.Connection.Connect();
777+
var time = Stopwatch.StartNew();
778+
while (!printer.Connection.IsConnected
779+
&& time.ElapsedMilliseconds < (1000 * 60 * 3))
780+
{
781+
Thread.Sleep(1000);
782+
}
783+
784+
// start a print
785+
var inputStream = new MemoryStream(Encoding.ASCII.GetBytes(string.Join("\n", inputGCode)));
786+
await printer.Connection.StartPrint(inputStream);
787+
788+
// wait for the print to finish (or 3 minutes to pass)
789+
time = Stopwatch.StartNew();
790+
while(//printer.Connection.Printing
791+
//&&
792+
time.ElapsedMilliseconds < (1000 * 60 * 3))
793+
{
794+
Thread.Sleep(1000);
795+
//printer.Connection.upda
796+
}
797+
798+
Assert.AreEqual(expectedIndex, expected.Length, "We should have seen all the expected lines");
799+
}
800+
801+
private static void Connection_LineReceived(object sender, string e)
802+
{
803+
throw new System.NotImplementedException();
804+
}
805+
742806
[Test, Category("GCodeStream")]
743807
public void WriteReplaceStreamTests()
744808
{

0 commit comments

Comments
 (0)