I'm using a printer (ESC POS) that prints text when data is written on /dev/usb/lp0
The simplified code is:
package main
import (
"bufio"
"os"
)
func main() {
f, err := os.Create("/dev/usb/lp0")
if err != nil {
panic(err)
}
defer f.Close()
w := bufio.NewWriter(f)
w.Write([]byte("\x1B@"))
w.WriteString("test-1
")
w.WriteString("test-2
")
w.WriteString("test-3
")
w.WriteString("test-4
")
w.WriteString("test-5
")
w.WriteString("test-6
")
w.WriteString("test-7
")
w.WriteString("test-8
")
w.WriteString("test-9
")
w.Flush()
f.Sync()
}
and most of the time the last line is not printed, unless I add a timer at the very end.
I think I closed everything properly, so not sure what's going on here... If I write to a file, it works fine BTW, luck ?
Thank you in advance, I spend last evening on this and it is driving me crazy :)
EDIT: Code with error handling (f.Sync() actually failed and I removed it)
package main
import (
"bufio"
"os"
)
func main() {
f, err := os.Create("/dev/usb/lp0")
if err != nil {
panic(err)
}
defer f.Close()
w := bufio.NewWriter(f)
_, err = w.Write([]byte("\x1B@"))
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-1
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-2
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-3
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-4
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-5
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-6
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-7
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-8
")
if err != nil {
panic(err)
}
_, err = w.WriteString("toto-9
")
if err != nil {
panic(err)
}
err = w.Flush()
if err != nil {
panic(err)
}
}