/// <summary> /// /// </summary> /// <param name="byteCommand"></param> /// <param name="latencyTime"></param> public void Send(byte[] byteCommand, int latencyTime) { // 2006.11.03 Added byteCommand null check // if (byteCommand == null) { throw new ArgumentNullException("byteCommand"); } // 2006.09.15 Added // 最小等待时间 15 毫秒 //Debug.Assert (latencyTime >= 15, "latencyTime must >= 15 "); if (latencyTime < MIN_LATENCY_TIME) { throw new ArgumentOutOfRangeException("latencyTime", latencyTime, SR.GetString("LE_LatencyTime_Min")); } if (m_State != CommPortProxyState.None) { throw new Exception(SR.GetString("LE_SendData")); } if ((StateConstants)_winsock.State == StateConstants.sckConnected) { m_State = CommPortProxyState.Receiving; // 发送前清空接收、发送缓冲区 if (_winsock.BytesReceived > 0) { object data = new byte[_winsock.BytesReceived]; _winsock.GetData(ref data, _type, _winsock.BytesReceived); } _winsock.SendData(byteCommand); m_Timer.Interval = latencyTime; m_Timer.Start(); // for debug // WSListen._log.AddLog(this.RemoteHostIP + " Timer start"); } }
/// <summary> /// 接收到时 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void m_Timer_Tick(object sender, EventArgs e) { m_Timer.Stop(); if (_winsock.BytesReceived > 0) { byte[] bs = new byte[_winsock.BytesReceived]; object obj = bs; _winsock.GetData(ref obj, _type, _winsock.BytesReceived); //m_ReceivedData = (byte[]) bs.Clone(); m_ReceivedData = MergeBytes(m_ReceivedData, bs); } if (ReceiveComplete != null) { ReceiveComplete(this, EventArgs.Empty); } m_State = CommPortProxyState.None; // for debug // WSListen._log.AddLog(RemoteHostIP + " timer stop"); }