Esempio n. 1
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;
 }
Esempio n. 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));
                }
            }
        }
Esempio n. 3
0
        public void WriteLoop()
        {
            try
            {
                while (true)
                {
                    if (cmdList.Count > 0)
                    {
                        byte[] wBuf = new byte[cmdList[0].Length];
                        UTF8Encoding.UTF8.GetBytes(cmdList[0], 0, cmdList[0].Length, wBuf, 0);

                        ioStream.Write(wBuf, 0, cmdList[0].Length);

                        /* remove written data from commandlist */
                        cmdList.RemoveAt(0);
                    }
                    else if (cmdList.Count == 0)
                    {
                        /* wait until new data is signaled */
                        newWriteData.Reset();
                        newWriteData.WaitOne();
                    }
                }
            }
            catch (Exception e)
            {
                if (PipeErrorEvent != null)
                {
                    PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
                }
            }
        }
Esempio n. 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));
         }
     }
 }
Esempio n. 5
0
 public bool Write(string output)
 {
     lock (this)
     {
         if (mSocket == null)
         {
             return(false);
         }
         try
         {
             byte[] outbuf = UTF8Encoding.UTF8.GetBytes(output);
             int    off = 0, res;
             do
             {
                 res = mSocket.Send(outbuf, off, outbuf.Length - off, SocketFlags.None);
                 if (res > 0)
                 {
                     off += res;
                 }
             }while (off < outbuf.Length && res != -1);
             return(off == outbuf.Length);
         }
         catch (System.Net.Sockets.SocketException se)
         {
             mSocket.Close();
             if (PipeErrorEvent != null)
             {
                 PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(se.Message));
             }
             return(false);
         }
         catch (Exception) { return(false); }
     }
 }
Esempio n. 6
0
 public void SerialPortErrorReceived(object sender, SerialErrorReceivedEventArgs args)
 {
     /* port will be closed by debug connection */
     if (PipeErrorEvent != null)
     {
         PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(args.EventType.ToString()));
     }
 }
Esempio n. 7
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));
                    }
                }
            }
        }
Esempio n. 8
0
 public bool Write(string output)
 {
     lock (this)
     {
         if (mSerialPort == null)
         {
             return(false);
         }
         try
         {
             byte[] outbuf = UTF8Encoding.UTF8.GetBytes(output);
             int    off = 0, len;
             //supply the output according to the buffer size, might not be needed
             do
             {
                 if (output.Length - off > mBufSize)
                 {
                     len = mBufSize;
                 }
                 else
                 {
                     len = output.Length - off;
                 }
                 mSerialPort.Write(outbuf, off, len);
                 off += len;
             }while (off < outbuf.Length);
             return(off == outbuf.Length);
         }
         catch (Exception e)
         {
             mSerialPort.Close();
             if (PipeErrorEvent != null)
             {
                 PipeErrorEvent.Invoke(this, new PipeErrorEventArgs(e.Message));
             }
             return(false);
         }
     }
 }