规范串口通讯

This commit is contained in:
2024-12-19 10:49:02 +08:00
parent 3f760e2955
commit b4913d63b3
5 changed files with 37 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
location: wushan
point: 5
relay: /dev/ttyUSB0
point: 4
relay: /dev/ttyUSB1
maxTimeout: 60 # 单位 s必须大于 0
log:
level: debug
@@ -19,14 +19,14 @@ aliyun:
timeout: 10 # 单位 s
voice: zhifeng_emo
game:
addr: /dev/ttyUSB0 # 点位 5 的串口地址
# pushGroups: # 点位 4 的发卡器配置
# - name: gpiochip0
# outOK: 6
# lower: 13
# error: 19
# empty: 26
# push: 11
# reset: 22
# pull: 27
# readAddr: /dev/ttyUSB0 # 点位 4 的读卡器配置
# addr: /dev/ttyUSB0 # 点位 5 的串口地址
pushGroups: # 点位 4 的发卡器配置
- name: gpiochip0
outOK: 12
lower: 1
error: 7
empty: 8
push: 0
reset: 5
pull: 6
readAddr: /dev/ttyUSB0 # 点位 4 的读卡器配置

View File

@@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"syscall"
"time"
)
func main() {
@@ -19,8 +20,17 @@ func main() {
}
defer r.Close()
r.On(0)
defer r.Off(0)
for i := 0; i < 4; i++ {
func(num int) {
r.On(num)
defer r.Off(num)
time.Sleep(1 * time.Second)
}(i)
time.Sleep(1 * time.Second)
}
r.OnAll()
defer r.OffAll()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)

View File

@@ -48,7 +48,7 @@ func PushCard(ctx context.Context) leaf.HandlerFunc {
devices := make([]*card_pusher.Device, 0)
for i, group := range g.PushGroups {
// 对读卡器初始化配置
reader.SetSlave(uint8(i + 1))
reader.SetSlave(byte(i + 1))
err = reader.Init()
if err != nil {
zap.S().Panicln("读卡器初始配置失败", err)
@@ -114,7 +114,7 @@ func PushCard(ctx context.Context) leaf.HandlerFunc {
if body.num != 0 {
// 若卡片就位,读取卡片信息
if devices[body.num-1].GetOutOk() == 1 {
reader.SetSlave(uint8(body.num))
reader.SetSlave(byte(body.num))
if info := reader.GetCardInfo(); info != nil {
body.CardId = info.ID
}

View File

@@ -13,7 +13,8 @@ import (
type Reader interface {
io.Closer
SetSlave(uint8)
// SetSlave 设置读卡器的 UnitID
SetSlave(byte)
// WriteUnitId 写入读卡器的 UnitID
WriteUnitId(uint16) error
// Init 初始化读卡器配置

View File

@@ -7,10 +7,15 @@ import (
type Relay interface {
io.Closer
SetSlave(slaveID byte)
On(num int) error
Off(num int) error
// SetSlave 设置继电器的 UnitID
SetSlave(byte)
// On 打开继电器
On(int) error
// Off 关闭继电器
Off(int) error
// OnAll 打开所有继电器
OnAll() error
// OffAll 关闭所有继电器
OffAll() error
}