Пример #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);
     }
 }
Пример #2
0
        public static void WriteSNLog(string type, string headAddress, string segment, string sn, byte[] data)
        {
            DateTime d        = DateTime.Now;
            string   filePath = "";
            string   fileName = "";

            filePath = string.Format("log\\SN\\{0}-{1}\\{2}\\{3}", segment, headAddress, d.ToString("yyyy"), d.ToString("yyyy-MM"));
            fileName = d.ToString("yyyy-MM-dd HH") + " .txt";
            try
            {
                Directory.CreateDirectory(filePath);
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true, Encoding.GetEncoding("gb2312"));
                sw.WriteLine("接收时间:" + d.ToString());
                sw.WriteLine("类别:" + type);
                sw.WriteLine("流水号:" + sn);
                if (data != null)
                {
                    sw.WriteLine("原始数据:" + PubLib.BytesToString(data));
                }
                sw.WriteLine();
                sw.Flush();
                sw.Close();
            }
            catch { }
        }
Пример #3
0
        public static void WriteLog(Exception ex, byte[] data)
        {
            DateTime d        = DateTime.Now;
            string   filePath = "";
            string   fileName = "";

            filePath = string.Format("log\\{0}\\{1}", d.ToString("yyyy"), d.ToString("yyyy-MM"));
            fileName = d.ToString("yyyy-MM-dd") + ".txt";
            try
            {
                Directory.CreateDirectory(filePath);
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true, Encoding.GetEncoding("gb2312"));
                sw.WriteLine("错误时间:" + d.ToString());
                sw.WriteLine("错误内容:" + ex.Message);
                if (data != null)
                {
                    sw.WriteLine("数据包内容:" + PubLib.BytesToString(data));
                }
                sw.WriteLine("堆栈信息:" + ex.StackTrace);
                sw.WriteLine();
                sw.Flush();
                sw.Close();
            }
            catch { }
        }
Пример #4
0
        public void 退币信号延时检测指令(string Code, string Address)
        {
            RouteInfo route;

            if (GetRouteInfo(Code, out route))
            {
                if (route.IsOnline)
                {
                    Info.HeadInfo.机头绑定信息 bind = new Info.HeadInfo.机头绑定信息();
                    bind.控制器令牌 = Code;
                    bind.短地址   = Address;
                    Info.HeadInfo.机头信息 h = Info.HeadInfo.GetHeadInfoByShort(bind);
                    if (h != null)
                    {
                        FrameData data = new FrameData();
                        data.commandType  = CommandType.远程被动退分解锁指令;
                        data.routeAddress = route.Segment;
                        List <byte> dataList = new List <byte>();
                        dataList.Add((byte)Convert.ToByte(Address, 16));
                        dataList.Add(0x01);
                        byte[] Send = PubLib.GetFrameDataBytes(data, dataList.ToArray(), CommandType.远程被动退分解锁指令);

                        SendData(Send, route.RemotePoint);
                    }
                }
            }
        }
Пример #5
0
        public void 机头锁定解锁指令(string Code, string Address, bool isLock)
        {
            try
            {
                RouteInfo route;
                if (GetRouteInfo(Code, out route))
                {
                    if (route.IsOnline)
                    {
                        //当前路由器在线
                        FrameData data = new FrameData();
                        data.commandType  = CommandType.机头锁定解锁指令;
                        data.routeAddress = route.Segment;
                        List <byte> dataList = new List <byte>();
                        dataList.Add((byte)Convert.ToByte(Address, 16));
                        dataList.Add((byte)(isLock ? 1 : 0));
                        byte[] Send = PubLib.GetFrameDataBytes(data, dataList.ToArray(), CommandType.机头锁定解锁指令);

                        SendData(Send, route.RemotePoint);
                    }
                }
            }
            catch
            {
                throw;
            }
        }
Пример #6
0
        public void 远程投币上分(string Code, string Address, string ICCardID, int Coins)
        {
            RouteInfo route;

            if (GetRouteInfo(Code, out route))
            {
                if (route.IsOnline)
                {
                    Info.HeadInfo.机头绑定信息 bind = new Info.HeadInfo.机头绑定信息();
                    bind.控制器令牌 = Code;
                    bind.短地址   = Address;
                    Info.HeadInfo.机头信息 h = Info.HeadInfo.GetHeadInfoByShort(bind);
                    if (h != null)
                    {
                        h.常规.当前卡片号 = ICCardID;
                        FrameData data = new FrameData();
                        data.commandType  = CommandType.远程投币上分指令;
                        data.routeAddress = route.Segment;
                        List <byte> dataList = new List <byte>();
                        dataList.Add((byte)Convert.ToByte(Address, 16));
                        dataList.AddRange(BitConverter.GetBytes((UInt16)Coins));
                        dataList.AddRange(BitConverter.GetBytes((UInt16)h.远程投币上分流水号));
                        byte[] Send = PubLib.GetFrameDataBytes(data, dataList.ToArray(), CommandType.远程投币上分指令);

                        SendData(Send, route.RemotePoint);
                    }
                }
            }
        }
Пример #7
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);
            }
        }
Пример #8
0
        public void SendConnect()
        {
            FrameData data = new FrameData();

            data.frameType    = FrameType.命令帧;
            data.routeAddress = "0001";

            byte[] sDat = PubLib.GetFrameDataBytes(data, null, CommandType.网络初始化连接);
            SendData(sDat);
        }
Пример #9
0
        public void 指定路由器复位(string Code)
        {
            RouteInfo route;

            if (GetRouteInfo(Code, out route))
            {
                if (route.IsOnline)
                {
                    byte[]    routeBytes = BitConverter.GetBytes(Convert.ToUInt16(route.Segment, 16));
                    FrameData data       = new FrameData();
                    data.commandType  = CommandType.设置路由器地址;
                    data.routeAddress = "FFFF";
                    byte[] Senddata = PubLib.GetFrameDataBytes(data, routeBytes, CommandType.设置路由器地址);

                    SendData(Senddata, route.RemotePoint);
                }
            }
        }
Пример #10
0
        public static void WritePush(byte[] data, string segment, string headAddress, string pushType, string coin, string sn)
        {
            DateTime d        = DateTime.Now;
            string   filePath = "";
            string   fileName = "";

            filePath = string.Format("log\\SN\\{0}-{1}\\{2}\\{3}", segment, headAddress, d.ToString("yyyy"), d.ToString("yyyy-MM"));
            fileName = d.ToString("yyyy-MM-dd HH") + " PUSH.txt";
            try
            {
                Directory.CreateDirectory(filePath);
                StreamWriter sw = new StreamWriter(filePath + "\\" + fileName, true, Encoding.GetEncoding("gb2312"));
                sw.WriteLine("接收时间:" + d.ToString());
                sw.WriteLine("原始数据:" + PubLib.BytesToString(data));
                sw.WriteLine("投币类别:" + pushType);
                sw.WriteLine("投币数:" + coin);
                sw.WriteLine("投币流水:" + sn);
                sw.WriteLine();
                sw.Flush();
                sw.Close();
            }
            catch { }
        }
Пример #11
0
        public void 设置机头长地址(string Code, string MCUID)
        {
            RouteInfo route;

            if (GetRouteInfo(Code, out route))
            {
                if (route.IsOnline)
                {
                    List <byte> send = new List <byte>();
                    send.AddRange(PubLib.StringToByte(MCUID));
                    List <byte> sendRe = new List <byte>();
                    //位置调换,低位在前
                    for (int i = send.Count - 1; i >= 0; i--)
                    {
                        sendRe.Add(send[i]);
                    }
                    FrameData data = new FrameData();
                    data.routeAddress = "FFFF";
                    byte[] Senddata = PubLib.GetFrameDataBytes(data, sendRe.ToArray(), CommandType.设置机头长地址);

                    SendData(Senddata, route.RemotePoint);
                }
            }
        }
Пример #12
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;
                    }
                }
            }
        }
Пример #13
0
 public void SendData(byte[] data, EndPoint point)
 {
     try
     {
         ShowMsg(string.Format("[ {0} ] 发送:{1} : {2} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), point.ToString(), PubLib.BytesToString(data)));
         client.SendTo(data, point);
     }
     catch (Exception ex)
     {
         LogHelper.WriteLog(ex);
     }
 }
Пример #14
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;
                    }
                }
            }
        }