-
June 20th, 2019, 11:25 AM
#1
Program hangs when it runs and I can't figure out why
Hey guys,
I am trying to send a file of gcode commands to a 3D printer via serial connection. The printer has very little buffer space so I have to pass one line at a time. When I run my program it does not seem to pass or execute any of the commands.
I have tried waiting for a response from the printer after writing the first line but I couldn't get that to work, so I decided to read the space in the buffer to determine if it had enough space for another command.
Code:
String^ gcodeFile = "C:\\LUPrint\\profiles\\graphene\\temp.gcode";
try
{
// Open file for reading
StreamReader^ dataIn = File::OpenText(gcodeFile);
String^ gcodeCommand;
// Set variable equal to the number of bytes in buffer
int bytesAvailable = serialPort->BytesToRead;
while ((gcodeCommand = dataIn->ReadLine()) != nullptr)
{
// Holds while buffer is more than half full
while (serialPort->BytesToRead >= (bytesAvailable / 2))
{
// Do nothing
}
// Only write line if line isnt empty;
if (!String::IsNullOrEmpty(gcodeCommand))
{
this->serialPort->WriteLine(gcodeCommand);
}
}
}
catch (Exception^ e)
{
if (dynamic_cast<FileNotFoundException^> (e))
this->textBox1->Text = "File not found";
else
this->textBox1->Text = "Problem reading file";
}
The first few lines of the gcode (glorified text) file looks like this:
; generated by Slic3r 1.3.0 on 2019-06-20 at 09:36:32
; external perimeters extrusion width = 0.38mm (0.00mm^3/s)
; perimeters extrusion width = 0.49mm (0.00mm^3/s)
; infill extrusion width = 0.44mm (0.00mm^3/s)
; solid infill extrusion width = 0.49mm (0.00mm^3/s)
; top infill extrusion width = 0.49mm (0.00mm^3/s)
; support material extrusion width = 0.38mm (0.00mm^3/s)
M107
M190 S50 ; set bed temperature and wait for it to be reached
M104 S175 ; set temperature
G28 ; home all axes
G1 Z5 F5000 ; lift nozzle
; Filament gcode
M109 S175 ; set temperature and wait for it to be reached
G21 ; set units to millimeters
G90 ; use absolute coordinates
M82 ; use absolute distances for extrusion
G92 E0
G1 Z0.150 F7800.000
G1 E-2.00000 F2400.00000
G92 E0
G1 X85.206 Y83.418 F7800.000
G1 E2.00000 F2400.00000
G1 F1800
G1 X87.472 Y82.260 E2.04249
G1 X90.000 Y81.858 E2.08525
G1 X110.000 Y81.858 E2.41928
Anything after a ";" is a comment, the rest are the actual commands.
The code compiles with no errors but it freezes the program.
Any help would be much appreciated.
Last edited by VictorN; June 20th, 2019 at 11:48 AM.
Reason: Added CODE tags
-
June 20th, 2019, 11:50 AM
#2
Re: Program hangs when it runs and I can't figure out why
[Moved from Visual C++ Programming Forum]
Victor Nijegorodov
-
June 20th, 2019, 11:53 AM
#3
Re: Program hangs when it runs and I can't figure out why
Originally Posted by JHalps30
...
The code compiles with no errors but it freezes the program.
Any help would be much appreciated.
Did you try to debug your code?
just set the breakpoints inside your while loops, start debugger and see what happens...
Victor Nijegorodov
-
June 20th, 2019, 01:24 PM
#4
Re: Program hangs when it runs and I can't figure out why
Code:
while (serialPort->BytesToRead >= (bytesAvailable / 2))
{
// Do nothing
}
What causes the value of serialPort->BytesToRead to change during this while loop?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 20th, 2019, 01:33 PM
#5
Re: Program hangs when it runs and I can't figure out why
Originally Posted by 2kaud
Code:
while (serialPort->BytesToRead >= (bytesAvailable / 2))
{
// Do nothing
}
What causes the value of serialPort->BytesToRead to change during this while loop?
Good point!
However, I hoped OP would be able to investigate this problem him-/her-self...
Victor Nijegorodov
-
June 20th, 2019, 01:40 PM
#6
Re: Program hangs when it runs and I can't figure out why
Originally Posted by VictorN
Good point!
However, I hoped OP would be able to investigate this problem him-/her-self...
Yeah I realized that I meant to call BytesToWrite instead of BytesToRead. Once I switched that around the program started working.
Thanks for your help.
-
June 20th, 2019, 03:17 PM
#7
Re: Program hangs when it runs and I can't figure out why
Originally Posted by JHalps30
Yeah I realized that I meant to call BytesToWrite instead of BytesToRead. Once I switched that around the program started working.
Thanks for your help.
You are welcome!
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|