diff --git a/main.go b/main.go index 6769950..8a79d5a 100644 --- a/main.go +++ b/main.go @@ -20,8 +20,8 @@ func main() { mds, err := service.NewModbusDcService(&proto.ModbusConfig{ Id: 1, Url: "tcp://127.0.0.1:502", - UnitId: 2, - Timeout: 1000, + UnitId: 1, + Timeout: 500, Interval: 1000, Mapping: []*proto.ModbusDcMapping{ { diff --git a/modbus/client.go b/modbus/client.go index 83cda71..ace86e1 100644 --- a/modbus/client.go +++ b/modbus/client.go @@ -15,6 +15,11 @@ type ConnectState int const () +type ClientConfig struct { + Url string + Timeout uint32 // 超时时间,单位ms +} + type client struct { url string cli *modbus.ModbusClient @@ -23,16 +28,16 @@ type client struct { cancel context.CancelFunc } -func newClient(url string) (MasterClient, error) { +func newClient(conf *ClientConfig) (MasterClient, error) { cli, err := modbus.NewClient(&modbus.ClientConfiguration{ - URL: url, - Timeout: 1 * time.Second, + URL: conf.Url, + Timeout: time.Duration(conf.Timeout) * time.Millisecond, }) if err != nil { return nil, err } c := &client{ - url: url, + url: conf.Url, cli: cli, connected: false, } diff --git a/modbus/manage.go b/modbus/manage.go index 1b0bbe6..9a6b097 100644 --- a/modbus/manage.go +++ b/modbus/manage.go @@ -21,24 +21,24 @@ func GetClient(url string) (MasterClient, bool) { } // 新建客户端 -func NewClient(url string) (MasterClient, error) { +func NewClient(conf *ClientConfig) (MasterClient, error) { manager.lock.Lock() defer manager.lock.Unlock() - client, err := newClient(url) + client, err := newClient(conf) if err != nil { return nil, err } - manager.clientMap[url] = client + manager.clientMap[conf.Url] = client return client, err } // 获取或新建客户端 -func GetOrInitClient(url string) (MasterClient, error) { - client, ok := GetClient(url) +func GetOrInitClient(conf *ClientConfig) (MasterClient, error) { + client, ok := GetClient(conf.Url) if ok { return client, nil } - return NewClient(url) + return NewClient(conf) } // 删除客户端 diff --git a/proto/src/dc.proto b/proto/src/dc.proto index be19bd0..308cd46 100644 --- a/proto/src/dc.proto +++ b/proto/src/dc.proto @@ -19,7 +19,7 @@ message Modbus { } // Modbus服务写策略 enum WriteStrategy { - OnUpdate = 0; // 数据更新时执行写操作 + OnUpdate = 0; // 数据更新时写 OnScheduled = 1; // 定时写 } // 寄存器字节序 diff --git a/service/modbus_dc_app.go b/service/modbus_dc_service.go similarity index 98% rename from service/modbus_dc_app.go rename to service/modbus_dc_service.go index 0569858..6403c9a 100644 --- a/service/modbus_dc_app.go +++ b/service/modbus_dc_service.go @@ -28,7 +28,10 @@ func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, e if ok { return nil, fmt.Errorf("modbus客户端已存在,url=%s", config.Url) } - cli, err := modbus.NewClient(config.Url) + cli, err := modbus.NewClient(&modbus.ClientConfig{ + Url: config.Url, + Timeout: config.Timeout, + }) if err != nil { return nil, errors.Join(err, fmt.Errorf("modbus客户端创建失败,url=%s", config.Url)) } diff --git a/service/proto/dc.pb.go b/service/proto/dc.pb.go index e3d9555..2935260 100644 --- a/service/proto/dc.pb.go +++ b/service/proto/dc.pb.go @@ -142,7 +142,7 @@ func (Modbus_Function) EnumDescriptor() ([]byte, []int) { type Modbus_WriteStrategy int32 const ( - Modbus_OnUpdate Modbus_WriteStrategy = 0 // 数据更新时执行写操作 + Modbus_OnUpdate Modbus_WriteStrategy = 0 // 数据更新时写 Modbus_OnScheduled Modbus_WriteStrategy = 1 // 定时写 ) @@ -373,11 +373,11 @@ type ModbusDcMapping struct { unknownFields protoimpl.UnknownFields Function Modbus_Function `protobuf:"varint,1,opt,name=function,proto3,enum=iot_service.Modbus_Function" json:"function,omitempty"` // 功能 - Addr uint32 `protobuf:"varint,2,opt,name=addr,proto3" json:"addr,omitempty"` // 起始地址,当功能为位功能时,表示起始位地址,当功能为字节功能时,表示起始字节地址 - Quantity uint32 `protobuf:"varint,3,opt,name=quantity,proto3" json:"quantity,omitempty"` // 数量,当功能为位功能时,表示位数,当功能为字节功能时,表示字节数 + Addr uint32 `protobuf:"varint,2,opt,name=addr,proto3" json:"addr,omitempty"` // 起始地址,当功能为位功能时,表示起始位地址,当功能为寄存器功能时,表示起始字(2个字节)地址 + Quantity uint32 `protobuf:"varint,3,opt,name=quantity,proto3" json:"quantity,omitempty"` // 数量,当功能为位功能时,表示位数,当功能为寄存器功能时,表示字(2个字节)数 WriteStrategy Modbus_WriteStrategy `protobuf:"varint,4,opt,name=writeStrategy,proto3,enum=iot_service.Modbus_WriteStrategy" json:"writeStrategy,omitempty"` // 当功能为写入类功能时(不包含读写类功能),写策略 Type DataType `protobuf:"varint,5,opt,name=type,proto3,enum=iot_service.DataType" json:"type,omitempty"` // 对应数据类型 - Start uint32 `protobuf:"varint,6,opt,name=start,proto3" json:"start,omitempty"` // 驱动/采集码表中的起始下标,当功能为位功能时,表示起始位,当功能为字节功能时,表示起始字节 + Start uint32 `protobuf:"varint,6,opt,name=start,proto3" json:"start,omitempty"` // 驱动/采集码表中的起始下标,当功能为位功能时,表示起始位,当功能为寄存器功能时,表示起始字节 } func (x *ModbusDcMapping) Reset() {