GoOn-GoOff (gongoff) is a go implementation of the Epson Xon-Xoff protocol.
It's used to communicate with fiscal printers that support the Xon-Xoff protocol.
Supported connections: serial and network
Supported platforms: Windows, Linux, macOS, Android (only network), iOS (only network)
- Create a Printer object and call Open()
- Create the Documents or Commands objects that fit your needs
- Use the functions PrintCommands([]Command) or PrintDocument(Document) to execute the commands
Documents are a set of commands commonly sent to a printer together.
Supported documents are:
- DocumentCommercial (fiscal receipt)
- DocumentManagement
- DocumentCommercialReturn
- DocumentCommercialCancellation
- DocumentPOSReturn
- DocumentPOSCancellation
- DocumentInvoice
- DocumentCommercialWithInvoice
Commands are a less abstract way to use the library and allow to use all the features of Xon-Xoff.
There are a handful of commands with predefined implementations. All the other commands can be created using a GenericCommand with the appropriate parameters and terminator.
// Create a SerialPrinter object and open the serial port COM3.
printer := gongoff.NewSerialPrinter("COM3")
err := printer.Open()
if err != nil {
panic(err)
}
defer printer.Close()
// Create a management document.
doc := gongoff.NewDocumentManagement([]string{"test", "test2", "test3"})
// Print it.
err = printer.PrintDocument(doc)
if err != nil {
panic(err)
}
// Create a NetworkPrinter object and open the connection.
printer := gongoff.NewNetworkPrinter("192.168.1.100", 9100)
err := printer.Open()
if err != nil {
panic(err)
}
defer printer.Close()
// Create a receipt document.
// In this example, the document is a 7,50 euro receipt for a product called "Bread" paid with cash.
testProduct := "BREAD"
commandProduct := gongoff.NewCommandProduct(750, &testProduct, nil, nil)
commandPayment, err := gongoff.NewCommandPayment(gongoff.TerminatorTypePaymentCash, nil, nil)
if err != nil {
panic(err)
}
commercialDoc := gongoff.NewDocumentCommercial(
[]gongoff.CommandProduct{
*commandProduct,
},
[]gongoff.CommandPayment{
*commandPayment,
},
nil,
nil,
nil,
nil,
)
// Print it.
err = printer.PrintDocument(commercialDoc)
if err != nil {
panic(err)
}
// Suppose the printer object is already created and opened.
// Since there is no document that fits our needs, we use an array of commands.
commandFirstLine := gongoff.NewCommandDisplayMessage("First line", 1)
commandSecondLine := gongoff.NewCommandDisplayMessage("Second line", 2)
// Print the commands.
err := printer.PrintCommands([]gongoff.Command{commandFirstLine, commandSecondLine})
if err != nil {
panic(err)
}
// Suppose the printer object is already created and opened.
// Since there is neither a document nor a command that fits our needs, we generate a generic command with the right terminator.
var commands []gongoff.Command
terminator := gongoff.NewTerminator(nil, gongoff.TerminatorTypeFinancialReportZeroing)
commands = append(commands,
gongoff.NewCommandGeneric(
[]gongoff.Data{},
*terminator,
),
)
// Print the commands.
err := printer.PrintCommands(commands)
if err != nil {
panic(err)
}