modbus client构造参数调整,添加timeout参数

This commit is contained in:
walker 2023-12-11 14:26:31 +08:00
parent ba642bcfc0
commit a8c9c6857a
6 changed files with 26 additions and 18 deletions

View File

@ -20,8 +20,8 @@ func main() {
mds, err := service.NewModbusDcService(&proto.ModbusConfig{ mds, err := service.NewModbusDcService(&proto.ModbusConfig{
Id: 1, Id: 1,
Url: "tcp://127.0.0.1:502", Url: "tcp://127.0.0.1:502",
UnitId: 2, UnitId: 1,
Timeout: 1000, Timeout: 500,
Interval: 1000, Interval: 1000,
Mapping: []*proto.ModbusDcMapping{ Mapping: []*proto.ModbusDcMapping{
{ {

View File

@ -15,6 +15,11 @@ type ConnectState int
const () const ()
type ClientConfig struct {
Url string
Timeout uint32 // 超时时间,单位ms
}
type client struct { type client struct {
url string url string
cli *modbus.ModbusClient cli *modbus.ModbusClient
@ -23,16 +28,16 @@ type client struct {
cancel context.CancelFunc cancel context.CancelFunc
} }
func newClient(url string) (MasterClient, error) { func newClient(conf *ClientConfig) (MasterClient, error) {
cli, err := modbus.NewClient(&modbus.ClientConfiguration{ cli, err := modbus.NewClient(&modbus.ClientConfiguration{
URL: url, URL: conf.Url,
Timeout: 1 * time.Second, Timeout: time.Duration(conf.Timeout) * time.Millisecond,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := &client{ c := &client{
url: url, url: conf.Url,
cli: cli, cli: cli,
connected: false, connected: false,
} }

View File

@ -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() manager.lock.Lock()
defer manager.lock.Unlock() defer manager.lock.Unlock()
client, err := newClient(url) client, err := newClient(conf)
if err != nil { if err != nil {
return nil, err return nil, err
} }
manager.clientMap[url] = client manager.clientMap[conf.Url] = client
return client, err return client, err
} }
// 获取或新建客户端 // 获取或新建客户端
func GetOrInitClient(url string) (MasterClient, error) { func GetOrInitClient(conf *ClientConfig) (MasterClient, error) {
client, ok := GetClient(url) client, ok := GetClient(conf.Url)
if ok { if ok {
return client, nil return client, nil
} }
return NewClient(url) return NewClient(conf)
} }
// 删除客户端 // 删除客户端

View File

@ -19,7 +19,7 @@ message Modbus {
} }
// Modbus服务写策略 // Modbus服务写策略
enum WriteStrategy { enum WriteStrategy {
OnUpdate = 0; // OnUpdate = 0; //
OnScheduled = 1; // OnScheduled = 1; //
} }
// //

View File

@ -28,7 +28,10 @@ func NewModbusDcService(config *sproto.ModbusConfig, dc model.DC) (IotService, e
if ok { if ok {
return nil, fmt.Errorf("modbus客户端已存在,url=%s", config.Url) 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 { if err != nil {
return nil, errors.Join(err, fmt.Errorf("modbus客户端创建失败,url=%s", config.Url)) return nil, errors.Join(err, fmt.Errorf("modbus客户端创建失败,url=%s", config.Url))
} }

View File

@ -142,7 +142,7 @@ func (Modbus_Function) EnumDescriptor() ([]byte, []int) {
type Modbus_WriteStrategy int32 type Modbus_WriteStrategy int32
const ( const (
Modbus_OnUpdate Modbus_WriteStrategy = 0 // 数据更新时执行操作 Modbus_OnUpdate Modbus_WriteStrategy = 0 // 数据更新时
Modbus_OnScheduled Modbus_WriteStrategy = 1 // 定时写 Modbus_OnScheduled Modbus_WriteStrategy = 1 // 定时写
) )
@ -373,11 +373,11 @@ type ModbusDcMapping struct {
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Function Modbus_Function `protobuf:"varint,1,opt,name=function,proto3,enum=iot_service.Modbus_Function" json:"function,omitempty"` // 功能 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"` // 起始地址,当功能为位功能时,表示起始位地址,当功能为字节功能时,表示起始字节地址 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"` // 数量,当功能为位功能时,表示位数,当功能为字节功能时,表示字节 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"` // 当功能为写入类功能时(不包含读写类功能),写策略 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"` // 对应数据类型 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() { func (x *ModbusDcMapping) Reset() {