Exemple #1
0
        /// <summary>
        /// 重写,内部数据处理单独封装为方法,一些重复处理在switch外完成
        /// </summary>
        /// <param name="unit"> 操作单元数组</param>
        /// <param name="comtemp">发送信息缓存</param>
        /// <param name="b_recvframe">接受到的数据字节数组</param>
        /// <param name="errornum">请求操作单元的错误数</param>
        /// <param name="error">错误列表</param>
        /// <returns></returns>
        public static Dictionary <ushort, object> MsgHandle(ushort[] unit, PrepareData.Compare comtemp, byte[] b_recvframe,
                                                            ref ushort errornum, Dictionary <ushort, ushort> error)
        {
            Dictionary <ushort, object> retinfo = new Dictionary <ushort, object>();

            switch (HandleData.TypeofFrame(b_recvframe).frametype)
            {
            //接收的是长帧
            case 1:
            {
                //接收数据的拆分
                byte[] recvframeout = new byte[b_recvframe.Length - PrepareData.BUS_FRAME_MINLEN - 2];
                Array.Copy(b_recvframe, PrepareData.BUS_FRAME_MINLEN, recvframeout, 0,
                           b_recvframe.Length - PrepareData.BUS_FRAME_MINLEN - 2);
                //传入的字节数组只包括执行状态字和返回数据(除CRC外)
                TransInfo(recvframeout, unit, retinfo, ref errornum, error);

                #region  从右边去第一不方便,反相的对应取
                //for (int i = 0; i < basebyte; i++)
                //{
                //    byte low = sendframeout[sendframeout.Length - 2 * (i + 2)];
                //    byte high = sendframeout[sendframeout.Length - 2 * (i + 2) + 1];
                //    ushort temp = (ushort)(((ushort)(high << 8)) | low);
                //    //读最后一位执行状态字,看是否有错误,有错误则加到错误列表,并错误数加1
                //    //没有错误则读取倒数第一个值,与操作单元一起加入数据字典,取值时长度由数据字典截取
                //}
                #endregion
            }
            break;

            //接收帧为短帧
            case 0:
            {
                byte[] onlydata = ((PrepareData.Msg_Bus)ByteStruct.BytesToStruct(b_recvframe,
                                                                                 typeof(PrepareData.Msg_Bus))).data;
                //只有执行状态字和返回数据
                TransInfo(onlydata, unit, retinfo, ref errornum, error);
            }
            break;

            //返回的代码错误
            default:
            {
                //MessageBox.Show("返回的代码错误");
            }
            break;
            }
            return(retinfo);
        }
Exemple #2
0
        protected void BT_Data_Click(object sender, EventArgs e)
        {
            MyDictionary initialize = new MyDictionary();

            BT_Data.Text = "hello";
            IMServerUDP current = new IMServerUDP();

            ushort[] require = { 88, 87, 89, 86, 63, 61, 62, 165, 168, 169, 171, 173, 166, 167, 170, 175, 174, 172 };
            //初步测试期间不加心跳,故未能初始化Define.id_ip_port字典,这里添加以下,实际byte-ipendpoint的映射是在心跳处理中添加
            Define.id_ip_port.Add(0x01, new IPEndPoint(IPAddress.Parse("219.244.93.127"), 9999));
            //MyDictionary.ID_IP[0x01] = "219.244.93.127";
            //MyDictionary.ID_PORT[0x01] = 9999;
            //发送摘要缓冲
            PrepareData.Compare compare = new PrepareData.Compare();
            compare.srcID  = 0x00;
            compare.destID = 0x01;
            //读操作单元的配置参数
            compare.msgType    = (byte)MSGEncoding.MsgType.ReadUnit;
            compare.msgSubType = (byte)MSGEncoding.ReadUint.GetDevStatus;
            compare.msgVer     = MSGEncoding.msgVer;
            compare.msgDir     = (byte)MSGEncoding.MsgDir.Request;
            //由于一个触发可能发送多包,所以编码在packet中组织,哈希入表也在packet中组织
            //触发组包
            byte temp = PrepareData.AddRequire(compare, require);

            //缓冲三秒
            while (!HandleData.hello.readone)
            {
            }
            //为了跳出循环,这里不再复位标志位
            #region 赋值界面
            Dictionary <ushort, object> lady = (Dictionary <ushort, object>)HandleData.hello.result;
            for (int i = 0; i < lady.Count; i++)
            {
                ushort key   = lady.ElementAt(i).Key;
                object value = lady.ElementAt(i).Value;
            }
            #endregion
        }