Ejemplo n.º 1
0
        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();
        }
Ejemplo n.º 2
0
        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();
            }
        }