package bds import ( "encoding/binary" "fmt" "wb/ut" ) const ( startStopBit byte = 0x7e startBit byte = startStopBit stopBit byte = startStopBit escChar byte = 0x7d startStopAdd byte = 0x02 escCharAdd byte = 0x01 uartBit byte = 0x41 ) const msgMinLen = 9 const msgUartMinLen = msgMinLen + 3 var ( mtSvcTermRegister = []byte{0x81, 0x00} mtSvcCommonResponse = []byte{0x80, 0x01} mtSvcQueryUart = []byte{0x89, 0x00} mtSvcIo = []byte{0x85, 0x00} msSvcInitQueryUartSampleTime = []byte{0x89, 0x01} msSvcInitQueryUart = []byte{0x89, 0x02} msSvcDeleteQueryUart = []byte{0x89, 0x03} ) const ( msgTypeStart = 1 msgHaedStart = 3 msgSnStart = 5 msgBodyStart = 7 mdbsStart = 8 ) func parseMsg(msg []byte) string { msgType := ut.BytesToHexStr(msg[msgTypeStart:msgHaedStart]) msgHeader := ut.BytesToHexStr(msg[msgHaedStart:msgSnStart]) msgSn := ut.BytesToHexStr(msg[msgSnStart:msgBodyStart]) msgBody := ut.BytesToHexStr(msg[msgBodyStart : len(msg)-2]) return fmt.Sprint("TYPE=>", msgType, "HEAD=>", msgHeader, "SN=>", msgSn, "BODY=>", msgBody) } func getPosition(msg []byte) (x, y float64, ok bool) { bd := msg[msgBodyStart : len(msg)-2] if len(bd) < 16 { return 0, 0, false } //fmt.Println("len", len(bd)) //fmt.Println("bx", bx) y = calcPosition(bd[8:12]) //fmt.Println("by", by) x = calcPosition(bd[12:16]) return x, y, true } func calcPosition(msg []byte) float64 { r64 := float64(binary.BigEndian.Uint32(msg)) / 1000000 return r64 } func getCheckSum(msg []byte) byte { var checkSum byte = 0 for i := 1; i < len(msg)-2; i++ { checkSum = checkSum ^ msg[i] } return checkSum }