cd
is a Go library designed to interface with the "USB TRIGGER for CASH DRAWER" BT-100U. It simplifies the process of opening a cash drawer using Go code.
To use the cd
library in your Go project, you need to install it using:
go get github.com/Nimon77/cd
Here is a quick guide on how to use the cd
library to control your cash drawer.
package main
import (
"context"
"log"
"github.com/Nimon77/cd"
)
func main() {
ctx := context.Background()
// Initialize the CashDrawer manually with a specific port and baud rate
drawer, err := cd.New("/dev/ttyUSB0", 9600)
if err != nil {
log.Fatal(err)
}
defer drawer.Close()
// Open the cash drawer
err = drawer.Open(ctx)
if err != nil {
log.Fatal(err)
}
log.Println("Cash drawer opened successfully!")
}
If you prefer the library to automatically detect the correct USB port, you can use the NewAuto
function.
package main
import (
"log"
"github.com/Nimon77/cd"
)
func main() {
// Automatically detect the cash drawer and initialize it
drawer, err := cd.NewAuto()
if err != nil {
log.Fatal(err)
}
defer drawer.Close()
// Open the cash drawer
err = drawer.Open(context.Background())
if err != nil {
log.Fatal(err)
}
log.Println("Cash drawer opened successfully!")
}
Creates a new CashDrawer
instance with the specified serial port and baud rate.
port
: The serial port to which the cash drawer is connected (e.g.,/dev/ttyUSB0
).baud
: The baud rate for the serial communication (e.g., 9600).
Automatically detects the USB port connected to the cash drawer and initializes a new CashDrawer
instance. This function scans the available USB devices and matches the correct one using predefined rules.
Sends the command to open the cash drawer. It writes the necessary bytes to the serial port to trigger the drawer to open.
ctx
: The context for managing request deadlines and cancellation signals.
Closes the serial port connection to the cash drawer.
This library depends on the following packages:
github.com/citilinkru/libudev
- For scanning and detecting USB devices. (linux only)github.com/jacobsa/go-serial/serial
- For handling serial port communications.
Make sure to include these dependencies in your project.
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributions are welcome! Please fork the repository and submit a pull request with your changes.
If you encounter any issues or have questions, feel free to open an issue on GitHub.