jl-iot/main.go

66 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"fmt"
2023-12-08 18:08:15 +08:00
"log/slog"
"os"
"time"
2023-12-08 18:08:15 +08:00
"joylink.club/iot/service"
"joylink.club/iot/service/model"
"joylink.club/iot/service/proto"
)
func main() {
2023-12-08 18:08:15 +08:00
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: false,
})))
dc := model.NewDC(make([]byte, 2), make([]byte, 2))
2023-12-13 11:03:21 +08:00
mds, err := service.NewModbusQcService(&proto.ModbusConfig{
2023-12-08 18:08:15 +08:00
Url: "tcp://127.0.0.1:502",
UnitId: 2,
Timeout: 500,
2023-12-08 18:08:15 +08:00
Interval: 1000,
Mapping: []*proto.ModbusDcMapping{
{
// Function: proto.Modbus_ReadHoldingRegister,
Function: proto.Modbus_ReadCoil,
Addr: 0,
Quantity: 16,
Type: proto.DataType_CollectTable,
Start: 0,
},
{
Function: proto.Modbus_WriteCoils,
Addr: 16,
Quantity: 16,
Type: proto.DataType_DriveTable,
WriteStrategy: proto.Modbus_OnUpdate,
Start: 0,
},
},
}, dc)
if err != nil {
panic(err)
}
2023-12-08 18:08:15 +08:00
go func() {
i := 0
for {
c := dc.GetCollect()
fmt.Printf("采集数据: %v\n", c)
i++
if i%3 == 0 {
idx := i % 8
dc.UpdateDriveByBytes(0, []byte{byte(1 << idx)})
fmt.Printf("设置驱动数据: %v\n", dc.GetDrive())
}
2023-12-08 18:08:15 +08:00
time.Sleep(time.Second)
}
2023-12-08 18:08:15 +08:00
}()
2023-12-08 18:08:15 +08:00
time.Sleep(time.Minute * 2)
mds.Stop()
}