日志打印字节数组和bit数组格式化(字节16进制,bit二进制)

This commit is contained in:
walker 2023-12-18 16:25:48 +08:00
parent 0b20dcdf7a
commit aec90fa3b8
2 changed files with 24 additions and 6 deletions

View File

@ -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) slog.Error("Modbus驱动采集服务写入线圈失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err, "Function", mdm.Function)
return err return err
} else { } 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: case sproto.Modbus_WriteRegister, sproto.Modbus_WriteRegisters, sproto.Modbus_RWRegisters:
data := getQcBytes(bytes, mdm) 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) slog.Error("Modbus驱动采集服务写入寄存器失败", "url", m.config.Url, "unitid", m.config.UnitId, "error", err, "Function", mdm.Function)
return err return err
} else { } 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)
} }
} }
} }

View File

@ -1,8 +1,10 @@
package model package model
import ( import (
"encoding/hex"
"fmt" "fmt"
"log/slog" "log/slog"
"strings"
) )
type DCEvent int type DCEvent int
@ -169,7 +171,7 @@ func (d *qc) UpdateCjByBits(start uint32, bits []bool) error {
d.cjBits[i] = bits[i-start] d.cjBits[i] = bits[i-start]
} }
d.cj = EncodeBytes(d.cjBits) 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) // d.Emit(DCE_Collect_Update)
return nil return nil
} }
@ -186,7 +188,7 @@ func (d *qc) UpdateCjByBytes(start uint32, values []byte) error {
} }
copy(d.cj[start:end], values) copy(d.cj[start:end], values)
d.cjBits = DecodeBools(d.cj) 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) // d.Emit(DCE_Collect_Update)
return nil return nil
} }
@ -205,7 +207,7 @@ func (d *qc) UpdateQdByBits(start uint32, bits []bool) error {
d.qdBits[i] = bits[i-start] d.qdBits[i] = bits[i-start]
} }
d.qd = EncodeBytes(d.qdBits) 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) // d.Emit(DCE_Drive_Update)
return nil return nil
} }
@ -222,7 +224,7 @@ func (d *qc) UpdateQdByBytes(start uint32, values []byte) error {
} }
copy(d.qd[start:end], values) copy(d.qd[start:end], values)
d.qdBits = DecodeBools(d.qd) 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) // d.Emit(DCE_Drive_Update)
return nil return nil
} }
@ -278,3 +280,19 @@ func EncodeBytes(bits []bool) (out []byte) {
} }
return out 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()
}