我正在努力构建一个可以与 Golang 中的 3D 打印机通信的工具。我遇到的问题困扰着我,似乎我可以启动与打印机的连接。LCD 屏幕发生变化,代码开始运行,但每当我尝试从端口读取数据时,我都没有得到任何返回。具体来说,他们应该返回“ok”消息,所以我知道继续下一条消息。此外,当我发送 Gcode 字符串时,它似乎永远无法理解它们,据我所知,它们只是被丢弃了。这是我正在使用的示例代码:
package printer
import (
"errors"
"fmt"
"log"
"strings"
"time"
"io/ioutil"
"path/filepath"
"github.com/tarm/serial"
)
//Printer is a connection to a reprap or similar printer
type Printer struct {
s *serial.Port
}
//Connect creates the printer struct and returns it after initing it
func Connect(port string, speed int64) Printer {
devices, err := getSerialDevices()
if err != nil {
log.Println(err)
}
for _, value := range devices {
log.Println("Found device: ", value)
}
c := &serial.Config{Name: devices[0], Baud: 115200, ReadTimeout: time.Second * 5}
s, err := serial.OpenPort(c)
if err != nil {
log.Println("Failed to open communication")
log.Fatal(err)
}
p := Printer{}
p.s = s
p.readPump()
p.readPump()
return p
}
func (p *Printer) readPump() string {
output := ""
oldLength := -1
for {
buf := make([]byte, 128)
/*
This appears to be where part of the problem is,
I never get anything back from the printer. Per the
tarm documentation since I'm in non-blocking mode
I don't capture the error because it's an EOF
*/
n, _:= p.s.Read(buf)
/*if err != nil {
log.Println("Failed to readPump")
log.Fatal(err)
}*/
output += fmt.Sprintf("%s", buf[:n])
if len(output) == oldLength {
return output
}
oldLength = len(output)
}
}
//SendCommand sends a single GCODE command to the printer
func (p *Printer) SendCommand(g string) error {
g = g + "\n"
_, err := p.s.Write([]byte(g))
if err != nil {
log.Println("Failed to write: ", g)
log.Fatal(err)
}
if !strings.HasSuffix(p.readPump(), "ok\n") {
log.Println(errors.New("command did not complete successfully"))
}
return nil
}
//Retrieve the absolute path for serial devices
func getSerialDevices() ([]string, error) {
log.Println("getting serial devices")
devices, err := ioutil.ReadDir("/dev/serial/by-id")
if err != nil {
log.Println(err)
return nil, err
}
deviceList := make([]string, len(devices))
for index, deviceLink := range devices {
//log.Println("Found device: ", deviceLink.Name())
abs, err := filepath.EvalSymlinks("/dev/serial/by-id/" + deviceLink.Name())
//log.Print("Absolute Device: ")
//log.Println(abs)
deviceList[index] = abs
if err != nil {
log.Println(err)
return nil, err
}
}
return deviceList, nil
}
这是我用来调用打印机的代码:
package main
import (
"fmt"
"log"
"github.com/david-fry/go-3dprinter"
)
func main() {
fmt.Println("Connecting to Printer...")
p := printer.Connect("COM3", 115200)
fmt.Println("Moving Extruder...")
err := p.SendCommand("G28")
err = p.SendCommand("G1 Z20")
err = p.SendCommand("G1 Y100")
err = p.SendCommand("G1 X100")
if err != nil {
log.Fatal(err)
}
fmt.Println("Heating Extruder...")
err = p.SendCommand("M109 S100")
if err != nil {
log.Fatal(err)
}
fmt.Println("Cooling Extruder...")
err = p.SendCommand("M109 S100")
if err != nil {
log.Fatal(err)
}
}
编辑:这是一个 Wanhao Duplicator 6。我可以让它与 Cura 和 Octoprint 一起工作,所以我很确定串行通信工作正常,我只是错过了一些东西。
EDIT2:更令人费解的是,如果我连接 Arduino IDE 并将串行波特率设置为 250000,它会向控制台吐出垃圾。如果我关闭它并打开 Cura,让它检测打印机,然后关闭 Cura 并重新打开控制台,它似乎工作正常。