private void InitializeRead() { byte[] buffer = new byte[BlockLimit]; void reader() { if (port.IsOpen) { try { port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar) { if (port.IsOpen) { try { int bytesRead = port.BaseStream.EndRead(ar); byte[] received = new byte[bytesRead]; Buffer.BlockCopy(buffer, 0, received, 0, bytesRead); Destuffer.Add(bytesRead, received); BytesReceived += bytesRead; } catch (Exception e) { Log.Error(e.Message); } reader(); } }, null); } catch (Exception e) { Log.Error(e.Message); } } } reader(); }
protected override void DoOpen() { lock (lockObject) { if (port.IsOpen) { Close(); } port.PortName = Location; port.BaudRate = BaudRate; port.Parity = Parity.None; port.StopBits = StopBits.One; port.DataBits = 8; port.Handshake = Handshake.None; port.DtrEnable = ResetOnConnection; port.ReadTimeout = 10; Destuffer.Reset(); port.Open(); InitializeRead(); } }