/// <summary> /// 上传升级文件到设备 /// </summary> /// <param name="destid"></param> /// <param name="data"></param> /// <returns></returns> public static byte[] UpGradeFile(byte destid, WriteFile.Parameter para) { byte[] donedata; byte[] tempdata = GetData(para, (int)FileType.UpGradeFile); PrepareData.Msg_Bus _frame = new PrepareData.Msg_Bus(); _frame.flag = PrepareData.BUS_FRAME_FLAG; _frame.srcID = MSGEncoding.srcID; _frame.destID = destid; _frame.msgDir = (byte)MSGEncoding.MsgDir.Request; _frame.msgVer = MSGEncoding.msgVer; _frame.msgType = (byte)MSGEncoding.MsgType.WriteFile; _frame.msgSubType = (byte)MSGEncoding.WriteFile.UpGradeFile; _frame.dataLen = (ushort)tempdata.Length; donedata = PrepareData.MergeMsg(ref _frame, tempdata); return(donedata); }
/// <summary> /// 上传数据文件到设备 /// </summary> /// <param name="destid"></param> /// <param name="data"></param> /// <returns></returns> public static byte[] UpcfgToDev(byte destid, WriteFile.Parameter para) { byte[] donedata; //由于结构体中普通文件和升级文件的名字都会给出,通过getdata的第二参数标定,为0为普通文件(数据和配置) //不再讨论文件类别,只给出编号,相应的文件以包装进类中 //恢复文件类型标准,由于para和fileitem死循环产生 byte[] tempdata = GetData(para, (int)FileType.CfgOrDataFile); PrepareData.Msg_Bus _frame = new PrepareData.Msg_Bus(); _frame.flag = PrepareData.BUS_FRAME_FLAG; _frame.srcID = MSGEncoding.srcID; _frame.destID = destid; _frame.msgDir = (byte)MSGEncoding.MsgDir.Request; _frame.msgVer = MSGEncoding.msgVer; _frame.msgType = (byte)MSGEncoding.MsgType.WriteFile; _frame.msgSubType = (byte)MSGEncoding.WriteFile.UpcfgToDev; _frame.dataLen = (ushort)tempdata.Length; donedata = PrepareData.MergeMsg(ref _frame, tempdata); return(donedata); }
/// <summary> /// 获取数据域,依赖于上次读到的总和为文件开始读取位置,如果读取到数据则说明未读完,继续组文件发送包 /// 如果读到字节数为0,那么说明读取完毕,将本次读到的字节数ref返回,在外判断循环组包结束 /// </summary> /// <returns></returns> public static byte[] GetData(WriteFile.Parameter para, int type) { byte[] ret; //读请求个数的字节 byte[] filedata = new byte[para.writebytes]; //这里读完到之后指针已偏移,默认指定读到的数据能全部发送 para.filehandle.Seek(para.start, SeekOrigin.Begin); ushort writedown = (ushort)para.filehandle.Read(filedata, 0, para.writebytes); //((FileItem)filepool[para.packetnum]).FileIndex += writedown; if (0 != writedown) { if ((int)FileType.UpGradeFile == type) { ret = new byte[writedown + upgradelen]; Array.Copy(para.upgradefilename, ret, upgradelen); Array.Copy(filedata, 0, ret, upgradelen, writedown); } else { ret = new byte[writedown + datacfglen]; byte[] temp = ByteStruct.StructToBytes(para.filename); Array.Copy(temp, ret, datacfglen); Array.Copy(filedata, 0, ret, datacfglen, writedown); } } //当读到0个字节,即已经读到文件结尾的时候,直接返回文件名,为结束帧 else { if (1 == type) { ret = para.upgradefilename; } else { ret = ByteStruct.StructToBytes(para.filename); } } return(ret); }