private void btnTestCOMTCP_Click(object sender, EventArgs e) { CRC.CRC8 crc = new CRC.CRC8(); int dataStartIndex = FileTransferProtocol.CommandDataIndex; //测试:准备发送新的文件 string filename = @"c:\abc.txt"; byte[] FileInfo = FileTransferProtocol.GetFrameFileInfo(255 + 256, filename); //帧头 Trace.Assert(FileInfo[0] == FileTransferProtocol.FrameHead); //crc //Trace.Assert(FileInfo[1] == crc); //命令字 Trace.Assert(FileInfo[1] == 0x00); //文件长度 Trace.Assert(FileInfo[2] == 0xFF); Trace.Assert(FileInfo[3] == 0x01); Trace.Assert(FileInfo[4] == 0); Trace.Assert(FileInfo[5] == 0); byte[] bs = System.Text.Encoding.Default.GetBytes(filename); Trace.Assert(FileInfo[6] == bs[0]); Trace.Assert(FileInfo[7] == bs[1]); //帧偏移 Trace.Assert((FileInfo[FileInfo.Length - 4] + FileInfo[FileInfo.Length - 3] * 256) == (FileInfo.Length - 1)); Trace.Assert(FileInfo[FileInfo.Length - 2] == 0x7F); Trace.Assert(FileInfo[FileInfo.Length - 1] == 0xFF); byte[] data = new byte[] { 0x01, 0x02, 0xFF, 0xEF, 0xCC, 0xDD, 0x00, 0xAA }; byte[] FileData = FileTransferProtocol.GetFrameFileData(0x01AABBFF, data); //帧头 Trace.Assert(FileData[0] == FileTransferProtocol.FrameHead); //帧偏移 Trace.Assert((FileData[FileData.Length - 3] * 256 + FileData[FileData.Length - 4]) == (FileData.Length - 1)); //命令字 Trace.Assert(FileData[1] == 0x1); //文件偏移 Trace.Assert(FileData[2] == 0xFF); Trace.Assert(FileData[3] == 0xBB); Trace.Assert(FileData[4] == 0xAA); Trace.Assert(FileData[5] == 0x01); //文件数据 Trace.Assert(FileData[6] == 0x01); Trace.Assert(FileData[7] == 0x02); Trace.Assert(FileData[8] == 0xFF); Trace.Assert(FileData[9] == 0xEF); Trace.Assert(FileData[10] == 0xCC); Trace.Assert(FileData[11] == 0xDD); //crc //byte[] x = new byte[] { 0x02, 0xFF, 0xBB, 0xAA, 0x01, 0x01, 0x02, 0xFF, 0xEF, 0xCC, 0xDD, 0x00, 0xAA, FileData[FileData.Length - 3], FileData[FileData.Length - 2] }; //Trace.Assert(FileData[1] == crc.Crc(x, 0, x.Length)); Trace.Assert(FileData[FileData.Length - 1] == 0x7F); //文件发送完毕 byte[] f = FileTransferProtocol.GetFrameFileSendFinish(); //帧头 Trace.Assert(f[0] == FileTransferProtocol.FrameHead); //帧偏移 Trace.Assert((f[f.Length - 3] * 256 + f[f.Length - 4]) == f.Length - 1); //命令字 Trace.Assert(f[1] == 0x02); //crc //Trace.Assert(f[1] == crc.Crc(f, dataStartIndex, f.Length - 3)); Trace.Assert(f[f.Length - 1] == 0x7F); //表示可以新的文件 byte[] a = FileTransferProtocol.GetFrameWhatIsYourMNandVersion(); TestCmdFrame(a, 0xFD, crc); //表示已经接收数据 //byte[] Ac = FileTransferProtocol.GetFrameMNAndVersion(1,"123"); //TestCmdFrame(Ac, 0xFE, crc); //表示已经保存文件完毕 byte[] s = FileTransferProtocol.GetFrameCloseSocket(); TestCmdFrame(s, 0xFF, crc); }