Exemple #1
0
 private void OnDisConnection(string message, SocketAsyncEventArgs socketAsync, int erorr)
 {
     if (socketAsync.UserToken != null)
     {
         try
         {
             lock (this.clients)
             {
                 TCPClientInfo item = this.clients.Find((TCPClientInfo gsv) => ((TCPClientInfo)socketAsync.UserToken).Host == gsv.Host);
                 this.clients.Remove(item);
             }
         }
         catch
         {
         }
     }
     try
     {
         LogMSG.AddInfo(DateTime.Now.ToString() + " ->终端 [" + socketAsync.AcceptSocket.RemoteEndPoint.ToString() + "] 离线.\r\n", 5);
     }
     catch
     {
     }
     socketAsync.UserToken = null;
 }
Exemple #2
0
 public byte[] Receive()
 {
     byte[] array = BufferManager.PopFirstMatchBuff(this.strA1, this.strA2);
     if (array.Length > 0)
     {
         LogMSG.AddInfo(DateTime.Now.ToString() + " ->Recv: " + ByteToHexStr(array) + "\r\n", 0);
     }
     return(array);
 }
Exemple #3
0
 private bool OnConnected(SocketAsyncEventArgs socketAsync)
 {
     try
     {
         LogMSG.AddInfo(DateTime.Now.ToString() + " ->终端 [" + socketAsync.AcceptSocket.RemoteEndPoint.ToString() + "] 连接\r\n", 3);
     }
     catch
     {
     }
     return(true);
 }
Exemple #4
0
        public bool ReadAndWrite(string strCID, string strAFN, string strFN, string strPN, string strData)
        {
            string text = string.Empty;

            try
            {
                text = ((this.OperType == 0) ? ("COM" + (this.m_IOPort as SerialPort).PortNum.ToString() + ":" + (this.m_IOPort as SerialPort).ComSettings) : (this.m_IOPort as Socket).RemoteEndPoint.ToString());
            }
            catch
            {
            }
            LogMSG.AddInfo(string.Concat(new string[]
            {
                "\r\n[",
                text,
                "] [A1=",
                this.strA1,
                ",A2=",
                this.strA2,
                ",AFN=",
                strAFN,
                ",FN=",
                strFN,
                ",PN=",
                strPN,
                "]\r\n"
            }), 1);
            this._framelists.Clear();
            byte[] sendData = this.ToBytes(0, 1, 0, 0, byte.Parse(strCID), strAFN, this.Tp, 1, 1, 0, strFN, strPN, strData);
            this.ParseData(sendData, sendData.Length, ref this.FrameData);
            this.AddFrame(true, this.FrameData);
            this.Clear();
            this.SendData(sendData);

            while (true)
            {
                byte[] array = new byte[0];
                if (!this.ReceiveData(ref array))
                {
                    break;
                }
                this.Clear();
                this.PFC = (byte)(checked (this.PFC + 1) & 255);
                this.ParseData(array, array.Length, ref this.FrameData);
                this.AddFrame(false, this.FrameData);
                if (!FrameInfo.HaveNextFrame(this.FrameData))
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #5
0
        /// <summary>
        /// 基础数据内容校验(检验数据的长度和CS校验)
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public bool Assert_Basic(byte[] frame)
        {
            bool lencheckflag = false;
            bool cscheckflag  = false;

            checked
            {
                if (frame[frame.Length - 1] == 22)
                {
                    int i;
                    for (i = 0; i < frame.Length; i++)
                    {
                        if (frame[i] == 104)
                        {
                            break;
                        }
                    }
                    string strData = frame[i + 1].ToString("X2") + frame[i + 2].ToString("X2");
                    int    dataLen = (int)GetDataLen(strData);
                    if (frame.Length >= dataLen + 8 && frame[i + 5] == 104)
                    {
                        lencheckflag = true;
                    }
                    if (lencheckflag)
                    {
                        int num = 0;
                        for (int j = i + 6; j < frame.Length - 2; j++)
                        {
                            num += (int)frame[j];
                        }
                        if ((byte)(num % 256) == frame[frame.Length - 2])
                        {
                            cscheckflag = true;
                        }
                    }
                }
                if (!lencheckflag)
                {
                    LogMSG.AddInfo("接收帧长度错误!\r\n", 4);
                }
                if (!cscheckflag)
                {
                    LogMSG.AddInfo("接收帧CS校验错误!\r\n", 4);
                }
                return(lencheckflag & cscheckflag);
            }
        }
Exemple #6
0
 public void Close()
 {
     try
     {
         if (this.hComm != -1)
         {
             SerialPort.FlushFileBuffers(this.hComm);
             SerialPort.CloseHandle(this.hComm);
             this.Opened = false;
         }
     }
     catch
     {
         this.Opened = false;
         LogMSG.AddInfo("串口关闭错误!", 2);
     }
 }
Exemple #7
0
 private bool BasicProcessMessage(byte[] data, SocketAsyncEventArgs socketAsync, EndPoint remoteEndPoint, DLT698Message.FrameInfo frameInfo, TCPClientInfo cl)
 {
     if (frameInfo.AFN == AFN.链路接口检测)
     {
         if (frameInfo.FN == "0001")
         {
             byte[] array = DLT698Message.CreateLogOKMessage(frameInfo.A1, frameInfo.A2, frameInfo.FN, frameInfo.PSEQ, this.Tpv);
             LogMSG.AddInfo(string.Concat(new string[] { DateTime.Now.ToString(), " ->终端 [", remoteEndPoint.ToString(), "] 登录:", PubFuncHelper.ByteToHexStr(data), "\r\n", DateTime.Now.ToString(), " ->回应 [", remoteEndPoint.ToString(), "] 登录确认:", PubFuncHelper.ByteToHexStr(array), "\r\n" }), 6);
             socketAsync.AcceptSocket.Send(array);
             socketAsync.UserToken = cl;
             lock (this.clients)
             {
                 if (!this.clients.Exists((TCPClientInfo gcl) => gcl.Host == cl.Host))
                 {
                     this.clients.Add(cl);
                 }
             }
         }
         else
         {
             if (frameInfo.FN == "0003")
             {
                 byte[] array = DLT698Message.CreateLogOKMessage(frameInfo.A1, frameInfo.A2, frameInfo.FN, frameInfo.PSEQ, this.Tpv);
                 LogMSG.AddInfo(string.Concat(new string[] { DateTime.Now.ToString(), " ->终端 [", remoteEndPoint.ToString(), "] 心跳:", PubFuncHelper.ByteToHexStr(data), "\r\n", DateTime.Now.ToString(), " ->回应 [", remoteEndPoint.ToString(), "] 心跳确认:", PubFuncHelper.ByteToHexStr(array), "\r\n" }), 7);
                 socketAsync.AcceptSocket.Send(array);
             }
         }
         return(true);
     }
     else
     {
         if (frameInfo.PRM == "0" & frameInfo.DIR == "1")
         {
             BufferManager.AddDataItem(frameInfo.A1, frameInfo.A2, data);
         }
         else
         {
             if (frameInfo.PRM == "1" & frameInfo.DIR == "1")
             {
                 ActiveReportMsg.AddInfo(data);
             }
         }
         return(false);
     }
     //return frameInfo;
 }
Exemple #8
0
        public void SendData(byte[] Val)
        {
            switch (this.OperType)
            {
            case ComunicationType.Serial:
                (this.m_IOPort as SerialPort).Write(Val);
                break;

            case ComunicationType.TCPMyisClient:
            case ComunicationType.TCPMyisServer:
                if (this.Opened)
                {
                    (this.m_IOPort as Socket).Send(Val);
                }
                break;
            }
            LogMSG.AddInfo(DateTime.Now.ToString() + " ->Send: " + ByteToHexStr(Val) + "\r\n", 0);
        }
Exemple #9
0
        public int Write(byte[] WriteBytes)
        {
            int result = 0;

            if (this.hComm != -1)
            {
                try
                {
                    SerialPort.OVERLAPPED oVERLAPPED = default(SerialPort.OVERLAPPED);
                    SerialPort.WriteFile(this.hComm, WriteBytes, WriteBytes.Length, ref result, ref oVERLAPPED);
                }
                catch
                {
                    LogMSG.AddInfo("串口写入错误!", 2);
                }
            }
            return(result);
        }
Exemple #10
0
        public static string GetDataCellMark(ushort iFN, ushort iPN)
        {
            ushort num = 0;

            checked
            {
                if (iFN == 0)
                {
                    num = iFN;
                }
                else
                {
                    try
                    {
                        num = (ushort)((int)num | (int)((iFN - 1) / 8) << 8);
                        num = (ushort)((int)num | 1 << (int)((iFN - 1) % 8));
                    }
                    catch (Exception ex)
                    {
                        LogMSG.AddInfo("DT Error: " + ex.Message, 2);
                    }
                }
                ushort num2 = 0;
                if (iPN == 0 || iPN == 65535)
                {
                    num2 = iPN;
                }
                else
                {
                    try
                    {
                        num2 = (ushort)((int)num2 | (int)((iPN - 1) / 8 + 1) << 8);
                        num2 = (ushort)((int)num2 | 1 << (int)((iPN - 1) % 8));
                    }
                    catch (Exception ex)
                    {
                        LogMSG.AddInfo("DA Error: " + ex.Message, 2);
                    }
                }
                string text = num.ToString("X4") + num2.ToString("X4");
                return(text.Substring(6, 2) + text.Substring(4, 2) + text.Substring(2, 2) + text.Substring(0, 2));
            }
        }
Exemple #11
0
        public void Open(string IPorSerialsetting, int port)
        {
            switch (this.OperType)
            {
            case 0:
                if (this.m_IOPort == null)
                {
                    this.m_IOPort = new SerialPort();
                }
                (this.m_IOPort as SerialPort).Open(IPorSerialsetting, port);
                break;

            case ComunicationType.TCPMyisServer:
                if (this.m_IOPort == null)
                {
                    LogMSG.AddInfo("无在线终端\r\n", 2);
                    return;
                }
                break;

            case ComunicationType.TCPMyisClient:
                if (!this.Opened)
                {
                    try
                    {
                        (this.m_IOPort as Socket).Connect(IPorSerialsetting, port);
                    }
                    catch (Exception ex)
                    {
                        LogMSG.AddInfo("连接失败-->" + ex.Message + "\r\n", 2);
                        return;
                    }
                }
                break;
            }
            if (this.Opened)
            {
                this.KeepAlive = true;
                ThreadPool.QueueUserWorkItem(new WaitCallback(this.OnReceive), this.m_IOPort);
            }
        }
Exemple #12
0
 public byte[] Read(int NumBytes)
 {
     byte[] array  = new byte[NumBytes];
     byte[] array2 = new byte[0];
     if (this.hComm != -1)
     {
         try
         {
             SerialPort.OVERLAPPED oVERLAPPED = default(SerialPort.OVERLAPPED);
             int num = 0;
             SerialPort.ReadFile(this.hComm, array, NumBytes, ref num, ref oVERLAPPED);
             array2 = new byte[num];
             Array.Copy(array, array2, num);
         }
         catch
         {
             LogMSG.AddInfo("串口读数据错误!", 2);
         }
     }
     return(array2);
 }
Exemple #13
0
        /// <summary>
        /// TPV校验
        /// </summary>
        /// <param name="Tpv"></param>
        /// <param name="SendTpv"></param>
        /// <returns></returns>
        public bool Assert_Tpv(string Tpv, string SendTpv)
        {
            bool result;

            if (!this.Assert.Tpv)
            {
                result = true;
            }
            else
            {
                if (Tpv == SendTpv)
                {
                    result = true;
                }
                else
                {
                    LogMSG.AddInfo("接收帧 TpV 错误!\r\n", 4);
                    result = false;
                }
            }
            return(result);
        }
Exemple #14
0
        /// <summary>
        /// SEQ校验
        /// </summary>
        /// <param name="PSEQ"></param>
        /// <param name="SendSEQ"></param>
        /// <returns></returns>
        public bool Assert_SEQ(string PSEQ, string SendSEQ)
        {
            bool result;

            if (!this.Assert.SEQ)
            {
                result = true;
            }
            else
            {
                if (PSEQ == SendSEQ)
                {
                    result = true;
                }
                else
                {
                    LogMSG.AddInfo("接收帧 SEQ 错误!\r\n", 4);
                    result = false;
                }
            }
            return(result);
        }
 private void OnDataReceived(object sender, SocketAsyncEventArgs e)
 {
     try
     {
         if (e.BytesTransferred > 0)
         {
             EndPoint remoteEndPoint = e.RemoteEndPoint;
             byte[]   array          = new byte[e.BytesTransferred];
             Array.Copy(e.Buffer, 0, array, 0, e.BytesTransferred);
             RecieveLog.AddInfo(string.Concat(new string[]
             {
                 DateTime.Now.ToString(),
                 " ->接收(UDP) [",
                 remoteEndPoint.ToString(),
                 "] :",
                 this.ByteToHexStr(array),
                 "\r\n"
             }));
             DLT698Message.FrameInfo frameInfo = default(DLT698Message.FrameInfo);
             DLT698Message           dLT       = new DLT698Message();
             if (dLT.Assert_Basic(array))
             {
                 dLT.ParseData(array, e.BytesTransferred, ref frameInfo);
                 if (frameInfo.AFN == "02")
                 {
                     if (frameInfo.FN == "0003")
                     {
                         byte[] data = this.LogOK(frameInfo.A1, frameInfo.A2, frameInfo.FN, frameInfo.PSEQ);
                         LogMSG.AddInfo(string.Concat(new string[]
                         {
                             DateTime.Now.ToString(),
                             " ->终端(UDP) [",
                             remoteEndPoint.ToString(),
                             "] 心跳:",
                             this.ByteToHexStr(array),
                             "\r\n",
                             DateTime.Now.ToString(),
                             " ->回应(UDP) [",
                             remoteEndPoint.ToString(),
                             "] 心跳确认:",
                             this.ByteToHexStr(data),
                             "\r\n"
                         }), 7);
                         this.Send(data);
                     }
                 }
                 else
                 {
                     if (frameInfo.PRM == "0" & frameInfo.DIR == "1")
                     {
                         BufferManager.AddDataItem(frameInfo.A1, frameInfo.A2, array);
                     }
                     else
                     {
                         if (frameInfo.PRM == "1" & frameInfo.DIR == "1")
                         {
                             ActiveReportMsg.AddInfo(array);
                         }
                     }
                 }
             }
         }
     }
     catch
     {
     }
 }