/// <summary>
        /// 数据处理定时器
        /// 定时检查缓冲区是否有数据,如果有数据则将数据处理并广播。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SPRecvTimer_Tick(object sender, EventArgs e)
        {
            byte[] TemporaryData       = new byte[byteLength];
            int    TemporaryDataLength = 0;

            if (ReceivedDataEvent != null)
            {
                byteSerialPortRecv.CopyTo(TemporaryData, 0);
                TemporaryDataLength = pSerialPortRecv;

                ReceivedDataEvent.Invoke(this, new SerialPortRecvEventArgs(TemporaryData, TemporaryDataLength));
                // 数据处理完后,将指针指向数据头,等待接收新的数据
                pSerialPortRecv = 0;
            }
        }
Exemplo n.º 2
0
    private void ReceiveCallback(IAsyncResult ar)
    {
        var state = ar.AsyncState as SocketState;

        try
        {
            if (state != null)
            {
                int receiveLen = state.Self.EndReceiveFrom(ar, ref state.RemotePoint);
                if (receiveLen > 0)
                {
                    byte[] receivedData = new byte[receiveLen];
                    Array.Copy(state.Buffer, 0, receivedData, 0, receiveLen);
                    state.Buffer = receivedData;

                    //string str = System.Text.Encoding.UTF8.GetString(receivedData);
                    //Debug.Log(str);
                    //SqlHelper.Insatance.GetSqlConn();

                    Debug.Log($"接收数据长度:{receivedData.Length}");

                    Debug.Log(BitConverter.ToString(receivedData));
                    GPSInfosManager.Insatance.praseMessage(receivedData);

                    state.ReceivedTime = DateTime.Now;
                    ReceivedDataEvent?.Invoke(state);
                }
            }
        }
        catch (Exception error)
        {
            ErrorEvent?.Invoke(error.Message, state?.RemotePoint);
        }
        finally
        {
            if (state != null)
            {
                BeginReceive();
            }
        }
    }
Exemplo n.º 3
0
 protected virtual void OnRaiseReceivedData(string ip, byte[] data)
 {
     ReceivedDataEvent?.Invoke(this, new SocketEventArgs(ip, data));
 }
Exemplo n.º 4
0
 public void ReceivedData(string status)
 {
     ReceivedDataEvent?.Invoke(status);
 }