dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Program hangs when it runs and I can't figure out why

  1. #1
    Join Date
    Jun 2019
    Posts
    2

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: Program hangs when it runs and I can't figure out why

    [Moved from Visual C++ Programming Forum]
    Victor Nijegorodov

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: Program hangs when it runs and I can't figure out why

    Quote Originally Posted by JHalps30 View Post
    ...
    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

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,764

    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++17 Compiler: Microsoft VS2019 (16.2.2)

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: Program hangs when it runs and I can't figure out why

    Quote Originally Posted by 2kaud View Post
    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

  6. #6
    Join Date
    Jun 2019
    Posts
    2

    Re: Program hangs when it runs and I can't figure out why

    Quote Originally Posted by VictorN View Post
    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.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,423

    Re: Program hangs when it runs and I can't figure out why

    Quote Originally Posted by JHalps30 View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)