commit 0689803582118ea9b01cebfe6a58ff8b186a06c3 Author: Marvin Steadfast Date: Sat Mar 27 15:14:37 2021 +0100 works kinda diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..11c1100 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module gitlab.com/wobcom/iot/schnutibox + +go 1.16 + +require github.com/karalabe/hid v1.0.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e09e634 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/karalabe/hid v1.0.0 h1:+/CIMNXhSU/zIJgnIvBD2nKHxS/bnRHhhs9xBryLpPo= +github.com/karalabe/hid v1.0.0/go.mod h1:Vr51f8rUOLYrfrWDFlV12GGQgM5AT8sVh+2fY4MPeu8= diff --git a/main.go b/main.go new file mode 100644 index 0000000..5052984 --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +// https://medium.com/coinmonks/iot-tutorial-read-tags-from-a-usb-rfid-reader-with-raspberry-pi-and-node-red-from-scratch-4554836be127 +// nolint: gochecknoglobals, lll, forbidigo, godox +package main + +import ( + "fmt" + "log" + + "github.com/karalabe/hid" +) + +const ( + product = 0x27db + vendor = 0x16c0 +) + +const newLine = 40 + +var charMap = map[byte]string{ + 30: "1", + 31: "2", + 32: "3", + 33: "4", + 34: "5", + 35: "6", + 36: "7", + 37: "8", + 38: "9", + 39: "0", +} + +func main() { + readerInfo := hid.Enumerate(vendor, product)[0] + + d, err := readerInfo.Open() + if err != nil { + log.Fatal(err) + } + + id := make(chan string) + + go func() { + buf := make([]byte, 3) + + rfid := "" + + for { + // Reading the RFID reader. + // TODO: Check if "/dev/hidraw" can be used. + _, err := d.Read(buf) + if err != nil { + log.Print(err) + + continue + } + + // 40 means "\n" and is an indicator that the input is complete. + // Send the id string to the consumer channel. + if buf[2] == newLine { + id <- rfid + rfid = "" + + continue + } + + // Check if byte is in the charMap and if its true, append it to the rfid string. + // It waits till next read and checks for the newline byte. + c, ok := charMap[buf[2]] + if ok { + rfid += c + } + } + }() + + // Wait for an RFID ID and do something with it. + for { + rfid := <-id + fmt.Println(rfid) + } +}