public byte[] CreateHeader(UpdateFileParam FilePar, int nSegmentSize,int nUpdateDevCnt) { byte[] header = new byte[15];//无CRC16和转义 header[0] = 0xFA; header[1] = 0x00; header[2] = 0x00; header[3] = 0; //帧号 byte[] validLen = UInt16ToBcd((ushort)9);//命令字2D有效数据长度9 //BCD header[4] = validLen[0]; header[5] = validLen[1]; //有效数据 header[6] = 0x2D; header[7] = (byte)FilePar.eType; header[8] = nUpdateDevCnt > 0 ? (byte)2 : (byte)0; byte[] FileSize = BitConverter.GetBytes(FilePar.FileLen); header[9] = FileSize[3]; header[10] = FileSize[2]; header[11] = FileSize[1]; header[12] = FileSize[0]; byte[] segmentSize = BitConverter.GetBytes((UInt16)nSegmentSize); header[13] = segmentSize[1]; header[14] = segmentSize[0]; return header; }
public byte[] CreateBody(UpdateFileParam FilePar, int nOffset,int nBodyLen) { m_nFrameId++; if (m_nFrameId >= 0x7F) m_nFrameId = 0; int nPacketLen = nBodyLen + 12;//无CRC16和转义 byte[] bodyData = new byte[nPacketLen]; bodyData[0] = 0xFA; bodyData[1] = 0x00; bodyData[2] = 0x00; bodyData[3] = (byte)m_nFrameId; //帧号 //命令字2E有效数据长度 bodyData[4] = 0; bodyData[5] = 0; //有效数据 bodyData[6] = 0x2E; bodyData[7] = (byte)FilePar.eType; byte[] byteOffset = BitConverter.GetBytes(nOffset); bodyData[8] = byteOffset[3]; bodyData[9] = byteOffset[2]; bodyData[10] = byteOffset[1]; bodyData[11] = byteOffset[0]; byte[] fileData = ReadBinFile(FilePar.strFileFullPath,nOffset,nBodyLen); Buffer.BlockCopy(fileData, 0, bodyData, 12, fileData.Length); //最后一包使用实际长度 if(fileData.Length == nBodyLen) { byte[] validLen = UInt16ToBcd((ushort)(nBodyLen + 6));//命令字2E有效数据长度 bodyData[4] = validLen[0]; bodyData[5] = validLen[1]; return bodyData; } else { byte[] validLen = UInt16ToBcd((ushort)(fileData.Length + 6));//命令字2E有效数据长度 bodyData[4] = validLen[0]; bodyData[5] = validLen[1]; int nRealLen = 12 + fileData.Length; byte[] retBuffer = new byte[nRealLen]; Buffer.BlockCopy(bodyData, 0, retBuffer, 0, nRealLen); return retBuffer; } }
private void ReadUpdateFileVersion() { lstUpdateFile.Clear(); string[] updatefiles = Directory.GetFiles(Application.StartupPath, "*.bin",SearchOption.TopDirectoryOnly); foreach (string strFileName in updatefiles) { //文件名固定n-ver.bin(如键盘程序为2-K.ZHY.1.23.48.bin) int nPos = strFileName.LastIndexOf("\\") + 1; string strFileParse = strFileName.Substring(nPos, strFileName.LastIndexOf(".bin") - nPos); int nSplit = strFileParse.IndexOf("-"); if (nSplit != -1) { int nType = Convert.ToInt32(strFileParse.Substring(0, nSplit)); string strVersion = strFileParse.Substring(nSplit + 1, strFileParse.Length - 2); if (nType >= 1 && nType <= 4) { UpdateFileParam par = new UpdateFileParam(); FileInfo fileInfo = new FileInfo(strFileName); par.eType = (UpdateFileType)nType; par.strFileFullPath = strFileName; par.FileLen = Convert.ToInt32(fileInfo.Length); par.FileVersion = strVersion; lstUpdateFile.Add(par); } } } }
private byte[] GetVerData(UpdateFileParam fileInfo) { if (string.IsNullOrEmpty(fileInfo.FileVersion)) return null; int nLen = fileInfo.FileVersion.Length + 2; byte[] retVal = new byte[nLen]; retVal[0] = (byte)fileInfo.eType; retVal[1] = (byte)fileInfo.FileVersion.Length; byte[] VerData = Encoding.ASCII.GetBytes(fileInfo.FileVersion); Buffer.BlockCopy(VerData, 0, retVal, 2, VerData.Length); return retVal; }