package main import ( "fmt" "log/slog" "os" "time" "joylink.club/iot/service" "joylink.club/iot/service/model" "joylink.club/iot/service/proto" ) func main() { 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", UnitId: 2, Timeout: 1000, 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) } 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()) } time.Sleep(time.Second) } }() time.Sleep(time.Minute * 2) mds.Stop() }