예제 #1
0
        /**
         * <summary>
         * Send first message in tx fifo
         * </summary>
         *
         * @throws USBtinException On serial port errors
         */
        protected void SendFirstTXFifoMessage()
        {
            if (fifoTX.Count == 0)
            {
                return;
            }

            CANMessage canmsg = fifoTX.First.Value;

            try
            {
                serialPort.Write(encoding.GetBytes(canmsg.ToString() + "\r"), 0, 1);
            }
            catch (Exception e)
            {
                throw new USBtinException(e.Message);
            }
        }
예제 #2
0
        /**
         * <summary>
         * Handle serial port event.
         * Read single byte and check for end of line character.
         * If end of line is reached, parse command and dispatch it.
         * </summary>
         *
         * <param name="e">Serial port event args</param>
         */
        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort s = (SerialPort)sender;

            try
            {
                byte[] buffer = null;
                s.Read(buffer, 0, s.BytesToRead);
                foreach (byte b in buffer)
                {
                    if ((b == '\r') & incomingMessage.Length > 0)
                    {
                        string message = incomingMessage.ToString();
                        char   cmd     = message[0];

                        // check if this is a CAN message
                        if (cmd == 't' || cmd == 'T' || cmd == 'r' || cmd == 'R')
                        {
                            // create CAN message from message string
                            CANMessage canmsg = new CANMessage(message);

                            // give the CAN message to the listeners
                            foreach (ICANMessageListener listener in listeners)
                            {
                                listener.ReceiveCANMessage(canmsg);
                            }
                        }
                        else if ((cmd == 'z') || (cmd == 'Z'))
                        {
                            // remove first message from transmit fifo and send next one
                            fifoTX.RemoveFirst();

                            try
                            {
                                SendFirstTXFifoMessage();
                            }
                            catch (USBtinException)
                            {
                                //System.err.println(ex);
                            }
                        }
                        incomingMessage.Length = 0;
                    }
                    else if (b == 0x07)
                    {
                        // resend first element from tx fifo
                        try
                        {
                            SendFirstTXFifoMessage();
                        }
                        catch (USBtinException)
                        {
                            //System.err.println(ex);
                        }
                    }
                    else if (b != '\r')
                    {
                        incomingMessage.Append((char)b);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new USBtinException(ex.Message);
            }
        }