package semi_physical_train import ( "encoding/hex" "fmt" "joylink.club/bj-rtsts-server/const/balise_const" "joylink.club/bj-rtsts-server/third_party/message" "strings" "testing" "time" ) const ( WAIT_FF_C1 = 0x00 WAIT_FF_C2 = 0x01 WAIT_NO_FF = 0x02 WAIT_FF_POS = 0x03 ABNORMAL_STATE = 0x04 /*FFFE数据接收状态*/ COM_WAIT_START_FF = 0x00 COM_WAIT_START_FE = 0x01 COM_WAIT_STOP_FF = 0x02 COM_WAIT_STOP_FD = 0x03 ) // TranslateFromFFFE 将经过FFFE转义处理后的数据(已经去掉了头FFFE及尾FFFD)还原为原始数据。 func TranslateFromFFFE(pSrc []byte) ([]byte, uint16) { var ( srcPos, tgtPos, nonFFCount, nextFFPos uint16 char uint8 state int ) if len(pSrc) == 0 { return nil, 0 // 入参错误 } pTgt := make([]byte, len(pSrc)) // 预分配与输入等长的缓冲区 tgtPos = 0 state = WAIT_FF_C1 nonFFCount = 0 for srcPos = 0; srcPos < uint16(len(pSrc)); srcPos++ { char = pSrc[srcPos] switch state { case WAIT_FF_C1: if char == 0xFF { pTgt[tgtPos] = char tgtPos++ state = WAIT_FF_C2 } else { pTgt[tgtPos] = char tgtPos++ } case WAIT_FF_C2: if char == 0xFD || char == 0xFE || char == 0xFF { state = ABNORMAL_STATE } else { if char == 0 { state = WAIT_NO_FF } else if char == 1 { pTgt[tgtPos] = 0xFF tgtPos++ state = WAIT_FF_C1 } else { nextFFPos = srcPos + uint16(char) - 1 state = WAIT_FF_POS } } case WAIT_NO_FF: nonFFCount++ if char == 0xFF && nonFFCount < 252 { state = ABNORMAL_STATE } else { pTgt[tgtPos] = char tgtPos++ if nonFFCount == 252 { nonFFCount = 0 state = WAIT_FF_C1 } } case WAIT_FF_POS: if char == 0xFF { state = ABNORMAL_STATE } else { pTgt[tgtPos] = char tgtPos++ if srcPos == nextFFPos { pTgt[tgtPos] = 0xFF tgtPos++ state = WAIT_FF_C1 } } default: state = ABNORMAL_STATE } if state == ABNORMAL_STATE { tgtPos = 0 break } } // 退出时的状态判断 if state == WAIT_FF_C2 || state == WAIT_FF_POS { tgtPos = 0 } return pTgt[:tgtPos], tgtPos } func TestTss(t *testing.T) { ss := "fffee601804f004d00921d21002a00ea014fef63995bff009122ce0000000000000000000000000000211d90000000000000000000007b07310379cefffd" ss = "fffee601804f004d00444a1c002a00473bbfa11d4b3b029122a800000000000000000000000000001c4a42000000000000000000008970492fff00d0fffd" dd, _ := hex.DecodeString(ss) frameType, dataText, _ := message.BtmVobcDecode(dd) if frameType == message.COMMAND_TYPE { fmt.Println("1111111111111111") } else if frameType == message.REQUEST_TYPE { req := &message.BtmVobcReq{} req.Decode(dataText) fmt.Println("22222222222222222222") } else { fmt.Println("zzzzzzzzzzzzzzzzzzzzz") } } func TestFffe(t *testing.T) { var testUserBtmMsg = "90007F8181B60B10183280003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" userMsg, _ := hex.DecodeString(testUserBtmMsg) msg := &message.BtmVobcMessage{FontTtl: 5, BtmStatus: 0x00, DecodeTime: 10, BackTtl: 4, BtmMsg: userMsg, ResponseTime: 10, VobcLifeNum: 123, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: 123}} sendData := msg.Encode() fmt.Println(len(sendData), hex.EncodeToString(sendData)) } func TestFffe2(t *testing.T) { var testUserBtmMsg = strings.Repeat("00", balise_const.UserTelegramByteLen) userMsg, _ := hex.DecodeString(testUserBtmMsg) msg2 := &message.BtmVobcMsgFree{BtmStatus: 0x00, WorkTemperature: 10, Fun1: uint16(0), Fun2: uint16(0), Fun3: uint16(0), Fun4: uint16(0), //FreeMsg: freeMsg, RespTime: 20, VobcLifeNum: lifeNum, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: autoId}, MsgSerial: newMsg} FreeMsg: userMsg, RespTime: 20, VobcLifeNum: 2266113, BaseBtmVobc: message.BaseBtmVobc{AutoIdFrame: 123}, MsgSerial: message.GetAutoMessageId()} sendData := msg2.Encode() fmt.Println(len(sendData), hex.EncodeToString(sendData)) } func TestDecode232(t *testing.T) { var dd uint32 = 888888888 fmt.Println(dd) fmt.Println(uint16(dd)) ss := "9287a8000511006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a0023438bf4c27929" ss = "fffe9287d300056f006464000a000000000000000000000490007f8181b60b10183280003fff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff01ff000a00221eedf0be28e9fffd" userMsg, _ := hex.DecodeString(ss) arr, _ := TranslateFromFFFE(userMsg) fmt.Println(arr) fmt.Println(hex.EncodeToString(arr)) } func TestDocode(t *testing.T) { fmt.Println(time.Microsecond * 200) ss := "fffee601804f004d0032d8e1002a006aff0020fc60316e299122ea0000000000000000000000000000e1d83000000000000000000000768888cc4961fffd" cfs, _ := hex.DecodeString(ss) frameType, dataText, err := message.BtmVobcDecode(cfs) fmt.Println(frameType) fmt.Println(hex.EncodeToString(dataText)) fmt.Println(err) } func TestDecode2311(t *testing.T) { var data uint16 // 初始化为0 // 将第0, 1, 2, 3位设置为1(二进制中的1111) data |= (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) // 第4-5位设置为0(不需要操作,因为初始就是0) // 但如果需要明确设置,可以这样做: // data &= ^((1 << 4) | (1 << 5)) // 使用按位取反和按位与来清除这些位 // 第6-7位设置为3(二进制中的11) data |= (3 << 6) // 打印结果 fmt.Printf("The modified data is: %04X\n", data) fmt.Printf("The modified data is: %b\n", data) }