public static void Read()
    {
        byte[] buffer      = new byte[16];
        Action kickoffRead = null;
        Action reconnect   = null;
        Action next        = kickoffRead;

        reconnect = delegate {
            // if (_serialPort == null || !_serialPort.IsOpen) {
            Debug.Log("Attempting to reconnect to port " + "COM8"); // faking the reconnection status indication.
            _serialPortStatus = SerialPortStatus.Reconnecting;
            Thread.Sleep(400);
            if (Connect("COM8"))
            {
                Debug.Log("We decided we are connected?");
                next = kickoffRead;
            }
            else
            {
                Thread.Sleep(2600);
                next = reconnect;
            }
            // }
            if (_alive)
            {
                next();
            }
        };

        kickoffRead = delegate {
            _serialPort.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar)
            {
                try {
                    int actualLength = _serialPort.BaseStream.EndRead(ar);
                    byte[] received  = new byte[actualLength];
                    Buffer.BlockCopy(buffer, 0, received, 0, actualLength);
                    splitter.OnIncomingBinaryBlock(received);
                    next = kickoffRead;
                }
                catch (IOException exc) {
                    Debug.Log("Serial read fail: " + exc.ToString());
                    next = reconnect;
                }
                catch (InvalidOperationException exc) {
                    Debug.Log("Serial read fail: " + exc.ToString());
                    next = reconnect;
                }
                if (_alive)
                {
                    next();
                }
            }, null);
        };
        reconnect();
    }
Exemplo n.º 2
0
        /// <summary>
        /// Called when any data bytes are received over the serial port.
        /// </summary>
        /// <remarks>
        /// This method calls the message parser class, which will call the
        /// MessageReceived delegate in this class.
        /// </remarks>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int dataLength = _serialPort.BytesToRead;

            byte[] data        = new byte[dataLength];
            int    nbrDataRead = _serialPort.Read(data, 0, dataLength);

            if (nbrDataRead != 0)
            {
                lineSplitter.OnIncomingBinaryBlock(data);
            }
        }
Exemplo n.º 3
0
        public void DetectorTestTwoLinesArrivingTogether()
        {
            // Create a list to hold data packets.
            List <byte[]> result_lines = new List <byte[]>();

            // Create a line splitter object.
            LineSplitter lineSplitter = new LineSplitter();

            // When a new data packet is received, add it to the list of data packets.
            lineSplitter.LineReceived += result_lines.Add;

            // Send an array of bytes into the splitter.
            lineSplitter.OnIncomingBinaryBlock(new byte[] { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x72, 0x65, 0x61, 0x64, 0x65, 0x72, 0x73, 0x21, 0x0D, 0x0A, 0x53, 0x70, 0x61, 0x72, 0x78, 0x20, 0x69, 0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x65, 0x73, 0x74, 0x2E, 0x0D, 0x0A });

            // Check that the line splitter correctly found two lines.
            Assert.AreEqual(2, result_lines.Count);
        }