2023-12-06 17:07:52 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-08 18:08:15 +08:00
|
|
|
"log/slog"
|
|
|
|
"os"
|
2023-12-06 17:07:52 +08:00
|
|
|
"time"
|
|
|
|
|
2023-12-08 18:08:15 +08:00
|
|
|
"joylink.club/iot/service"
|
|
|
|
"joylink.club/iot/service/model"
|
|
|
|
"joylink.club/iot/service/proto"
|
2023-12-06 17:07:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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))
|
|
|
|
mds, err := service.NewModbusDcService(&proto.ModbusConfig{
|
|
|
|
Id: 1,
|
|
|
|
Url: "tcp://127.0.0.1:502",
|
2023-12-11 14:26:31 +08:00
|
|
|
UnitId: 1,
|
|
|
|
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)
|
2023-12-06 17:07:52 +08:00
|
|
|
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-06 17:07:52 +08:00
|
|
|
}
|
2023-12-08 18:08:15 +08:00
|
|
|
time.Sleep(time.Second)
|
2023-12-06 17:07:52 +08:00
|
|
|
}
|
2023-12-08 18:08:15 +08:00
|
|
|
}()
|
2023-12-06 17:07:52 +08:00
|
|
|
|
2023-12-08 18:08:15 +08:00
|
|
|
time.Sleep(time.Minute * 2)
|
|
|
|
mds.Stop()
|
2023-12-06 17:07:52 +08:00
|
|
|
}
|