Exemplo n.º 1
0
 public FrameData(byte[] data)
 {
     recvData = data;
     try
     {
         if (CRC.CRCCheck(data))
         {
             isComplite    = true;
             frameType     = (FrameType)data[5];
             routeAddress  = PubLib.Hex2String(data[7]) + PubLib.Hex2String(data[6]);
             commandType   = (CommandType)data[8];
             commandLength = (int)data[9];
             commandData   = data.Skip(10).Take(commandLength).ToArray();
         }
         else
         {
             LogHelper.WriteLog("数据接收错误", data);
             PubLib.当前错误指令数++;
         }
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog("数据接收错误", data);
         LogHelper.WriteLog(ex);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// socket数据接收异步回调
        /// </summary>
        /// <param name="ar"></param>
        void RecvCallBack(IAsyncResult ar)
        {
            allDone.Set();

            StateObject so           = (StateObject)ar.AsyncState;
            IPEndPoint  sender       = new IPEndPoint(IPAddress.Any, 0);
            EndPoint    tempRemoteEP = (EndPoint)sender;

            int readBytes = 0;

            try
            {
                readBytes = so.socket.EndReceiveFrom(ar, ref tempRemoteEP);
            }
            catch (ObjectDisposedException oe)
            {
                Console.WriteLine(oe);
                throw oe;
            }
            catch (SocketException se)
            {
                Console.WriteLine(se);
                throw se;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                // 获得接收失败信息
                throw e;
            }

            if (readBytes > 0)
            {
                byte[] mybytes = new byte[readBytes];
                Array.Copy(so.buffer, mybytes, readBytes);
                //获取DTU设备的定义码
                byte[] code      = mybytes.Take(4).ToArray();
                string RouteCode = PubLib.Hex2String(code[0]) + PubLib.Hex2String(code[1]) + PubLib.Hex2String(code[2]) + PubLib.Hex2String(code[3]);
                //获取接收的真实数据
                byte[] data = mybytes.Skip(4).Take(readBytes - 4).ToArray();
                //创建接收处理对象
                RecvObject o = new RecvObject()
                {
                    Code        = RouteCode,
                    Data        = data,
                    RemotePoint = tempRemoteEP
                };
                ProcessCommad(o);

                //Thread t = new Thread(new ParameterizedThreadStart(ProcessCommad));
                //t.IsBackground = true;
                //t.Name = code + "|" + tempRemoteEP.ToString() + "处理线程";
                //t.Start((object)o);
                //收到数据处理
                so.socket.BeginReceiveFrom(so.buffer, 0, StateObject.BUF_SIZE, SocketFlags.None, ref tempRemoteEP, new AsyncCallback(RecvCallBack), so);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 数据接收处理线程
        /// </summary>
        /// <param name="o"></param>
        void ProcessCommad(object o)
        {
            RecvObject recv = o as RecvObject;

            if (recv != null)
            {
                Console.WriteLine("收到:" + PubLib.Hex2String(recv.Data));
                DateTime time = DateTime.Now;

                FrameData f = new FrameData(recv.Data);
                SetRouteInfo(recv.Code, f.routeAddress, recv.RemotePoint);
                f.Code = recv.Code;
                DataAccess ac = new DataAccess();
                DataTable  dt = ac.ExecuteQueryReturnTable("select * from Base_DeviceInfo where DeviceType='0' and Status<>2 and Token='" + f.Code + "'"); //绑定所有空闲或正常的控制设备
                if (dt.Rows.Count > 0)
                {
                    PubLib.当前总指令数++;
                    //控制器合法
                    switch (f.commandType)
                    {
                    case CommandType.机头网络状态报告:
                    {
                        Recv机头网络状态报告 cmd = new Recv机头网络状态报告(f);
                        SendData(cmd.SendData, recv.RemotePoint);
                    }
                    break;

                    case CommandType.机头地址动态分配:
                    {
                        Recv机头地址动态分配 cmd = new Recv机头地址动态分配(f);
                        SendData(cmd.SendData, recv.RemotePoint);
                    }
                    break;

                    case CommandType.游戏机参数申请:
                    {
                        Recv游戏机参数申请 cmd = new Recv游戏机参数申请(f);
                        SendData(cmd.SendData, recv.RemotePoint);
                    }
                    break;

                    case CommandType.远程投币上分指令应答:
                    {
                        int SN = BitConverter.ToUInt16(f.recvData, 6);          //用于清除重发队列
                    }
                    break;

                    case CommandType.远程被动退分解锁指令应答:
                    {
                        //远程退分指令应答
                    }
                    break;

                    case CommandType.机头卡片报警指令:
                    {
                        Recv机头卡片报警指令 cmd = new Recv机头卡片报警指令(f, DateTime.Now);
                        SendData(cmd.SendData, recv.RemotePoint);
                    }
                    break;

                    case CommandType.IC卡模式退币数据:
                    case CommandType.IC卡模式投币数据:
                    {
                        Recv卡头进出币数据 cmd = new Recv卡头进出币数据(f, DateTime.Now);
                        SendData(cmd.SendData, recv.RemotePoint);
                    }
                    break;
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 数据接收处理线程
        /// </summary>
        /// <param name="o"></param>
        void ProcessCommad(object o)
        {
            RecvObject recv = o as RecvObject;

            if (recv != null)
            {
                Console.WriteLine("收到:" + PubLib.Hex2String(recv.Data));
                DateTime time = DateTime.Now;

                FrameData f = new FrameData(recv.Data);
                f.Code = recv.Code;
                DataAccess ac = new DataAccess();
                DataTable  dt = ac.ExecuteQueryReturnTable("select * from Base_DeviceInfo where DeviceType='0' and Status<>2 and Token='" + f.Code + "'"); //绑定所有空闲或正常的控制设备
                if (dt.Rows.Count > 0)
                {
                    PubLib.当前总指令数++;
                    //控制器合法
                    switch (f.commandType)
                    {
                    case CommandType.机头网络状态报告:
                    {
                        Recv机头网络状态报告 cmd = new Recv机头网络状态报告(f);
                        SendData(cmd.SendData);
                    }
                    break;

                    case CommandType.机头地址动态分配:
                    {
                        Recv机头地址动态分配 cmd = new Recv机头地址动态分配(f);
                        SendData(cmd.SendData);
                    }
                    break;

                    case CommandType.游戏机参数申请:
                    {
                        Recv游戏机参数申请 cmd = new Recv游戏机参数申请(f);
                        SendData(cmd.SendData);
                    }
                    break;
                        //case CommandType.IC卡模式会员余额查询:
                        //    {
                        //        RecvIC卡模式会员余额查询 cmd = new RecvIC卡模式会员余额查询(f, time);
                        //        if (cmd.IsSuccess)
                        //        {
                        //            object obj = null;
                        //            int res = CheckRepeat(f.Code, cmd.机头地址, cmd.IC卡号码, CommandType.IC卡模式投币数据, ref obj, cmd.流水号);
                        //            if (res == 0)
                        //            {
                        //                //未处理指令
                        //                //处理指令
                        //                cmd.Process();
                        //                //加入重复性检查队列
                        //                InsertRepeat(f.Code, cmd.机头地址, cmd.IC卡号码, CommandType.IC卡模式投币数据, CommandType.IC卡模式投币数据应答, cmd.应答数据, cmd.流水号, time);
                        //            }
                        //            else if (res == 1)
                        //            {
                        //                //已处理过指令
                        //                cmd.应答数据 = (AskIC卡模式会员余额查询)obj;
                        //                PubLib.当前IC卡查询重复指令数++;
                        //            }
                        //            else
                        //            {
                        //                //遇到错误退出
                        //                return;
                        //            }
                        //        }
                        //    }
                        //    break;
                    }
                }
            }
        }