Пример #1
0
 public void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs args)
 {
     if (PipeReceiveEvent != null)
     {
         PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(mSerialPort.ReadExisting()));
     }
 }
Пример #2
0
        public void ReadLoop()
        {
            byte[] buf  = new byte[PIPE_SIZE];
            int    read = 0;

            try
            {
                while (true)
                {
                    read = ioStream.Read(buf, 0, PIPE_SIZE);
                    if (read > 0)
                    {
                        if (PipeReceiveEvent != null)
                        {
                            PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(UTF8Encoding.UTF8.GetString(buf, 0, read)));
                        }
                    }
                    else
                    {
                        /* connecion closed */
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                if (PipeErrorEvent != null)
                {
                    PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
                }
            }
        }
Пример #3
0
 public SerialPipe(SerialPort port)
 {
     mSerialPort = port;
     mBufSize    = mSerialPort.WriteBufferSize;
     //try to get older input once without timeout (to avoid blocking behaviour)
     mSerialPort.ReadTimeout = 10;
     try
     {
         String buf = mSerialPort.ReadLine();
         if (buf.Length > 0 && PipeReceiveEvent != null)
         {
             PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(buf));
         }
     }
     catch (TimeoutException) { }
     catch (Exception e)
     {
         if (PipeErrorEvent != null)
         {
             PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
         }
     }
     mSerialPort.ReadTimeout = -1;
     //set up internal handlers
     mSerialPort.DataReceived  += SerialPortDataReceived;
     mSerialPort.ErrorReceived += SerialPortErrorReceived;
 }
Пример #4
0
 public void TriggerReadable(IAsyncResult result)
 {
     try
     {
         int    bytes   = mSocket.EndReceive(result);
         string datastr = UTF8Encoding.UTF8.GetString(mBuf, 0, bytes);
         if (PipeReceiveEvent != null)
         {
             PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(datastr));
         }
         do
         {
             mResult = mSocket.BeginReceive
                           (mBuf, 0, mBuf.Length, SocketFlags.None, TriggerReadable, this);
         } while (mResult.CompletedSynchronously);
     }
     catch (Exception e)
     {
         mSocket.Close();
         if (PipeErrorEvent != null)
         {
             PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
         }
     }
 }
Пример #5
0
        public void ReadLoop()
        {
            byte[] buf  = new byte[PIPE_SIZE];
            int    read = 0;

            if (!bClientConn)
            {
                if (PipeErrorEvent != null)
                {
                    PipeErrorEvent.Invoke(this, new PipeErrorEventArgs("Client not connected"));
                }
            }
            else
            {
                try
                {
                    while (bClientConn)
                    {
                        read = ioStream.Read(buf, 0, PIPE_SIZE);
                        if (read > 0)
                        {
                            if (PipeReceiveEvent != null)
                            {
                                PipeReceiveEvent.Invoke(this, new PipeReceiveEventArgs(UTF8Encoding.UTF8.GetString(buf, 0, read)));
                            }
                        }
                        else
                        {
                            /*
                             * Connecion closed!
                             * We'll hijack this thread and use it to set up our pipe server again.
                             * This thread will terminate once the connection is set up, it does not block.
                             */
                            signalDisconnected();
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    if (PipeErrorEvent != null)
                    {
                        PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
                    }
                }
            }
        }