首页 > 解决方案 > 尝试通过串行连接传递文本文件的行时,程序挂起。我究竟做错了什么?

问题描述

我正在尝试通过串行连接将 gcode 命令文件发送到 3D 打印机。打印机的缓冲区空间很小,所以我必须一次通过一行。当我运行我的程序时,它似乎没有传递或执行任何命令。

在写完第一行之后,我尝试等待打印机的响应,但我无法让它工作,所以我决定读取缓冲区中的空间以确定它是否有足够的空间来执行另一个命令。

    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";
    }

gcode(美化文本)文件的前几行如下所示:

; 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

“;”之后的任何内容 是注释,其余的是实际命令。

代码编译没有错误,但它冻结了程序。

标签: .netvisual-c++serial-portc++-clitext-files

解决方案


推荐阅读