Esempio n. 1
0
        /*
         * private void sendAt5Baud(int addr)
         * {
         *  //set start bit (line low --> break true)
         *  _port.BreakState = true;
         *  Thread.Sleep(BIT_INTERVAL_5B);
         *
         *  int i = 0;
         *  for (i = 0; i < BYTE_LENGTH; ++i)
         *  {
         *      //_stopWatch.Stop();
         *      //Console.WriteLine("5Baud interval was " + _stopWatch.Elapsed.TotalMilliseconds + " ms");
         *      //_stopWatch.Reset();
         *      //_stopWatch.Start();
         *      if ((addr & (1 << i)) > 0)
         *      {
         *          //set line high
         *          _port.BreakState = false;
         *      }
         *      else
         *      {
         *          //set line low
         *          _port.BreakState = true;
         *      }
         *      Thread.Sleep(BIT_INTERVAL_5B);
         *  }
         *  //_stopWatch.Reset();
         *
         *  //set stop bit
         *  _port.BreakState = false;
         *  Thread.Sleep(BIT_INTERVAL_5B);
         *
         *
         * }
         */

        private void handle5BaudTimer()
        {
            //_stopWatch.Stop();
            //TimeSpan ts = _stopWatch.Elapsed;
            //Console.WriteLine("5baud microtimer interval was " + ts.TotalMilliseconds + " ms");
            //_stopWatch.Reset();
            //_stopWatch.Start();

            if (_startctr == 0)
            {
                _ecustate = ICommunication.ECUState.NotInitialized;
                CastInfoEvent("Sending init/wakeup sequence [" + _wakeupRetries.ToString() + "/5]", 0);
                //start bit
                _port.BreakState = true;
            }
            else if (_startctr == 9)
            {
                //stop bit
                _port.BreakState = false;
            }
            else if (_startctr == 10)
            {
                _stopWatch.Reset();
                _state = CommunicationState.WaitForKeywords;

                _port.BaudRate      = _baudrate;
                _timeout            = 0;
                _startctr           = 0;
                _microTimer.Enabled = false;
            }
            else
            {
                if ((_ecuaddr & (1 << (_startctr - 1))) > 0)
                {
                    //set line high
                    //Console.WriteLine("HIGH");
                    _port.BreakState = false;
                }
                else
                {
                    //set line low
                    //Console.WriteLine("LOW");
                    _port.BreakState = true;
                }
            }
            ++_startctr;
        }
Esempio n. 2
0
        private void clear()
        {
            _ecustate = ICommunication.ECUState.NotInitialized;

            _startctr      = 0;
            _timeout       = 0;
            _wakeupRetries = 1;
            _echo          = 0;
            _idlesent      = false;

            _kw1 = 0;
            _kw2 = 0;

            _syncseen    = false;
            _kw1seen     = false;
            _kw2seen     = false;
            _invaddrseen = false;

            _sendctr = 0;
            _state   = CommunicationState.Start;
        }
Esempio n. 3
0
 private void HandleInitByte(byte b)
 {
     if (!_syncseen)
     {
         if (b == SYNC_BYTE)
         {
             _syncseen = true;
             Console.WriteLine("Sync byte received!");
         }
         //CastInfoEvent("Synchronization in progress", 0);
     }
     else
     {
         if (!_kw1seen)
         {
             _kw1seen = true;
             _kw1     = b;
         }
         else if (!_kw2seen)
         {
             _kw2seen = true;
             _kw2     = b;
             //TODO: need to apply W4 delay (25-50ms) before sending this?
             SendAck(_kw2, true);
             Console.WriteLine("Keywords: " + _kw1.ToString("X2") + _kw2.ToString("X2"));
             //AddToLog("kw1: " + kw1.ToString("X2") + " kw2: " + kw2.ToString("X2"));
             //AddToLog("Entering idle state");
         }
         else if (!_invaddrseen)
         {
             _invaddrseen = true;
             _state       = CommunicationState.Idle;
             _timeout     = 0;
             _ecustate    = ICommunication.ECUState.CommunicationRunning;
             CastInfoEvent("Communication ready", 0);
             CastECUInfoEvent(3, "M2.10.3"); //a bit of hack but works
             Console.WriteLine("Inverted address received!");
         }
     }
 }
Esempio n. 4
0
        private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _timer.Enabled = false;
            if (_port.IsOpen)
            {
                try
                {
                    switch (_state)
                    {
                    case CommunicationState.Start:
                        //_ecustate = ECUState.NotInitialized;
                        //CastInfoEvent("Sending init/wakeup sequence [" + _wakeupRetries.ToString() + "/5]", 0);
                        //_port.BaudRate = 5;

                        //Console.WriteLine("Starting 5baud init!");

                        //sendAt5Baud(_ecuaddr);

                        //_state = CommunicationState.WaitForKeywords;

                        //_port.BaudRate = _baudrate;
                        //_timeout = 0;

                        //not used currently...
                        //handle5BaudTimer()

                        break;

                    case CommunicationState.WaitForKeywords:
                        if (_timeout == 0 || _timeout == 100 || _timeout == 200 || _timeout == 300 || _timeout == 400 || _timeout == 500)
                        {
                            int secs = _timeout / 100;
                            CastInfoEvent("Waiting for keywords from ECU (" + secs.ToString() + "/5 seconds)", 0);
                        }
                        if (_timeout++ > 500)
                        {
                            _ecustate = ICommunication.ECUState.NotInitialized;
                            CastInfoEvent("Timeout waiting for keywords", 0);
                            Console.WriteLine("Timeout waiting for keywords");
                            _state   = CommunicationState.Start;
                            _timeout = 0;
                            _wakeupRetries++;
                            if (_wakeupRetries == 6)
                            {
                                _wakeupRetries = 1;

                                stop();
                                CastInfoEvent("Unable to connect to ECU", 0);
                                return;     // don't restart the timer
                            }
                            _microTimer.Enabled = true;
                        }
                        // timeout?
                        break;

                    case CommunicationState.Idle:
                        if (_timeout++ > IDLE_TIMEOUT)
                        {
                            //send the idle message to prevent the connection from closing
                            Console.WriteLine("Send the idle message");
                            _idlesent            = true;
                            _timeout             = 0;
                            _sendMsg             = _idleMsg;
                            _sendctr             = 0;
                            _state               = CommunicationState.SendCommand;
                            _microTimer.Interval = P4MIN * 1000;
                            _microTimer.Enabled  = true;
                        }

                        break;

                    case CommunicationState.WaitForResponse:
                        ++_timeout;
                        if (_timeout == 6)
                        {
                            Console.WriteLine("Receiving finished");
                            _timeout = 0;
                            _state   = CommunicationState.Idle;

                            //inform that complete message arrived
                            if (_idlesent == false)
                            {
                                _event.Set();
                            }
                            else
                            {
                                _idlesent = false;
                                _rcvMsg.Clear();
                            }
                        }
                        break;
                    }
                }
                catch (Exception E)
                {
                    //AddToLog(E.Message);
                    Console.WriteLine(E.Message);
                }
            }
            _timer.Enabled = true;
        }
Esempio n. 5
0
        public bool slowInit(string comportnumber, int ecuaddr, int baudrate)
        {
            clear();

            _ecuaddr  = ecuaddr;
            _baudrate = baudrate;

            try
            {
                if (!_initIsDone)
                {
                    _timer          = new System.Timers.Timer(10);
                    _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);

                    //micro timer is used for precise events such as the 5baud init
                    //sleep is not accurate enough with all computers even with MM period set to 1ms
                    _microTimer = new MicroTimer(BIT_INTERVAL_5B * 1000);
                    _microTimer.MicroTimerElapsed += new MicroLibrary.MicroTimer.MicroTimerElapsedEventHandler(microTimerEvent);

                    if (_port.IsOpen)
                    {
                        _port.Close();
                    }
                    _port.Encoding = Encoding.GetEncoding("ISO-8859-1");
                    _port.BaudRate = 5;
                    _port.PortName = comportnumber;
                    _port.ReceivedBytesThreshold = 1;
                    _port.DataReceived          += new SerialDataReceivedEventHandler(_port_DataReceived);
                    _port.Open();
                    try
                    {
                        _port.Handshake  = Handshake.None;
                        _port.RtsEnable  = true;
                        _port.BreakState = false;
                        _port.DtrEnable  = true;
                    }
                    catch (Exception E)
                    {
                        //AddToLog("Failed to set pins: " + E.Message);
                    }
                    //no need for this anymore
                    //MM_BeginPeriod(1);
                    //Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; // high prio thread
                    _initIsDone = true;
                }
                else
                {
                    //MM_BeginPeriod(1);
                    if (_port.IsOpen)
                    {
                        _port.Close();
                    }
                    _port.BaudRate = _baudrate;
                    _port.PortName = comportnumber;
                    _port.Open();
                }

                _timer.Enabled      = true;
                _microTimer.Enabled = true;
                return(true);
            }
            catch (Exception E)
            {
                _ecustate = ICommunication.ECUState.NotInitialized;
                CastInfoEvent("Failed to initialize: " + E.Message, 0);
                Console.WriteLine("Failed to initialize: " + E.Message);
            }
            return(false);
        }