From 620d904921c76b6b29d1b69787da7a82121d4c31 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 26 十月 2023 21:15:35 +0800 Subject: [PATCH] 读写plc支持modbusRTU --- model/device_plc.go | 21 ++++++++++++++++++--- 1 files changed, 18 insertions(+), 3 deletions(-) diff --git a/model/device_plc.go b/model/device_plc.go index 4136ecb..c68b1f3 100644 --- a/model/device_plc.go +++ b/model/device_plc.go @@ -13,14 +13,20 @@ // DevicePlc 璁惧鐨凱LC閰嶇疆 DevicePlc struct { gorm.Model + DeviceID string `gorm:"column:device_id;type:varchar(255);not null;unique" json:"deviceID"` //璁惧缂栧彿 Brand string `gorm:"type:varchar(191)" json:"brand"` Method constvar.PlcMethod `gorm:"type:varchar(191)" json:"method"` Address string `gorm:"type:varchar(191)" json:"address"` //plc ip鍦板潃锛� method = modbusTCP鐢� Port int `json:"port"` //plc 绔彛鍙凤紝 method = modbusTCP鐢� BaudRate int `json:"baudRate"` //涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢� SerialName string `json:"serialName"` //涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢� - IsOpen bool `json:"isOpen"` - Detail string `gorm:"type:varchar(2048)" json:"-"` + + DataBit int `gorm:"type:int(11)" json:"dataBit"` //鏁版嵁浣嶏紝method = modbusRTU 鐢� + StopBit int `gorm:"type:int(11)" json:"stopBit"` //鍋滄浣嶏紝method = modbusRTU 鐢� + Parity constvar.Parity `gorm:"type:int(11)" json:"parity"` //鏍¢獙鏂瑰紡锛宮ethod = modbusRTU 鐢� + + IsOpen bool `json:"isOpen"` + Detail string `gorm:"type:varchar(2048)" json:"-"` Details []*DevicePlcAddress `gorm:"-" json:"details"` @@ -84,11 +90,20 @@ return slf } +func (slf *DevicePlcSearch) SetDeviceId(deviceId string) *DevicePlcSearch { + slf.DeviceID = deviceId + return slf +} + func (slf *DevicePlcSearch) build() *gorm.DB { var db = slf.Orm.Table(slf.TableName()) if slf.ID > 0 { db = db.Where("id = ?", slf.ID) + } + + if slf.DeviceID != "" { + db = db.Where("device_id = ?", slf.DeviceID) } if slf.Order != "" { @@ -112,7 +127,7 @@ func (slf *DevicePlcSearch) Save(record *DevicePlc) error { var db = slf.build() - if err := db.Omit("CreatedAt").Save(record).Error; err != nil { + if err := db.Omit("CreatedAt").Updates(record).Error; err != nil { return fmt.Errorf("save err: %v, record: %+v", err, record) } -- Gitblit v1.8.0