From aec90fa3b8126c6a9684abbe8ceab10b1251c888 Mon Sep 17 00:00:00 2001 From: walker Date: Mon, 18 Dec 2023 16:25:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0=E5=AD=97?= =?UTF-8?q?=E8=8A=82=E6=95=B0=E7=BB=84=E5=92=8Cbit=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=EF=BC=88=E5=AD=97=E8=8A=8216?= =?UTF-8?q?=E8=BF=9B=E5=88=B6,bit=E4=BA=8C=E8=BF=9B=E5=88=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/modbus_qc_mapping.go | 4 ++-- service/model/dc.go | 26 ++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/service/modbus_qc_mapping.go b/service/modbus_qc_mapping.go index ee58620..494bcc5 100644 --- a/service/modbus_qc_mapping.go +++ b/service/modbus_qc_mapping.go @@ -121,7 +121,7 @@ func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { slog.Error("Modbus驱动采集服务写入线圈失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err, "Function", mdm.Function) return err } else { - slog.Info("Modbus驱动采集服务写入线圈成功", "url", m.config.Url, "unitid", m.config.UnitId, "Function", mdm.Function, "data", data, "mapping", mdm) + slog.Debug("Modbus驱动采集服务写入线圈成功", "url", m.config.Url, "unitid", m.config.UnitId, "Function", mdm.Function, "data", model.BitsDebug(data), "mapping", mdm) } case sproto.Modbus_WriteRegister, sproto.Modbus_WriteRegisters, sproto.Modbus_RWRegisters: data := getQcBytes(bytes, mdm) @@ -130,7 +130,7 @@ func (m *modbusQcService) onWrite(dt sproto.DataType, bytes []byte) error { slog.Error("Modbus驱动采集服务写入寄存器失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err, "Function", mdm.Function) return err } else { - slog.Info("Modbus驱动采集服务写入寄存器成功", "url", m.config.Url, "unitid", m.config.UnitId, "Function", mdm.Function, "data", data, "mapping", mdm) + slog.Debug("Modbus驱动采集服务写入寄存器成功", "url", m.config.Url, "unitid", m.config.UnitId, "Function", mdm.Function, "data", model.BytesDebug(data), "mapping", mdm) } } } diff --git a/service/model/dc.go b/service/model/dc.go index f4a239d..d8abf1e 100644 --- a/service/model/dc.go +++ b/service/model/dc.go @@ -1,8 +1,10 @@ package model import ( + "encoding/hex" "fmt" "log/slog" + "strings" ) type DCEvent int @@ -169,7 +171,7 @@ func (d *qc) UpdateCjByBits(start uint32, bits []bool) error { d.cjBits[i] = bits[i-start] } d.cj = EncodeBytes(d.cjBits) - slog.Debug("UpdateCollectByBits成功", "collect", fmt.Sprintf("%v", d.cj), "collectBits", d.cjBits) + slog.Debug("UpdateCollectByBits成功", "collect", BytesDebug(d.cj), "collectBits", BitsDebug(d.cjBits)) // d.Emit(DCE_Collect_Update) return nil } @@ -186,7 +188,7 @@ func (d *qc) UpdateCjByBytes(start uint32, values []byte) error { } copy(d.cj[start:end], values) d.cjBits = DecodeBools(d.cj) - slog.Debug("UpdateCollectByBytes成功", "collect", fmt.Sprintf("%v", d.cj), "collectBits", d.cjBits) + slog.Debug("UpdateCollectByBytes成功", "collect", BytesDebug(d.cj), "collectBits", BitsDebug(d.cjBits)) // d.Emit(DCE_Collect_Update) return nil } @@ -205,7 +207,7 @@ func (d *qc) UpdateQdByBits(start uint32, bits []bool) error { d.qdBits[i] = bits[i-start] } d.qd = EncodeBytes(d.qdBits) - slog.Debug("UpdateDriveByBits成功", "drive", fmt.Sprintf("%v", d.qd), "driveBits", d.qdBits) + slog.Debug("UpdateDriveByBits成功", "drive", BytesDebug(d.qd), "driveBits", BitsDebug(d.qdBits)) // d.Emit(DCE_Drive_Update) return nil } @@ -222,7 +224,7 @@ func (d *qc) UpdateQdByBytes(start uint32, values []byte) error { } copy(d.qd[start:end], values) d.qdBits = DecodeBools(d.qd) - slog.Debug("UpdateDriveByBytes成功", "drive", fmt.Sprintf("%v", d.qd), "driveBits", d.qdBits) + slog.Debug("UpdateDriveByBytes成功", "drive", BytesDebug(d.qd), "driveBits", BitsDebug(d.qdBits)) // d.Emit(DCE_Drive_Update) return nil } @@ -278,3 +280,19 @@ func EncodeBytes(bits []bool) (out []byte) { } return out } + +func BytesDebug(bytes []byte) string { + return hex.EncodeToString(bytes) +} + +func BitsDebug(bits []bool) string { + var sb strings.Builder + for _, v := range bits { + if v { + sb.WriteString("1") + } else { + sb.WriteString("0") + } + } + return sb.String() +}