我正在开发一种可以与Golang中的3D打印机通信的工具。我遇到的问题困扰我,看来我可以启动与打印机的连接。 LCD屏幕发生变化,代码开始运行,但是每当我尝试从端口读取内容时,我什么也收不到。具体来说,他们应该返回“确定”消息,所以我知道继续下一条消息。另外,当我发送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复印机6。我可以使它与Cura和Octoprint一起使用,因此我可以肯定串行通信有效,但我只是缺少了一些东西。
EDIT2:更令人费解的是,如果我连接了Arduino IDE并将串行波特率设置为250000,它将向控制台吐出垃圾。如果我关闭它并打开Cura,则让它检测打印机,然后关闭Cura并重新打开控制台,看来一切正常。
最佳答案
万一有人偶然发现将golang连接到3D打印机时遇到麻烦,我终于找到了解决方案。事实证明,go标准库中当前的syscall实施不支持非规范的波特率。大多数3D打印机以250K波特运行,并且不支持该速率,因此它将失败。使用实现termios2规范的软件包可以实现异常的比特率并解决该问题。这是我正在使用并且运行良好的当前实现。
https://github.com/distributed/sers