开源改变世界

可能的发件人错误? #476

推推 grbl 3年前 (2023-02-03) 194次浏览
关闭
MitchBradley 打开了这个问题 2019 年 5 月 11 日 · 2 条评论
关闭

可能的发件人错误?#476

MitchBradley 打开了这个问题 2019 年 5 月 11 日 · 2 条评论

评论

可能的发件人错误? #476

我一直在尝试详细了解 Sender 和 Workflow 逻辑,并且发现了我认为可能是错误的地方。

在 Sender.js 中,我们有这样的代码:

                   // The newline character (\n) consumed the RX buffer space
                   if ((sp.line.length > 0) && ((sp.dataLength + sp.line.length + 1) >= sp.bufferSize)) { 
                        break;
                  }

如果 sp.line.length == 0,则跳过测试的其余部分,并继续发送。这对我来说似乎不正确。消耗 RX 缓冲区空间的空行加换行符在我看来与非空行加上消耗它的换行符一样。如果该解释是正确的,(sp.line.length > 0) &&则应删除该部分。

可能的发件人错误? #476
合作者
奇顿 评论了 2019 年 5 月 11 日  

下面是对代码的详细解释:

加载阶段

加载 gcode 时将过滤掉所有空行,该filter()方法返回一个包含非空行的新数组:

const lines = gcode.split('\n')
    .filter(line => (line.trim().length > 0));

this.state.lines = lines; // non-empty lines without "\n"

处理阶段

如果sp.line是空字符串(''),则返回指定索引(即lines[sent])处的字符串,并调用trim()去除字符串两端空格的方法:

sp.line = sp.line || this.state.lines[this.state.sent].trim();

dataFilter 用于翻译一行中的表达式,在返回新行之前将删除 gcode 注释。请注意,转换后的过滤结果可能是一个空字符串。

if (this.dataFilter) {
    sp.line = this.dataFilter(sp.line, this.state.context) || '';
}

实际上,这(sp.line.length > 0) &&部分不应该从下面的表达式中删除,因为它在计算可用缓冲区长度时只关心非空字符串:

// The newline character (\n) consumed the RX buffer space
if ((sp.line.length > 0) && ((sp.dataLength + (sp.line.length + 1)) >= sp.bufferSize)) {
    break;
}

空行不会发出data将数据写入控制器的事件,并且这些空行将由发送方本身确认以增加received计数:

this.state.sent++;
this.emit('change');

if (sp.line.length === 0) {
    this.ack(); // ack empty line
    continue;
}

const line = sp.line + '\n';
sp.line = '';
sp.dataLength += line.length;
sp.queue.push(line.length);
this.emit('data', line, this.state.context);
可能的发件人错误? #476
贡献者作者

明白了,谢谢。