示例#1
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 { }
        }
示例#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 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 { }
        }
示例#4
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);
     }
 }
示例#5
0
        /// <summary>
        /// 异步接收回调
        /// </summary>
        /// <param name="ar"></param>
        void ReceiveCallback(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);
                LogHelper.WriteLog(oe);
                throw oe;
            }
            catch (SocketException se)
            {
                Console.WriteLine(se);
                LogHelper.WriteLog(se);
                throw se;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                LogHelper.WriteLog(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
                };

                ShowMsg(string.Format("[ {0} ] 来自:{1} 编号: {2} : {3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), tempRemoteEP.ToString(), RouteCode, PubLib.BytesToString(data)));
                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(ReceiveCallback), so);
            }
        }