.net - 尝试通过串行连接传递文本文件的行时,程序挂起。我究竟做错了什么?
问题描述
我正在尝试通过串行连接将 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
“;”之后的任何内容 是注释,其余的是实际命令。
代码编译没有错误,但它冻结了程序。
解决方案
推荐阅读
- windows - procdump 转储是否显示由于需求分页而从未加载的页面?
- html - 从 Boostrap 3 迁移的 Bootstrap 4 网格布局问题
- asp.net-core-3.1 - 使用依赖注入从 serilog .Net Core Web API 读取 InMemory 日志
- android - 无法在锁屏或应用程序处于后台时显示闹钟播放 ui
- sql - 在 Laravel 中使用 Eloquent 在模型中应用语句
- pandas - 平均熊猫数据框:重新采样,插值?
- hibernate - 以前有人处理过这个错误吗?(org.hibernate.action.internal.EntityActionVetoException:EntityInsertAction 被否决)
- matlab - Matlab中ARIMA-GARCH模型的滚动窗口预测
- mysql - SQL将别名写入永久列?
- sql - Microsoft Access 数据库引擎无法将“[tbl_AssignmentDetails].[Position Name]”识别为有效的字段名称或表达式