陶晶驰串口屏上如何获取grbl的返回信息 并将x y z a b c各轴坐标显示在对应文本框中编程脚本
时间: 2023-11-22 14:56:35 浏览: 122
要在串口屏上获取 GRBL 的返回信息并将各轴坐标显示在对应的文本框中,您需要在控制器(如 Arduino)上运行一个程序来与 GRBL 进行通信,并将返回的信息传输到串口屏幕上。
以下是一个示例程序,它可以在 Arduino 上运行,并将 GRBL 发送的信息显示在串口屏幕上:
```
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
}
void loop() {
if (mySerial.available()) {
Serial.write(mySerial.read());
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
```
该程序使用 SoftwareSerial 库创建一个串口对象,以便将 GRBL 发送的信息传输到串口屏幕上。在 setup 函数中,程序初始化了两个串口的波特率,并在 loop 函数中不断地从两个串口中读取数据,并将其传输到另一个串口中。
接下来,您需要在程序中添加代码,以便将 GRBL 返回的信息解析为坐标,并将其显示在对应的文本框中。下面是一个示例程序,它可以实现这个功能:
```
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
String axisX, axisY, axisZ, axisA, axisB, axisC;
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
// 设置串口屏幕文本框的初始值
mySerial.print("t0.txt=\"X:0\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t1.txt=\"Y:0\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t2.txt=\"Z:0\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t3.txt=\"A:0\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t4.txt=\"B:0\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t5.txt=\"C:0\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
}
void loop() {
if (mySerial.available()) {
char c = mySerial.read();
if (c == '<') {
axisX = "";
axisY = "";
axisZ = "";
axisA = "";
axisB = "";
axisC = "";
} else if (c == ',') {
axisX.trim();
axisY.trim();
axisZ.trim();
axisA.trim();
axisB.trim();
axisC.trim();
mySerial.print("t0.txt=\"X:");
mySerial.print(axisX);
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t1.txt=\"Y:");
mySerial.print(axisY);
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t2.txt=\"Z:");
mySerial.print(axisZ);
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t3.txt=\"A:");
mySerial.print(axisA);
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t4.txt=\"B:");
mySerial.print(axisB);
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
mySerial.print("t5.txt=\"C:");
mySerial.print(axisC);
mySerial.print("\"");
mySerial.write(0xff);
mySerial.write(0xff);
mySerial.write(0xff);
delay(100);
} else {
if (c == 'X') {
axisX = "";
} else if (c == 'Y') {
axisY = "";
} else if (c == 'Z') {
axisZ = "";
} else if (c == 'A') {
axisA = "";
} else if (c == 'B') {
axisB = "";
} else if (c == 'C') {
axisC = "";
} else {
if (axisX.length() < 10) {
axisX += c;
} else if (axisY.length() < 10) {
axisY += c;
} else if (axisZ.length() < 10) {
axisZ += c;
} else if (axisA.length() < 10) {
axisA += c;
} else if (axisB.length() < 10) {
axisB += c;
} else if (axisC.length() < 10) {
axisC += c;
}
}
}
Serial.write(c);
}
if (Serial.available()) {
mySerial.write(Serial.read());
}
}
```
该程序在设置初始文本框值后,使用一个状态机来解析 GRBL 返回的信息。当程序接收到 '<' 字符时,它会清空所有轴的值;当程序接收到 ',' 字符时,它会解析所有轴的值,并将其显示在对应的文本框中。在解析过程中,程序将每个轴的值存储在一个字符串变量中,并使用 trim() 函数去掉前导和尾部空格。
请注意,这是一个简单的示例程序,您需要根据自己的需求进行修改和优化。例如,您可能需要添加错误处理代码,以便在 GRBL 返回错误消息时将其显示在串口屏幕上。
阅读全文