package flow import ( "encoding/binary" "fmt" "log" ) // Sensor 传感器 type Sensor struct { SP01 uint16 // 清洗罐气压传感器 SP-01 罐体排空,值≤0.5(暂定) SP02 uint16 // 清洗液水箱水位传感器 SP-02 模拟量传感器 LT01 uint16 // 清洗罐满水位传感器 (非接触式水位传感器) LT-01 模拟量传感器 LT02 uint16 // 清洗罐排水传感器 (非接触式水位传感器) LT-02 模拟量传感器 LT03 uint8 // 清洗液水箱满水位传感器 (浮子式水位传感器) LT-03 值为1,清洗液水箱满水位 } func (sensor *Sensor) String() string { return fmt.Sprintf("SP01=%d SP02=%d LT01=%d LT02=%d LT03=%d", sensor.SP01, sensor.SP02, sensor.LT01, sensor.LT02, sensor.LT03) } func NewSensor(buf []byte) *Sensor { if len(buf) == 14 { if buf[0] == byte(0xaa) && buf[1] == byte(0x55) { sensor := &Sensor{} sensor.SP01 = binary.BigEndian.Uint16(buf[2:4]) sensor.SP02 = binary.BigEndian.Uint16(buf[4:6]) sensor.LT01 = binary.BigEndian.Uint16(buf[6:8]) sensor.LT02 = binary.BigEndian.Uint16(buf[8:10]) sensor.LT03 = uint8(buf[10]) return sensor } } log.Println(buf, "非标准传感器字节流") return nil } // BytesFromSensor 生成Buf从Sensor func BytesFromSensor(sensor *Sensor) []byte { buf := make([]byte, 14) buf[0] = byte(0xaa) buf[1] = byte(0x55) binary.BigEndian.PutUint16(buf[2:4], sensor.SP01) binary.BigEndian.PutUint16(buf[4:6], sensor.SP02) binary.BigEndian.PutUint16(buf[6:8], sensor.LT01) binary.BigEndian.PutUint16(buf[8:10], sensor.LT02) buf[10] = sensor.LT03 for _, b := range buf[0:13] { buf[13] += b } return buf }