public void Start() { byte[] buffer = new byte[1024]; Action kickoffRead = null; kickoffRead = delegate { BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { try { int actualLength = BaseStream.EndRead(ar); byte[] received = new byte[actualLength]; Buffer.BlockCopy(buffer, 0, received, 0, actualLength); AppSerialDataEvent(received); } catch (Exception ex) { //System.Diagnostics.Debugger.Break(); Trace.WriteLine(ex.Message); } if (IsOpen) { kickoffRead(); // re-trigger } }, null); }; kickoffRead(); }
private void ContinuousRead() { byte[] buffer = new byte[4096]; //On lance une action asynchrone de lecture sur le port série Action kickoffRead = null; kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { try { //On récupère le buffer avec les datas dispo int count = BaseStream.EndRead(ar); byte[] dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); //On lance un évènement OnDatReceived OnDataReceived(dst); } catch (Exception exception) { //SI le port ne répond pas Console.WriteLine("OptimizedSerialPort exception !"); IsSerialPortConnected = false; } if (IsSerialPortConnected && !isRequiredToBeClosed) { //Si on est connecté, on relance l'acquisition en boucle kickoffRead(); } }, null)); kickoffRead(); }
private void ContinuousRead() { byte[] buffer = new byte[4096]; Action kickoffRead = null; kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { try { if (!base.IsOpen) { return; } int count = BaseStream.EndRead(ar); byte[] dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); OnDataReceived(dst); kickoffRead(); } catch (Exception) { return; } }, null)); kickoffRead(); }
// callback function for asynchrous reading on base stream private void ReadCallback(IAsyncResult baseStreamResult) { var outerResult = (DeflateStreamAsyncResult) baseStreamResult.AsyncState; outerResult.m_CompletedSynchronously &= baseStreamResult.CompletedSynchronously; var bytesRead = 0; try { EnsureNotDisposed(); bytesRead = BaseStream.EndRead(baseStreamResult); if (bytesRead <= 0) { // This indicates the base stream has received EOF outerResult.InvokeCallback(0); return; } // Feed the data from base stream into decompression engine inflater.SetInput(buffer, 0, bytesRead); bytesRead = inflater.Inflate(outerResult.buffer, outerResult.offset, outerResult.count); if (bytesRead == 0 && !inflater.Finished()) BaseStream.BeginRead(buffer, 0, buffer.Length, m_CallBack, outerResult); else outerResult.InvokeCallback(bytesRead); } catch (Exception exc) { // Defer throwing this until EndRead where we will likely have user code on the stack. outerResult.InvokeCallback(exc); } }
private void ContinuousRead() { try { byte[] buffer = new byte[4096]; Action kickoffRead = null; kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { try { if (!IsOpen) { return; //Wenn beim Lesen die Verbindung abbricht. } int count = BaseStream.EndRead(ar); byte[] dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); OnDataReceived(dst); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception exception) { Console.WriteLine("ContinuousRead(): Lesefehler Bitstream von COM-Port:\r\n" + ">" + System.Text.Encoding.UTF8.GetString(buffer) + "<" + Environment.NewLine + exception.GetType() + Environment.NewLine + exception.Message + Environment.NewLine + exception.InnerException + Environment.NewLine + exception.Source + Environment.NewLine + exception.StackTrace); #if DEBUG throw exception; #endif } #pragma warning restore CA1031 // Do not catch general exception types if (IsOpen) { kickoffRead(); } }, null)); kickoffRead(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception exception) { Console.WriteLine("ContinuousRead(): Lesefehler bei Beginn. COM-Port:\r\n" + exception.GetType() + Environment.NewLine + exception.Message + Environment.NewLine + exception.InnerException + Environment.NewLine + exception.Source + Environment.NewLine + exception.StackTrace); #if DEBUG throw exception; #endif } #pragma warning restore CA1031 // Do not catch general exception types }
/// <summary> /// Waits for the pending asynchronous read to complete. /// </summary> /// <param name="asyncResult">The reference to the pending asynchronous request to finish.</param> /// <returns> /// The number of bytes read from the stream, between zero (0) and the number of bytes you requested. Streams return zero (0) only at the end of the stream, otherwise, they should block until at least one byte is available. /// </returns> /// <exception cref="T:System.ArgumentNullException"> /// <paramref name="asyncResult"/> is null. </exception> /// <exception cref="T:System.ArgumentException"> /// <paramref name="asyncResult"/> did not originate from a <see cref="M:System.IO.Stream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object)"/> method on the current stream. </exception> /// <exception cref="T:System.IO.IOException">The stream is closed or an internal error has occurred.</exception> public override int EndRead(IAsyncResult asyncResult) { if (_proxyAsyncRequests) { return(BaseStream.EndRead(asyncResult)); } else { return(base.EndRead(asyncResult)); } }
private void ContinuousRead() { byte[] buffer = new byte[4096]; Action kickoffRead = null; kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate (IAsyncResult ar) { if (!base.IsOpen) return; //if base is closed exit func, avoid port closed error int count = BaseStream.EndRead(ar); byte[] dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); OnDataReceived(dst); kickoffRead(); }, null)); kickoffRead(); }
/// <summary> /// Reads data from the stream /// </summary> /// <param name="buffer">Buffer to read into</param> /// <param name="offset">Where in the buffer to start</param> /// <param name="count">Number of bytes to be read</param> /// <returns></returns> public override int Read(byte[] buffer, int offset, int count) { IAsyncResult ar = null; if (!IsConnected || BaseStream == null) { return(0); } ar = BaseStream.BeginRead(buffer, offset, count, null, null); if (!ar.AsyncWaitHandle.WaitOne(m_readTimeout, true)) { Close(); throw new TimeoutException("Timed out trying to read data from the socket stream!"); } return(BaseStream.EndRead(ar)); }
private void ContinuousRead() { byte[] buffer = new byte[4096]; Action kickoffRead = null; kickoffRead = (Action)(() => BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { try { int count = BaseStream.EndRead(ar); byte[] dst = new byte[count]; Buffer.BlockCopy(buffer, 0, dst, 0, count); OnDataReceived(dst); } catch (Exception exception) { Console.WriteLine("OptimizedSerialPort exception !" + exception.Message); } kickoffRead(); }, null)); kickoffRead(); }
/// <summary> /// Reads data from the stream /// </summary> /// <param name="buffer">Buffer to read into</param> /// <param name="offset">Where in the buffer to start</param> /// <param name="count">Number of bytes to be read</param> /// <returns></returns> public override int Read(byte[] buffer, int offset, int count) { IAsyncResult ar = null; if (BaseStream == null) { return(0); } m_lastActivity = DateTime.Now; ar = BaseStream.BeginRead(buffer, offset, count, null, null); if (!ar.AsyncWaitHandle.WaitOne(m_readTimeout, true)) { Close(); throw new TimeoutException("Timed out trying to read data from the socket stream!"); } var x = BaseStream.EndRead(ar); return(x); }
/// <summary>异步读结束</summary> /// <param name="asyncResult"></param> /// <returns></returns> public override Int32 EndRead(IAsyncResult asyncResult) { RaiseAction("EndRead"); return(BaseStream.EndRead(asyncResult)); }
public override int EndRead(IAsyncResult asyncResult) { return(BaseStream.EndRead(asyncResult)); }
/// <summary> /// Waits for the pending asynchronous read to complete. /// </summary> /// <param name="asyncResult"> /// The reference to the pending asynchronous request to finish. /// </param> /// <returns> /// The number of bytes read from the stream, between zero (0) /// and the number of bytes you requested. Streams only return /// zero (0) at the end of the stream, otherwise, they should /// block until at least one byte is available. /// </returns> public override int EndRead(IAsyncResult asyncResult) { CheckClosed(); return(BaseStream.EndRead(asyncResult)); }