|
|
|
@ -2,16 +2,25 @@ package sl500_api |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"github.com/tarm/serial" |
|
|
|
|
"fmt" |
|
|
|
|
"log" |
|
|
|
|
"encoding/binary" |
|
|
|
|
"bytes" |
|
|
|
|
) |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var Baud = baudRegistry() |
|
|
|
|
|
|
|
|
|
func baudRegistry() *baudList { |
|
|
|
|
return &baudList{ |
|
|
|
|
Baud4800: baud{0, 4800}, |
|
|
|
|
Baud9600: baud{1, 9600}, |
|
|
|
|
Baud14400: baud{2, 14400}, |
|
|
|
|
Baud19200: baud{3, 19200}, |
|
|
|
|
Baud28800: baud{4, 28800}, |
|
|
|
|
Baud38400: baud{5, 38400}, |
|
|
|
|
Baud57600: baud{6, 57600}, |
|
|
|
|
Baud115200: baud{7, 115200}, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -24,7 +33,14 @@ const ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type baudList struct { |
|
|
|
|
Baud4800 baud |
|
|
|
|
Baud9600 baud |
|
|
|
|
Baud14400 baud |
|
|
|
|
Baud19200 baud |
|
|
|
|
Baud28800 baud |
|
|
|
|
Baud38400 baud |
|
|
|
|
Baud57600 baud |
|
|
|
|
Baud115200 baud |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type baud struct { |
|
|
|
@ -37,61 +53,82 @@ type Sl500 struct { |
|
|
|
|
port *serial.Port |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewConnection(path string, baud baud) Sl500 { |
|
|
|
|
c := &serial.Config{Name: path, Baud: baud.IntValue} // TODO
|
|
|
|
|
func NewConnection(path string, baud baud) (Sl500, error) { |
|
|
|
|
c := &serial.Config{Name: path, Baud: baud.IntValue, ReadTimeout: 5 * time.Second} // TODO
|
|
|
|
|
o, err := serial.OpenPort(c) |
|
|
|
|
|
|
|
|
|
res := Sl500{} |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
return res, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
res.config = c |
|
|
|
|
res.port = o |
|
|
|
|
|
|
|
|
|
_, err = res.RfInitCom(baud.ByteValue) |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err) |
|
|
|
|
return res, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
res := Sl500{config: c, port: o} |
|
|
|
|
log.Println("RfInitCom", res.RfInitCom(baud.ByteValue)) |
|
|
|
|
res.RfInitType(Type_ISO) |
|
|
|
|
log.Println("AntennaSta", res.RfAntennaSta(AntennaOff)) |
|
|
|
|
log.Println("Device ID", res.RfGetDeviceNumber()) |
|
|
|
|
_, err = res.RfInitType(Type_ISO) |
|
|
|
|
if err != nil { |
|
|
|
|
return res, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
_, err = res.RfAntennaSta(AntennaOff) |
|
|
|
|
if err != nil { |
|
|
|
|
return res, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return res |
|
|
|
|
_, err = res.RfGetDeviceNumber() |
|
|
|
|
if err != nil { |
|
|
|
|
return res, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return res, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Sl500) RfInitCom(baud byte) []byte { |
|
|
|
|
func (s *Sl500) RfInitCom(baud byte) ([]byte, error) { |
|
|
|
|
sendRequest(s.port, 0x0101, []byte{baud}) |
|
|
|
|
return readResponse(s.port) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Sl500) RfGetDeviceNumber() []byte { |
|
|
|
|
func (s *Sl500) RfGetDeviceNumber() ([]byte, error) { |
|
|
|
|
sendRequest(s.port, 0x0301, []byte{}) |
|
|
|
|
return readResponse(s.port) |
|
|
|
|
} |
|
|
|
|
func (s *Sl500) RfAntennaSta(antennaState byte) []byte { |
|
|
|
|
func (s *Sl500) RfAntennaSta(antennaState byte) ([]byte, error) { |
|
|
|
|
sendRequest(s.port, 0x0C01, []byte{antennaState}) |
|
|
|
|
return readResponse(s.port) |
|
|
|
|
} |
|
|
|
|
func (s *Sl500) RfInitType(workType byte) { |
|
|
|
|
func (s *Sl500) RfInitType(workType byte) ([]byte, error) { |
|
|
|
|
sendRequest(s.port, 0x0801, []byte{workType}) |
|
|
|
|
log.Println("RfInitType", readResponse(s.port)) |
|
|
|
|
return readResponse(s.port) |
|
|
|
|
} |
|
|
|
|
func (s *Sl500) RfBeep(durationMs byte) { |
|
|
|
|
func (s *Sl500) RfBeep(durationMs byte) ([]byte, error) { |
|
|
|
|
sendRequest(s.port, 0x0601, []byte{durationMs}) |
|
|
|
|
log.Println("RfBeep", readResponse(s.port)) |
|
|
|
|
return readResponse(s.port) |
|
|
|
|
} |
|
|
|
|
func readResponse(port *serial.Port) []byte { |
|
|
|
|
func readResponse(port *serial.Port) ([]byte, error) { |
|
|
|
|
var buf []byte |
|
|
|
|
innerBuf := make([]byte, 128) |
|
|
|
|
|
|
|
|
|
totalRead := 0 |
|
|
|
|
readTriesCount := 0 |
|
|
|
|
maxReadCount := 50 |
|
|
|
|
|
|
|
|
|
for ; ; { |
|
|
|
|
readTriesCount += 1 |
|
|
|
|
|
|
|
|
|
if readTriesCount >= maxReadCount { |
|
|
|
|
break |
|
|
|
|
return nil, fmt.Errorf("Reads tries exceeded") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
innerBuf := make([]byte, 128) |
|
|
|
|
n, err := port.Read(innerBuf) |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Println(err) |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
totalRead += n |
|
|
|
@ -107,10 +144,10 @@ func readResponse(port *serial.Port) []byte { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
if buf[0] != 0xAA || buf[1] != 0xBB { |
|
|
|
|
log.Println("shit happens") |
|
|
|
|
return nil, fmt.Errorf("Response format invalid") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return buf |
|
|
|
|
return buf, nil |
|
|
|
|
} |
|
|
|
|
func sendRequest(port *serial.Port, commandCode int16, bytesData []byte) { |
|
|
|
|
buf := new(bytes.Buffer) |
|
|
|
|