// Start is called before the first frame update void Start() { //初始化对象 //山和云的对象 LB_11 = GameObject.FindWithTag("BackGround_11").GetComponent <LoopBack>(); LB_12 = GameObject.FindWithTag("BackGround_12").GetComponent <LoopBack>(); LB_13 = GameObject.FindWithTag("BackGround_13").GetComponent <LoopBack>(); LB_21 = GameObject.FindWithTag("BackGround_21").GetComponent <LoopBack>(); LB_22 = GameObject.FindWithTag("BackGround_22").GetComponent <LoopBack>(); LB_23 = GameObject.FindWithTag("BackGround_23").GetComponent <LoopBack>(); LB_31 = GameObject.FindWithTag("BackGround_31").GetComponent <LoopBack>(); LB_32 = GameObject.FindWithTag("BackGround_32").GetComponent <LoopBack>(); LB_33 = GameObject.FindWithTag("BackGround_33").GetComponent <LoopBack>(); LB_41 = GameObject.FindWithTag("BackGround_41").GetComponent <LoopBack>(); LB_42 = GameObject.FindWithTag("BackGround_42").GetComponent <LoopBack>(); LB_43 = GameObject.FindWithTag("BackGround_43").GetComponent <LoopBack>(); LB_51 = GameObject.FindWithTag("BackGround_51").GetComponent <LoopBack>(); LB_52 = GameObject.FindWithTag("BackGround_52").GetComponent <LoopBack>(); LB_53 = GameObject.FindWithTag("BackGround_53").GetComponent <LoopBack>(); LB_61 = GameObject.FindWithTag("BackGround_61").GetComponent <LoopBack>(); LB_62 = GameObject.FindWithTag("BackGround_62").GetComponent <LoopBack>(); LB_63 = GameObject.FindWithTag("BackGround_63").GetComponent <LoopBack>(); //树林的对象 LBFT_11 = GameObject.FindWithTag("small_1").GetComponent <LoopBackForTree>(); LBFT_12 = GameObject.FindWithTag("middle_1").GetComponent <LoopBackForTree>(); LBFT_13 = GameObject.FindWithTag("big_1").GetComponent <LoopBackForTree>(); LBFT_21 = GameObject.FindWithTag("small_2").GetComponent <LoopBackForTree>(); LBFT_22 = GameObject.FindWithTag("middle_2").GetComponent <LoopBackForTree>(); LBFT_23 = GameObject.FindWithTag("big_2").GetComponent <LoopBackForTree>(); //地面的对象 LFG_11 = GameObject.FindWithTag("ground_1").GetComponent <LoopForGround>(); LFG_12 = GameObject.FindWithTag("ground_11").GetComponent <LoopForGround>(); LFG_21 = GameObject.FindWithTag("ground_2").GetComponent <LoopForGround>(); LFG_22 = GameObject.FindWithTag("ground_22").GetComponent <LoopForGround>(); //小麦的对象 wh = GameObject.FindWithTag("wheat").GetComponent <wheat>(); //黄河的对象 mR = GameObject.FindWithTag("river").GetComponent <moveRiver>(); //河里的水的对象 RMD = GameObject.FindWithTag("water").GetComponent <RiverMoveDown>(); // sT = GameObject.FindWithTag("stone").GetComponent <stone>(); sT_2 = GameObject.FindWithTag("stone_2").GetComponent <stone>(); // th_1 = GameObject.FindWithTag("thorns_1").GetComponent <thorns>(); th_2 = GameObject.FindWithTag("thorns_2").GetComponent <thorns>(); // mU = GameObject.FindWithTag("peach").GetComponent <moveUp>(); }
/// <summary> /// 如果是多包数据,那么分包后放到list中 /// 1、当需要组包时,传入参数,分析、使用switch区分,组装不同的帧 /// 2、类型和子类型的方法分别用于区分和组装数据域外数据 /// 3、针对不同的功能组织不同的数据域,最后合并 /// 4、ref donewrite参数为写文件准备,返回本次组包本地读取的字节数 /// </summary> /// <param name="destid"></param> /// <param name="msgtype"></param> /// <param name="msgsubtype"></param> /// <returns>错误代码另外给出</returns> public static PacketRet Packetization(Compare example, object data) { byte[] frame = null; PacketRet tempret = new PacketRet(); //packetization的错误代码定义另外给出 tempret.errorcode = 0; switch (example.msgType) { //读操作单元 case (byte)MSGEncoding.MsgType.ReadUnit: { switch (example.msgSubType) { //读数据 case (byte)MSGEncoding.ReadUint.ReadData: { frame = ReadUnit.ReadData(example.destID, (ushort[])data); break; } //读取配置信息 case (byte)MSGEncoding.ReadUint.Readcfg: { frame = ReadUnit.Readcfg(example.destID, (ushort[])data); break; } //查询设备状态 case (byte)MSGEncoding.ReadUint.GetDevStatus: { frame = ReadUnit.GetDevStatus(example.destID, (ushort[])data); break; } default: { //MessageBox.Show("preparedata-packetzation:错误子代码!"); tempret.errorcode = 1; } break; } break; } //写操作单元 case (byte)MSGEncoding.MsgType.WriteUnit: { //为适应前台,测试其他成功后添加转换 Dictionary <ushort, object> data_temp = WriteUnit.TransDict(((Dictionary <string, object>)data)); switch (example.msgSubType) { //控制设备(应该是状态/控制量) case (byte)MSGEncoding.WriteUint.ControlDev: { frame = WriteUnit.ControlDev(example.destID, data_temp); break; } //设置配置信息(配置量) case (byte)MSGEncoding.WriteUint.Setcfg: { frame = WriteUnit.Setcfg(example.destID, data_temp); break; } //写数据(数据量) case (byte)MSGEncoding.WriteUint.WriteData: { frame = WriteUnit.WriteData(example.destID, data_temp); break; } default: { //MessageBox.Show("preparedata-packetzation:错误子代码!"); tempret.errorcode = 1; } break; } break; } //读文件 case (byte)MSGEncoding.MsgType.ReadFile: { switch (example.msgSubType) { //读取指定配置文件,从x位置开始的n个字节数据 case (byte)MSGEncoding.ReadFile.ReadcfgFile: { frame = ReadFile.ReadcfgFile(example.destID, (ReadFile.Parameter)data); break; } //读取指定数据文件,从x位置开始的n个字节数据 case (byte)MSGEncoding.ReadFile.ReadDataFile: { frame = ReadFile.ReadDataFile(example.destID, (ReadFile.Parameter)data); break; } default: { //MessageBox.Show("preparedata-packetzation:错误子代码!"); tempret.errorcode = 1; } break; } break; } //写文件到设备 //注意:传入的para中的filehandle已经初始化 case (byte)MSGEncoding.MsgType.WriteFile: { switch (example.msgSubType) { //上传配置文件到设备,并作为最新配置 case (byte)MSGEncoding.WriteFile.UpcfgToDev: { frame = WriteFile.UpcfgToDev(example.destID, (WriteFile.Parameter)data); break; } //上传数据文件到设备 case (byte)MSGEncoding.WriteFile.UpDataToDev: { frame = WriteFile.UpDataToDev(example.destID, (WriteFile.Parameter)data); break; } //上传升级文件 case (byte)MSGEncoding.WriteFile.UpGradeFile: { frame = WriteFile.UpGradeFile(example.destID, (WriteFile.Parameter)data); break; } default: { //MessageBox.Show("preparedata-packetzation:错误子代码!"); tempret.errorcode = 1; } break; } break; } //通信测试 case (byte)MSGEncoding.MsgType.LoopBack: { //LoopBack通讯测试用 frame = LoopBack.LoopTest(example.destID, (ushort)data); break; } //其他(向设备查询Error Code的详细解释) case (byte)MSGEncoding.MsgType.GetErrorInfo: { //向设备查询Error Code的详细解释 frame = GetErrorInfo.ExplainError(example.destID, (ushort)data); break; } //文件信息查询 case (byte)MSGEncoding.MsgType.GetFileInfo: { switch (example.msgSubType) { //取得配置文件名称(=最新更新时间)及文件大小 case (byte)MSGEncoding.GetFileInfo.GetcfgFile: { frame = GetFileInfo.GetcfgFile(example.destID, (GetFileInfo.Parameter)data); break; } //按日期范围查询数据文件名及文件大小 case (byte)MSGEncoding.GetFileInfo.GetFileByRange: { frame = GetFileInfo.GetFileByRange(example.destID, (GetFileInfo.Parameter)data); break; } //查询离指定日期最近的x个数据文件名及文件大小 case (byte)MSGEncoding.GetFileInfo.GetXFileByLately: { frame = GetFileInfo.GetXFileByLately(example.destID, (GetFileInfo.Parameter)data); break; } default: { //MessageBox.Show("preparedata-packetzation:错误子代码!"); tempret.errorcode = 1; } break; } break; } //读缓冲区 case (byte)MSGEncoding.MsgType.ReadBuffer: { switch (example.msgSubType) { //读CO2气体的采样缓存 case (byte)MSGEncoding.ReadBuffer.ReadCO2Cache: { frame = ReadBuffer.ReadCO2Cache(example.destID, (ReadBuffer.Parameter)data); break; } //读六种气体的采样缓存 case (byte)MSGEncoding.ReadBuffer.ReadGasCache: { frame = ReadBuffer.ReadGasCache(example.destID, (ReadBuffer.Parameter)data); break; } //读H2O采样数据缓存 case (byte)MSGEncoding.ReadBuffer.ReadH2OCache: { frame = ReadBuffer.ReadH2OCache(example.destID, (ReadBuffer.Parameter)data); break; } default: { //MessageBox.Show("preparedata-packetzation:错误子代码!"); tempret.errorcode = 1; } break; } break; } default: { //MessageBox.Show("preparedata-packetzation:数据帧外层类型存在错误!"); tempret.errorcode = 2; } break; } try { //为统一发送和接受帧的对照,添加的标志,这里稳定后挪进所有帧组装方法中 byte[] changeframe = new byte[frame.Length + 1]; Array.Copy(frame, 0, changeframe, 0, 8); changeframe[8] = packetindex; Array.Copy(frame, 8, changeframe, 9, frame.Length - 8); tempret.packetnum = packetindex; tempret.packetdata = frame; //通过自动溢出归零 packetindex++; } catch (Exception ep) { //组好的原始帧中插入帧编码存在错误! //MessageBox.Show("preparedata-packetization:"+ep.ToString()); tempret.errorcode = 3; } return(tempret); }