void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (e.EventType == SerialData.Chars) { string rxdata = _port.ReadExisting(); for (int t = 0; t < rxdata.Length; t++) { byte b = Convert.ToByte(rxdata[t]); if (_ecustate == ECUState.CommunicationRunning) { //AddToLog("RX: " + b.ToString("X2")); HandleRunningCommunicationByte(b); } else { AddToLog("RXINIT: " + b.ToString("X2")); if (_nextByteStartRunningState) { _nextByteStartRunningState = false; _ecustate = ECUState.CommunicationRunning; CastInfoEvent("Communication ready", 0); } else { HandleInitByte(b); } } } } }
private bool InitializeCommunication(string comportnumber) { Console.WriteLine("InitializeCommunication: " + comportnumber); try { if (!_initIsDone) { _timer = new System.Timers.Timer(10); _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); 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); } MM_BeginPeriod(1); Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; // high prio thread _initIsDone = true; } else { MM_BeginPeriod(1); if (_port.IsOpen) { _port.Close(); } _port.BaudRate = 5; _port.PortName = comportnumber; _port.Open(); } _timer.Enabled = true; return(true); } catch (Exception E) { _ecustate = ECUState.NotInitialized; CastInfoEvent("Failed to initialize KWP71: " + E.Message, 0); } return(false); }
public override void StartCommunication(string comportnumber, bool HighSpeed) { Console.WriteLine("StartCommunication: " + comportnumber); ACK_Buf[0] = 0x82; ACK_Buf[1] = 0x7A; ACK_Buf[2] = 0x13; ACK_Buf[3] = 0xA1; ACK_Buf[4] = 0xB0; // block to be send as default if no other command si ready rx_state = 0; _ecustate = ECUState.NotInitialized; IsRecv = false; Cmd_Rdy = false; IsConnected = false; CmdLock = false; // flag to avoid writing a command while sending ; this avoids use of complicated mutexes LastCmd_CTR = 0; // stores the frame counter for last comand sent _communicationRunning = false; _state = CommunicationState.Start; _syncSeen = false; kw1seen = false; kw2seen = false; kw1 = 0; kw2 = 0; _bytesToIgnore = 0; Send_ACK = true; // when there is nothing else to send Echo = true; // true because we answered by KW complement ECU_IsMaster = true; // because ECU is master after wake up Rx_Ctr = 0; Tx_Ctr = 0; Bytes_ToRead = 0; Block_Ctr = 0; // counter for frames IDctr = 0; // counter for initial asscii messages Cmd_Len = 0; // length of frame Buf_Ptr = 0; Bytes_ToSend = 0; if (InitializeCommunication(comportnumber)) { //Do stuff _communicationRunning = true; } }
private void HandleInitByte(byte b) { /*if (_ecustate == ECUState.CommunicationRunning) * { * SendAck(b); * }*/ switch (rx_state) { case 0: if (_state == CommunicationState.Start || _state == CommunicationState.WaitForKeywords) { if (!_syncSeen) { if (b == 0x55) { _syncSeen = true; } _ecustate = ECUState.Initialized; //CastInfoEvent("Synchronization in progress", 0); } else { if (!kw1seen) { kw1seen = true; kw1 = b; } else if (!kw2seen) { kw2seen = true; kw2 = b; _state = CommunicationState.Idle; SendAck(kw2, true); AddToLog("kw1: " + kw1.ToString("X2") + " kw2: " + kw2.ToString("X2")); AddToLog("Entering idle state"); _nextByteStartRunningState = true; } } } break; } }
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; // we need to send 0x10 at 5 baud //_port.Write("\x10"); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = false; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(600); _port.BreakState = false; Thread.Sleep(200); //Thread.Sleep(2000); //while (_port.BytesToWrite > 0) Thread.Sleep(0); _state = CommunicationState.WaitForKeywords; _port.BaudRate = 9600; _timeout = 0; 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 = ECUState.NotInitialized; CastInfoEvent("Timeout waiting for keywords", 0); _state = CommunicationState.Start; _timeout = 0; _wakeupRetries++; if (_wakeupRetries == 6) { _wakeupRetries = 1; StopCommunication(); CastInfoEvent("Unable to connect to ECU", 0); return; // don't restart the timer } } // timeout? break; case CommunicationState.Idle: //CastInfoEvent("In idle state waiting for messages", 0); // doin' nuthing break; case CommunicationState.SendCommand: break; } } catch (Exception E) { AddToLog(E.Message); } } _timer.Enabled = true; }
private void HandleInitByte(byte b) { /*if (_ecustate == ECUState.CommunicationRunning) * { * SendAck(b); * }*/ switch (rx_state) { case 0: if (_state == CommunicationState.Start || _state == CommunicationState.WaitForKeywords) { if (!_syncSeen) { if (b == 0x55) { _syncSeen = true; } _ecustate = ECUState.Initialized; CastInfoEvent("Synchronization in progress", 0); } else { if (!kw1seen) { kw1seen = true; kw1 = b; } else if (!kw2seen) { kw2seen = true; kw2 = b; SendAck(kw2, true); AddToLog("kw1: " + kw1.ToString("X2") + " kw2: " + kw2.ToString("X2")); } else if (!kw2complseen) { kw2complseen = true; _state = CommunicationState.Idle; AddToLog("Entering idle state"); _nextByteStartRunningState = true; pData = (byte)(0xFF - b); Console.WriteLine("ECUID: " + b.ToString("X2")); Cmd_Buf[0] = 0x83; Cmd_Buf[1] = 0x7A; Cmd_Buf[2] = 0x13; Cmd_Buf[3] = 0xB9; Cmd_Buf[4] = 0xF0; Cmd_Buf[5] = 0xB9; Cmd_Rdy = true; // get ID immediately after starting communication Thread.Sleep(250); Len = (byte)(Cmd_Buf[0] - 0x80 + 3); // size of cmd + chksum , zero based index for (i = 0; i < Len; i++) { pData = Cmd_Buf[i]; SendAck(pData, false); } Echo = true; Cmd_Rdy = false; for (i = 0; i < Len; i++) { SendAck(b, false); } Cmd_Rdy = false; Exp_Ans = true; //Echo received. Expecting answer to command Send_ACK = true; // next cmd will be an ACK IsConnected = true; } } } break; } }
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", 0); //_port.BaudRate = 5; // we need to send 0x10 at 5 baud // for kwp81 this is 0x7A _port.BreakState = true; // 0 startbit Thread.Sleep(200); _port.BreakState = true; // 0 bit 7 Thread.Sleep(200); _port.BreakState = false; // 1 bit 6 Thread.Sleep(200); _port.BreakState = true; // 0 bit 5 Thread.Sleep(200); _port.BreakState = false; // 1 bit 4 Thread.Sleep(200); _port.BreakState = false; // 1 bit 3 Thread.Sleep(200); _port.BreakState = false; // 1 bit 2 Thread.Sleep(200); _port.BreakState = false; // 1 bit 1 Thread.Sleep(200); _port.BreakState = true; // 0 bit 0 Thread.Sleep(200); _port.BreakState = false; // 1 stop bit or average bit? Thread.Sleep(200); /*_port.BreakState = true; // 0 * Thread.Sleep(200); * _port.BreakState = true; // 0 * Thread.Sleep(200); * _port.BreakState = true; // 0 * Thread.Sleep(200); * _port.BreakState = true; // 0 * Thread.Sleep(200); * _port.BreakState = true; // 0 * Thread.Sleep(200); * _port.BreakState = false; // 1 * Thread.Sleep(200); * _port.BreakState = true; // 0 0 0 * Thread.Sleep(600); * _port.BreakState = false; // 1 * Thread.Sleep(200);*/ //Thread.Sleep(2000); //while (_port.BytesToWrite > 0) Thread.Sleep(0); _state = CommunicationState.WaitForKeywords; _port.BaudRate = 10400; // new protocol = 10k4 baud _timeout = 0; break; case CommunicationState.WaitForKeywords: if (_timeout++ > 500) { _ecustate = ECUState.NotInitialized; CastInfoEvent("Timeout waiting for keywords", 0); _state = CommunicationState.Start; _timeout = 0; } //Console.WriteLine("waiting for kw: " + _timeout.ToString()); // timeout? break; case CommunicationState.Idle: //CastInfoEvent("In idle state waiting for messages", 0); // doin' nuthing break; case CommunicationState.SendCommand: break; } } catch (Exception E) { AddToLog(E.Message); } } _timer.Enabled = true; }
void _port_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (e.EventType == SerialData.Chars) { string rxdata = _port.ReadExisting(); for (int t = 0; t < rxdata.Length; t++) { byte b = Convert.ToByte(rxdata[t]); if (_ecustate == ECUState.CommunicationRunning) { //AddToLog("RX: " + b.ToString("X2")); HandleRunningCommunicationByte(b); } else { AddToLog("RXINIT: " + b.ToString("X2")); HandleInitByte(b); if (_nextByteStartRunningState) { _nextByteStartRunningState = false; _ecustate = ECUState.CommunicationRunning; CastInfoEvent("Communication ready", 0); } } } } }
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", 0); //_port.BaudRate = 5; // we need to send 0x10 at 5 baud // for kwp81 this is 0x7A _port.BreakState = true; // 0 startbit Thread.Sleep(200); _port.BreakState = true; // 0 bit 7 Thread.Sleep(200); _port.BreakState = false; // 1 bit 6 Thread.Sleep(200); _port.BreakState = true; // 0 bit 5 Thread.Sleep(200); _port.BreakState = false; // 1 bit 4 Thread.Sleep(200); _port.BreakState = false; // 1 bit 3 Thread.Sleep(200); _port.BreakState = false; // 1 bit 2 Thread.Sleep(200); _port.BreakState = false; // 1 bit 1 Thread.Sleep(200); _port.BreakState = true; // 0 bit 0 Thread.Sleep(200); _port.BreakState = false; // 1 stop bit or average bit? Thread.Sleep(200); /*_port.BreakState = true; // 0 Thread.Sleep(200); _port.BreakState = true; // 0 Thread.Sleep(200); _port.BreakState = true; // 0 Thread.Sleep(200); _port.BreakState = true; // 0 Thread.Sleep(200); _port.BreakState = true; // 0 Thread.Sleep(200); _port.BreakState = false; // 1 Thread.Sleep(200); _port.BreakState = true; // 0 0 0 Thread.Sleep(600); _port.BreakState = false; // 1 Thread.Sleep(200);*/ //Thread.Sleep(2000); //while (_port.BytesToWrite > 0) Thread.Sleep(0); _state = CommunicationState.WaitForKeywords; _port.BaudRate = 10400; // new protocol = 10k4 baud _timeout = 0; break; case CommunicationState.WaitForKeywords: if (_timeout++ > 500) { _ecustate = ECUState.NotInitialized; CastInfoEvent("Timeout waiting for keywords", 0); _state = CommunicationState.Start; _timeout = 0; } //Console.WriteLine("waiting for kw: " + _timeout.ToString()); // timeout? break; case CommunicationState.Idle: //CastInfoEvent("In idle state waiting for messages", 0); // doin' nuthing break; case CommunicationState.SendCommand: break; } } catch (Exception E) { AddToLog(E.Message); } } _timer.Enabled = true; }
private void HandleInitByte(byte b) { /*if (_ecustate == ECUState.CommunicationRunning) { SendAck(b); }*/ switch (rx_state) { case 0: if (_state == CommunicationState.Start || _state == CommunicationState.WaitForKeywords) { if (!_syncSeen) { if (b == 0x55) _syncSeen = true; _ecustate = ECUState.Initialized; CastInfoEvent("Synchronization in progress", 0); } else { if (!kw1seen) { kw1seen = true; kw1 = b; } else if (!kw2seen) { kw2seen = true; kw2 = b; SendAck(kw2, true); AddToLog("kw1: " + kw1.ToString("X2") + " kw2: " + kw2.ToString("X2")); } else if (!kw2complseen) { kw2complseen = true; _state = CommunicationState.Idle; AddToLog("Entering idle state"); _nextByteStartRunningState = true; pData = (byte)(0xFF - b); Console.WriteLine("ECUID: " + b.ToString("X2")); Cmd_Buf[0] = 0x83; Cmd_Buf[1] = 0x7A; Cmd_Buf[2] = 0x13; Cmd_Buf[3] = 0xB9; Cmd_Buf[4] = 0xF0; Cmd_Buf[5] = 0xB9; Cmd_Rdy = true; // get ID immediately after starting communication Thread.Sleep(250); Len = (byte)(Cmd_Buf[0] - 0x80 + 3); // size of cmd + chksum , zero based index for (i = 0; i < Len; i++) { pData = Cmd_Buf[i]; SendAck(pData, false); } Echo = true; Cmd_Rdy = false; for (i = 0; i < Len; i++) { SendAck(b, false); } Cmd_Rdy = false; Exp_Ans = true; //Echo received. Expecting answer to command Send_ACK = true; // next cmd will be an ACK IsConnected = true; } } } break; } }
private bool InitializeCommunication(string comportnumber) { Console.WriteLine("InitializeCommunication: " + comportnumber); try { if (!_initIsDone) { _timer = new System.Timers.Timer(10); _timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed); 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); } MM_BeginPeriod(1); Thread.CurrentThread.Priority = ThreadPriority.AboveNormal; // high prio thread _initIsDone = true; } else { MM_BeginPeriod(1); if (_port.IsOpen) _port.Close(); _port.BaudRate = 5; _port.PortName = comportnumber; _port.Open(); } _timer.Enabled = true; return true; } catch (Exception E) { _ecustate = ECUState.NotInitialized; CastInfoEvent("Failed to initialize KWP81: " + E.Message, 0); } return false; }
public StatusEventArgs(string info, int percentage, ECUState state) { this._info = info; this._percentage = percentage; this._state = state; }
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; // we need to send 0x10 at 5 baud //_port.Write("\x10"); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(200); _port.BreakState = false; Thread.Sleep(200); _port.BreakState = true; Thread.Sleep(600); _port.BreakState = false; Thread.Sleep(200); //Thread.Sleep(2000); //while (_port.BytesToWrite > 0) Thread.Sleep(0); _state = CommunicationState.WaitForKeywords; _port.BaudRate = 12700; _timeout = 0; 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 = ECUState.NotInitialized; CastInfoEvent("Timeout waiting for keywords", 0); _state = CommunicationState.Start; _timeout = 0; _wakeupRetries++; if (_wakeupRetries == 6) { _wakeupRetries = 1; StopCommunication(); CastInfoEvent("Unable to connect to ECU", 0); return; // don't restart the timer } } // timeout? break; case CommunicationState.Idle: //CastInfoEvent("In idle state waiting for messages", 0); // doin' nuthing break; case CommunicationState.SendCommand: break; } } catch (Exception E) { AddToLog(E.Message); } } _timer.Enabled = true; }
private void HandleInitByte(byte b) { /*if (_ecustate == ECUState.CommunicationRunning) { SendAck(b); }*/ switch (rx_state) { case 0: if (_state == CommunicationState.Start || _state == CommunicationState.WaitForKeywords) { if (!_syncSeen) { if (b == 0x55) _syncSeen = true; _ecustate = ECUState.Initialized; //CastInfoEvent("Synchronization in progress", 0); } else { if (!kw1seen) { kw1seen = true; kw1 = b; } else if (!kw2seen) { kw2seen = true; kw2 = b; _state = CommunicationState.Idle; SendAck(kw2, true); AddToLog("kw1: " + kw1.ToString("X2") + " kw2: " + kw2.ToString("X2")); AddToLog("Entering idle state"); _nextByteStartRunningState = true; } } } break; } }