Skip to content

Commit

Permalink
Add write timeout and get error command
Browse files Browse the repository at this point in the history
Signed-off-by: Maartje Eyskens <[email protected]>
  • Loading branch information
Maartje Eyskens committed Mar 3, 2021
1 parent 96d5901 commit 5a0d0ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ const (
// function type B
BarcodeTypeCODE128 BarcodeType = "\x49"
)

type ErrorStatus byte

const (
ErrorNone ErrorStatus = 0x00
ErrorCoverOpen ErrorStatus = 0x04
ErrorPaperOut ErrorStatus = 0x20
ErrorGeneric ErrorStatus = 0x40
)
9 changes: 8 additions & 1 deletion demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ func main() {
return
}

p.Init()
err = p.Init()
if err != nil {
fmt.Print(err)
return
}

p.Smooth(true)
p.Size(2, 2)
p.PrintLn("HELLO GO")
Expand Down Expand Up @@ -42,4 +47,6 @@ func main() {
p.Feed(2)
p.Cut()
p.End()
// do the next piece of work

}
24 changes: 24 additions & 0 deletions epson.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"path"
"strings"
"time"

"github.com/davecgh/go-spew/spew"

"github.com/bjarneh/latinx"
)
Expand All @@ -15,6 +18,7 @@ var ErrorNoDevicesFound = errors.New("No devices found")

type Printer struct {
s io.ReadWriteCloser
f *os.File
}

// NewUSBPrinter returns a new printer with a USb Vendor and Product ID
Expand Down Expand Up @@ -44,10 +48,14 @@ func NewUSBPrinterByPath(devpath string) (*Printer, error) {
}
return &Printer{
s: f,
f: f,
}, nil
}

func (p *Printer) write(cmd string) error {
if p.f != nil {
p.f.SetWriteDeadline(time.Now().Add(10 * time.Second))
}
_, err := p.s.Write([]byte(cmd))
return err
}
Expand Down Expand Up @@ -183,3 +191,19 @@ func (p *Printer) Barcode(barcode string, format BarcodeType) error {

return p.Print(fmt.Sprintf("%s", barcode))
}

func (p *Printer) GetErrorStatus() (ErrorStatus, error) {
_, err := p.s.Write([]byte{0x10, 0x04, 0x02})
if err != nil {
return 0, err
}
data := make([]byte, 1)
_, err = p.s.Read(data)
if err != nil {
return 0, err
}

spew.Dump(data)

return ErrorStatus(data[0]), nil
}

0 comments on commit 5a0d0ff

Please sign in to comment.