继电器、读卡器,都用统一的modbus协议
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"go.bug.st/serial/enumerator"
|
||||
)
|
||||
|
||||
func PrintPorts() {
|
||||
ports, err := enumerator.GetDetailedPortsList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if len(ports) == 0 {
|
||||
return
|
||||
}
|
||||
for _, port := range ports {
|
||||
fmt.Printf("Port: %s\n", port.Name)
|
||||
if port.Product != "" {
|
||||
fmt.Printf(" Product Name: %s\n", port.Product)
|
||||
}
|
||||
if port.IsUSB {
|
||||
fmt.Printf(" USB ID : %s:%s\n", port.VID, port.PID)
|
||||
fmt.Printf(" USB serial : %s\n", port.SerialNumber)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +1,64 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"go.bug.st/serial"
|
||||
"github.com/grid-x/modbus"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
port serial.Port
|
||||
type Relay interface {
|
||||
io.Closer
|
||||
SetSlave(slaveID byte)
|
||||
On(num int) error
|
||||
Off(num int) error
|
||||
OnAll() error
|
||||
OffAll() error
|
||||
}
|
||||
|
||||
func (r *Device) Close() error {
|
||||
return r.port.Close()
|
||||
type device struct {
|
||||
h modbus.ClientHandler
|
||||
c modbus.Client
|
||||
}
|
||||
|
||||
func (r *Device) On(num int) error {
|
||||
_, err := io.WriteString(r.port, fmt.Sprintf("AT+OUT%v+1=ON\r\n", num))
|
||||
func (r *device) SetSlave(slaveID byte) {
|
||||
r.h.SetSlave(slaveID)
|
||||
}
|
||||
|
||||
func (r *device) Close() error {
|
||||
return r.h.Close()
|
||||
}
|
||||
|
||||
func (r *device) OnAll() error {
|
||||
_, err := r.c.WriteMultipleCoils(uint16(0), 16, []byte{0xFF, 0xFF})
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Device) Off(num int) error {
|
||||
_, err := io.WriteString(r.port, fmt.Sprintf("AT+OUT%v+1=OFF\r\n", num))
|
||||
func (r *device) OffAll() error {
|
||||
_, err := r.c.WriteMultipleCoils(uint16(0), 16, []byte{0x00, 0x00})
|
||||
return err
|
||||
}
|
||||
|
||||
func New(portName string, reader func(msg string)) (*Device, error) {
|
||||
port, err := serial.Open(portName, &serial.Mode{
|
||||
BaudRate: 9600,
|
||||
DataBits: 8,
|
||||
})
|
||||
if err != nil {
|
||||
func (r *device) On(num int) error {
|
||||
_, err := r.c.WriteSingleCoil(uint16(num), 0xFF00)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *device) Off(num int) error {
|
||||
_, err := r.c.WriteSingleCoil(uint16(num), 0x0000)
|
||||
return err
|
||||
}
|
||||
|
||||
func New(address string) (Relay, error) {
|
||||
h := modbus.NewRTUClientHandler(address)
|
||||
h.SlaveID = 1
|
||||
h.BaudRate = 9600
|
||||
h.DataBits = 8
|
||||
h.Parity = "N"
|
||||
h.StopBits = 1
|
||||
|
||||
if err := h.Connect(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
r := bufio.NewReader(port)
|
||||
line, _, _ := r.ReadLine()
|
||||
if reader != nil {
|
||||
reader(string(line))
|
||||
}
|
||||
}
|
||||
}()
|
||||
return &Device{port: port}, nil
|
||||
c := modbus.NewClient(h)
|
||||
|
||||
return &device{h, c}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user