/// <summary> /// waitForMessage waits for a specific CAN message give by a CAN id. /// </summary> /// <param name="a_canID">The CAN id to listen for</param> /// <param name="timeout">Listen timeout</param> /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param> /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns> public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { r_canMsg = new Lawicel.CANUSB.CANMsg(); canMsg = new CANMessage(); string line = string.Empty; int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { m_serialPort.Write("\r"); m_serialPort.Write("P\r"); bool endofFrames = false; while (!endofFrames) { Console.WriteLine("reading line"); line = m_serialPort.ReadLine(); Console.WriteLine("line: " + line + " len: " + line.Length.ToString()); if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A') { endofFrames = true; } else { if (line.Length == 14) { // three bytes identifier r_canMsg = new Lawicel.CANUSB.CANMsg(); r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16); r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16); ulong data = 0; // add all the bytes data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16) << 7 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 6 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 5 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 4 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 3 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 2 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 1 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16); r_canMsg.data = data; canMsg.setID(r_canMsg.id); canMsg.setLength(r_canMsg.len); canMsg.setFlags(0); canMsg.setData(r_canMsg.data); if (r_canMsg.id != a_canID) { continue; } return((uint)r_canMsg.id); } } } //Thread.Sleep(0); nrOfWait++; } return(0); }
/// <summary> /// Send a message that starts a session. This is used to test if there is /// a connection. /// </summary> /// <returns></returns> private bool sendSessionRequest() { logger.Debug("Sending session request"); // 0x220 is for T7 // 0x7E0 is for T8 CANMessage msg1 = new CANMessage(0x220, 0, 8); Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg1.setData(0x000040021100813f); if (!sendMessage(msg1)) { logger.Debug("Unable to send session request"); return(false); } if (waitForMessage(0x238, 1000, out msg) == 0x238) { //Ok, there seems to be a ECU somewhere out there. //Now, sleep for 10 seconds to get a session timeout. This is needed for //applications on higher level. Otherwise there will be no reply when the //higher level application tries to start a session. Thread.Sleep(10000); logger.Debug("sendSessionRequest: TRUE"); return(true); } logger.Debug("sendSessionRequest: FALSE"); return(false); }
/// <summary> /// waitForMessage waits for a specific CAN message give by a CAN id. /// </summary> /// <param name="a_canID">The CAN id to listen for</param> /// <param name="timeout">Listen timeout</param> /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param> /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns> private uint waitForMessage(uint a_canID, uint timeout, out Lawicel.CANUSB.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { if (r_canMsg.id == 0x00) { nrOfWait++; } else if (r_canMsg.id != a_canID) { continue; } return((uint)r_canMsg.id); } else if (readResult == Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new Lawicel.CANUSB.CANMsg(); return(0); }
/// <summary> /// sendMessage send a CANMessage. /// </summary> /// <param name="a_message">A CANMessage.</param> /// <returns>true on success, othewise false.</returns> override public bool sendMessage(CANMessage a_message) { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); int writeResult; AddToCanTrace("TX: " + msg.id.ToString("X4") + " " + msg.data.ToString("X16")); writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg); if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { AddToCanTrace("Message sent successfully"); return(true); } else { switch (writeResult) { case Lawicel.CANUSB.ERROR_CANUSB_COMMAND_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM"); break; case Lawicel.CANUSB.ERROR_CANUSB_INVALID_PARAM: AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM"); break; case Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE: AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE"); break; case Lawicel.CANUSB.ERROR_CANUSB_NOT_OPEN: AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN"); break; case Lawicel.CANUSB.ERROR_CANUSB_OPEN_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM"); break; case Lawicel.CANUSB.ERROR_CANUSB_TX_FIFO_FULL: AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL"); break; default: AddToCanTrace("Message failed to send: " + writeResult.ToString()); break; } return(false); } }
/// <summary> /// readMessages is the "run" method of this class. It reads all incomming messages /// and publishes them to registered ICANListeners. /// </summary> public void readMessages() { int readResult = 0; Lawicel.CANUSB.CANMsg r_canMsg = new Lawicel.CANUSB.CANMsg(); CANMessage canMessage = new CANMessage(); Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { if (acceptMessageId(r_canMsg.id)) { canMessage.setID(r_canMsg.id); canMessage.setLength(r_canMsg.len); canMessage.setTimeStamp(r_canMsg.timestamp); canMessage.setFlags(r_canMsg.flags); canMessage.setData(r_canMsg.data); lock (m_listeners) { AddToCanTrace(string.Format("RX: {0} {1}", canMessage.getID().ToString("X3"), canMessage.getData().ToString("X16"))); foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } } else if (readResult == Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } } }
/// <summary> /// Check if there is connection with a CAN bus. /// </summary> /// <returns>true on connection, otherwise false</returns> private bool boxIsThere() { // TODO T8 disabled this method, always returned true Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); if (waitAnyMessage(2000, out msg) != 0) { Console.WriteLine("A message was seen"); return(true); } if (sendSessionRequest()) { Console.WriteLine("Session request success"); return(true); } Console.WriteLine("Box not there"); return(false); }
/// <summary> /// sendMessage send a CANMessage. /// </summary> /// <param name="a_message">A CANMessage.</param> /// <returns>true on success, othewise false.</returns> override public bool sendMessage(CANMessage a_message) { lock (lockObj) { while (interfaceBusy) { if (lastSentTimestamp < Environment.TickCount - timeoutWithoutReadyChar) { //Console.WriteLine("released"); break; } } lastSentTimestamp = Environment.TickCount; interfaceBusy = true; //Console.WriteLine("set"); Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); if (m_serialPort.IsOpen) { //m_serialPort.Write("\r"); string txstring = "t"; txstring += msg.id.ToString("X3"); txstring += "8"; // always 8 bytes to transmit for (int t = 0; t < 8; t++) { byte b = (byte)(((msg.data >> t * 8) & 0x0000000000000000FF)); txstring += b.ToString("X2"); } txstring += "\r"; AddToCanTrace(string.Format("TX: {0} {1} {2}", a_message.getID().ToString("X3"), a_message.getData().ToString("X16"), txstring)); m_serialPort.Write(txstring); return(true); } } return(false); }
/// <summary> /// sendMessage send a CANMessage. /// </summary> /// <param name="a_message">A CANMessage.</param> /// <returns>true on success, othewise false.</returns> override protected bool sendMessageDevice(CANMessage a_message) { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); int writeResult; writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg); if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { return(true); } else { logger.Debug("tx failed writeResult: " + writeResult); return(false); } }
/// <summary> /// readMessages is the "run" method of this class. It reads all incomming messages /// and publishes them to registered ICANListeners. /// </summary> public void readMessages() { int readResult = 0; Lawicel.CANUSB.CANMsg r_canMsg = new Lawicel.CANUSB.CANMsg(); CANMessage canMessage = new CANMessage(); logger.Debug("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { logger.Debug("readMessages thread ended"); return; } } readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { if (acceptMessageId(r_canMsg.id)) { canMessage.setID(r_canMsg.id); canMessage.setLength(r_canMsg.len); canMessage.setTimeStamp(r_canMsg.timestamp); canMessage.setFlags(r_canMsg.flags); canMessage.setData(r_canMsg.data); receivedMessage(canMessage); } } else if (readResult == Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } } }
/// <summary> /// waitForMessage waits for a specific CAN message give by a CAN id. /// </summary> /// <param name="a_canID">The CAN id to listen for</param> /// <param name="timeout">Listen timeout</param> /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param> /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns> public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { Lawicel.CANUSB.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new Lawicel.CANUSB.CANMsg(); readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { Thread.Sleep(1); logger.Trace("rx: 0x" + r_canMsg.id.ToString("X3") + r_canMsg.data.ToString("X16")); if (r_canMsg.id == 0x00) { nrOfWait++; } else if (r_canMsg.id != a_canID) { continue; } canMsg.setData(r_canMsg.data); canMsg.setID(r_canMsg.id); canMsg.setLength(r_canMsg.len); return((uint)r_canMsg.id); } else if (readResult == Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new Lawicel.CANUSB.CANMsg(); return(0); }
/// <summary> /// waitForMessage waits for a specific CAN message give by a CAN id. /// </summary> /// <param name="a_canID">The CAN id to listen for</param> /// <param name="timeout">Listen timeout</param> /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param> /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns> private uint waitForMessage(uint a_canID, uint timeout, out Lawicel.CANUSB.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { if (r_canMsg.id == 0x00) { nrOfWait++; } else if (r_canMsg.id != a_canID) continue; return (uint)r_canMsg.id; } else if (readResult == Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new Lawicel.CANUSB.CANMsg(); return 0; }
/// <summary> /// Send a message that starts a session. This is used to test if there is /// a connection. /// </summary> /// <returns></returns> private bool sendSessionRequest() { Console.WriteLine("Sending session request"); // 0x220 is for T7 // 0x7E0 is for T8 CANMessage msg1 = new CANMessage(0x220, 0, 8); Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg1.setData(0x000040021100813f); if (!sendMessage(msg1)) { Console.WriteLine("Unable to send session request"); return false; } if (waitForMessage(0x238, 1000, out msg) == 0x238) { //Ok, there seems to be a ECU somewhere out there. //Now, sleep for 10 seconds to get a session timeout. This is needed for //applications on higher level. Otherwise there will be no reply when the //higher level application tries to start a session. Thread.Sleep(10000); Console.WriteLine("sendSessionRequest: TRUE"); return true; } Console.WriteLine("sendSessionRequest: FALSE"); return false; }
/// <summary> /// Check if there is connection with a CAN bus. /// </summary> /// <returns>true on connection, otherwise false</returns> private bool boxIsThere() { // TODO T8 disabled this method, always returned true Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); if (waitAnyMessage(2000, out msg) != 0) { Console.WriteLine("A message was seen"); return true; } if (sendSessionRequest()) { Console.WriteLine("Session request success"); return true; } Console.WriteLine("Box not there"); return false; }
/// <summary> /// waitForMessage waits for a specific CAN message give by a CAN id. /// </summary> /// <param name="a_canID">The CAN id to listen for</param> /// <param name="timeout">Listen timeout</param> /// <param name="r_canMsg">The CAN message with a_canID that we where listening for.</param> /// <returns>The CAN id for the message we where listening for, otherwise 0.</returns> public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { Lawicel.CANUSB.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new Lawicel.CANUSB.CANMsg(); readResult = Lawicel.CANUSB.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { Thread.Sleep(1); AddToCanTrace("rx: 0x" + r_canMsg.id.ToString("X4") + r_canMsg.data.ToString("X16")); if (r_canMsg.id == 0x00) { nrOfWait++; } else if (r_canMsg.id != a_canID) continue; canMsg.setData(r_canMsg.data); canMsg.setID(r_canMsg.id); canMsg.setLength(r_canMsg.len); return (uint)r_canMsg.id; } else if (readResult == Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new Lawicel.CANUSB.CANMsg(); return 0; }
/// <summary> /// sendMessage send a CANMessage. /// </summary> /// <param name="a_message">A CANMessage.</param> /// <returns>true on success, othewise false.</returns> public override bool sendMessage(CANMessage a_message) { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); int writeResult; AddToCanTrace("TX: " + msg.id.ToString("X4") + " " + msg.data.ToString("X16")); writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg); if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { AddToCanTrace("Message sent successfully"); return true; } else { switch (writeResult) { case Lawicel.CANUSB.ERROR_CANUSB_COMMAND_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM"); break; case Lawicel.CANUSB.ERROR_CANUSB_INVALID_PARAM: AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM"); break; case Lawicel.CANUSB.ERROR_CANUSB_NO_MESSAGE: AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE"); break; case Lawicel.CANUSB.ERROR_CANUSB_NOT_OPEN: AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN"); break; case Lawicel.CANUSB.ERROR_CANUSB_OPEN_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM"); break; case Lawicel.CANUSB.ERROR_CANUSB_TX_FIFO_FULL: AddToCanTrace("Message failed to send: ERROR_CANUSB_TX_FIFO_FULL"); break; default: AddToCanTrace("Message failed to send: " + writeResult.ToString()); break; } return false; } }
/// <summary> /// sendMessage send a CANMessage. /// </summary> /// <param name="a_message">A CANMessage.</param> /// <returns>true on success, othewise false.</returns> public override bool sendMessage(CANMessage a_message) { lock (lockObj) { while (interfaceBusy) { if (lastSentTimestamp < Environment.TickCount - timeoutWithoutReadyChar) { //Console.WriteLine("released"); break; } } lastSentTimestamp = Environment.TickCount; interfaceBusy = true; //Console.WriteLine("set"); Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); if (m_serialPort.IsOpen) { //m_serialPort.Write("\r"); string txstring = "t"; txstring += msg.id.ToString("X3"); txstring += "8"; // always 8 bytes to transmit for (int t = 0; t < 8; t++) { byte b = (byte)(((msg.data >> t * 8) & 0x0000000000000000FF)); txstring += b.ToString("X2"); } txstring += "\r"; AddToCanTrace(string.Format("TX: {0} {1} {2}", a_message.getID().ToString("X3"), a_message.getData().ToString("X16"), txstring)); m_serialPort.Write(txstring); return true; } } return false; }
/// <summary> /// The open method tries to connect to both busses to see if one of them is connected and /// active. The first strategy is to listen for any CAN message. If this fails there is a /// check to see if the application is started after an interrupted flash session. This is /// done by sending a message to set address and length (only for P-bus). /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> override public OpenResult open() { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); if (!UseOnlyPBus) { logger.Debug("Lawicel.CANUSB.canusb_Open()"); if (TrionicECU == ECU.TRIONIC7) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_47K, // T7 i-bus Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else if (TrionicECU == ECU.TRIONIC8) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_33K, // GMLAN Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle != 0) { if (waitAnyMessage(1000, out msg) != 0) { logger.Debug("I bus connected"); m_readThread = new Thread(readMessages) { Name = "CANUSBDevice.m_readThread" }; if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } } Thread.Sleep(200); } close(); m_endThread = false; //I bus wasn't connected. //Check if P bus is connected logger.Debug("Lawicel.CANUSB.canusb_Open()"); m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, Lawicel.CANUSB.CAN_BAUD_500K, Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); if (m_deviceHandle == 0x00000000) { return(OpenResult.OpenError); } logger.Debug("P bus connected"); m_readThread = new Thread(readMessages) { Name = "CANUSBDevice.m_readThread" }; if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); }
// int thrdcnt = 0; /// <summary> /// readMessages is the "run" method of this class. It reads all incomming messages /// and publishes them to registered ICANListeners. /// </summary> public void readMessages() { while (true) { lock (m_synchObject) { if (m_endThread) { m_endThread = false; return; } } if (m_serialPort.IsOpen) { // read the status? string line = string.Empty; try { line = m_serialPort.ReadLine(); if (line.Length > 0) { if (line.Length == 25) { Lawicel.CANUSB.CANMsg r_canMsg = new Lawicel.CANUSB.CANMsg(); canMessage = new CANMessage(); // three bytes identifier r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16); r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16); ulong data = 0; // add all the bytes data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16); data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 1 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 2 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 3 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 4 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 5 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 6 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16) << 7 * 8; r_canMsg.data = data; canMessage.setID(r_canMsg.id); canMessage.setLength(r_canMsg.len); canMessage.setFlags(r_canMsg.flags); canMessage.setData(r_canMsg.data); lock (m_listeners) { AddToCanTrace(string.Format("RX: {0} {1} {2}", canMessage.getID().ToString("X3"), line.Substring(5, 16), line)); foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if (line.Contains("z")) { interfaceBusy = false; //Console.WriteLine("clear"); } else if (line.Length == 2 && Convert.ToInt32(line.Substring(1, 2), 16) != 0x07) { //Console.WriteLine("Send error"); } else { //Console.WriteLine("Unknown message: " + line); } } } catch (Exception E) { Console.WriteLine("Failed to read frames from CANbus: " + E.Message); } } //Thread.Sleep(2); Thread.Sleep(delayTimespan); // give others some air } }
/// <summary> /// sendMessage send a CANMessage. /// </summary> /// <param name="a_message">A CANMessage.</param> /// <returns>true on success, othewise false.</returns> protected override bool sendMessageDevice(CANMessage a_message) { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); int writeResult; writeResult = Lawicel.CANUSB.canusb_Write(m_deviceHandle, ref msg); if (writeResult == Lawicel.CANUSB.ERROR_CANUSB_OK) { return true; } else { logger.Debug("tx failed writeResult: " + writeResult); return false; } }
/// <summary> /// waitAnyMessage waits for any message to be received. /// </summary> /// <param name="timeout">Listen timeout</param> /// <param name="r_canMsg">The CAN message that was first received</param> /// <returns>The CAN id for the message received, otherwise 0.</returns> private uint waitAnyMessage(uint timeout, out Lawicel.CANUSB.CANMsg r_canMsg) { CANMessage canMessage = new CANMessage(); string line = string.Empty; int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { m_serialPort.Write("\r"); m_serialPort.Write("P\r"); bool endofFrames = false; while (!endofFrames) { Console.WriteLine("reading line"); line = m_serialPort.ReadLine(); Console.WriteLine("line: " + line + " len: " + line.Length.ToString()); if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A') { endofFrames = true; } else { if (line.Length == 14) { // three bytes identifier r_canMsg = new Lawicel.CANUSB.CANMsg(); r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16); r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16); ulong data = 0; // add all the bytes data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16) << 7 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 6 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 5 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 4 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 3 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 2 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 1 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16); r_canMsg.data = data; canMessage.setID(r_canMsg.id); canMessage.setLength(r_canMsg.len); canMessage.setFlags(0); canMessage.setData(r_canMsg.data); return (uint)r_canMsg.id; } } } //Thread.Sleep(0); nrOfWait++; } r_canMsg = new Lawicel.CANUSB.CANMsg(); return 0; }
/// <summary> /// The open method tries to connect to both busses to see if one of them is connected and /// active. The first strategy is to listen for any CAN message. If this fails there is a /// check to see if the application is started after an interrupted flash session. This is /// done by sending a message to set address and length (only for P-bus). /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> override public OpenResult open() { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); // Default to "Allow all" uint AcceptanceCode = Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL; uint AcceptanceMask = Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL; // ID 0x000 will thrash the filter calculation so bypass the whole filter if it's found foreach (var id in AcceptOnlyMessageIds) { if (id == 0) { m_filterBypass = true; } } if (m_filterBypass == false) { CalcAcceptanceFilters(out AcceptanceCode, out AcceptanceMask); } if (!UseOnlyPBus && TrionicECU != ECU.TRIONIC5) { logger.Debug("Lawicel.CANUSB.canusb_Open()"); if (TrionicECU == ECU.TRIONIC7) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_47K, // T7 i-bus AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else if (TrionicECU == ECU.TRIONIC8) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_33K, // GMLAN AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle != 0) { if (waitAnyMessage(1000, out msg) != 0) { logger.Debug("I bus connected"); m_readThread = new Thread(readMessages) { Name = "CANUSBDevice.m_readThread" }; if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } } Thread.Sleep(200); } close(); m_endThread = false; if (TrionicECU == ECU.TRIONIC5) { logger.Debug("Lawicel.CANUSB.canusb_Open()"); m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_615K, // T5, P-BUS AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else { //I bus wasn't connected. //Check if P bus is connected logger.Debug("Lawicel.CANUSB.canusb_Open()"); m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, Lawicel.CANUSB.CAN_BAUD_500K, AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle == 0x00000000) { return(OpenResult.OpenError); } logger.Debug("P bus connected"); m_readThread = new Thread(readMessages) { Name = "CANUSBDevice.m_readThread" }; if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); }
/// <summary> /// The open method tries to connect to both busses to see if one of them is connected and /// active. The first strategy is to listen for any CAN message. If this fails there is a /// check to see if the application is started after an interrupted flash session. This is /// done by sending a message to set address and length (only for P-bus). /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> override public OpenResult open() { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); // Note: setting these parameters in here means that tcanflash becomes unable to monitor all traffic with the logging feature. // Unfortunately canusb requires these parameters to be set AFTER the channel has been initialized but BEFORE it is open so setting them the same way as elm does is probably not possible..? // Default to "Allow all" uint AcceptanceCode = Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL; uint AcceptanceMask = Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL; // if (!Monitor) { CalcAcceptanceFilters(out AcceptanceCode, out AcceptanceMask); } if (!UseOnlyPBus && TrionicECU != ECU.TRIONIC5) { logger.Debug("Lawicel.CANUSB.canusb_Open()"); if (TrionicECU == ECU.TRIONIC7) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_47K, // T7 i-bus AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else if (TrionicECU == ECU.TRIONIC8) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_33K, // GMLAN AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle != 0) { if (waitAnyMessage(1000, out msg) != 0) { logger.Debug("I bus connected"); m_readThread = new Thread(readMessages) { Name = "CANUSBDevice.m_readThread" }; if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } } Thread.Sleep(200); } close(); m_endThread = false; if (TrionicECU == ECU.TRIONIC5) { logger.Debug("Lawicel.CANUSB.canusb_Open()"); m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, CAN_BAUD_BTR_615K, // T5, P-BUS AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else { //I bus wasn't connected. //Check if P bus is connected logger.Debug("Lawicel.CANUSB.canusb_Open()"); m_deviceHandle = Lawicel.CANUSB.canusb_Open(SelectedAdapter, Lawicel.CANUSB.CAN_BAUD_500K, AcceptanceCode, AcceptanceMask, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle == 0x00000000) { return(OpenResult.OpenError); } logger.Debug("P bus connected"); m_readThread = new Thread(readMessages) { Name = "CANUSBDevice.m_readThread" }; if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); }
// int thrdcnt = 0; /// <summary> /// readMessages is the "run" method of this class. It reads all incomming messages /// and publishes them to registered ICANListeners. /// </summary> public void readMessages() { while (true) { lock (m_synchObject) { if (m_endThread) { m_endThread = false; return; } } if (m_serialPort.IsOpen) { // read the status? string line = string.Empty; try { line = m_serialPort.ReadLine(); if (line.Length > 0) { if (line.Length == 25) { Lawicel.CANUSB.CANMsg r_canMsg = new Lawicel.CANUSB.CANMsg(); canMessage = new CANMessage(); // three bytes identifier r_canMsg.id = (uint)Convert.ToInt32(line.Substring(1, 3), 16); r_canMsg.len = (byte)Convert.ToInt32(line.Substring(4, 1), 16); ulong data = 0; // add all the bytes data |= (ulong)(byte)Convert.ToInt32(line.Substring(5, 2), 16); data |= (ulong)(byte)Convert.ToInt32(line.Substring(7, 2), 16) << 1 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(9, 2), 16) << 2 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(11, 2), 16) << 3 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(13, 2), 16) << 4 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(15, 2), 16) << 5 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(17, 2), 16) << 6 * 8; data |= (ulong)(byte)Convert.ToInt32(line.Substring(19, 2), 16) << 7 * 8; r_canMsg.data = data; canMessage.setID(r_canMsg.id); canMessage.setLength(r_canMsg.len); canMessage.setFlags(r_canMsg.flags); canMessage.setData(r_canMsg.data); lock (m_listeners) { AddToCanTrace(string.Format("RX: {0} {1} {2}", canMessage.getID().ToString("X3"), line.Substring(5, 16), line)); foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if(line.Contains("z")) { interfaceBusy = false; //Console.WriteLine("clear"); } else if (line.Length == 2 && Convert.ToInt32(line.Substring(1, 2), 16) != 0x07) { //Console.WriteLine("Send error"); } else { //Console.WriteLine("Unknown message: " + line); } } } catch (Exception E) { Console.WriteLine("Failed to read frames from CANbus: " + E.Message); } } //Thread.Sleep(2); Thread.Sleep(delayTimespan); // give others some air } }
/// <summary> /// The open method tries to connect to both busses to see if one of them is connected and /// active. The first strategy is to listen for any CAN message. If this fails there is a /// check to see if the application is started after an interrupted flash session. This is /// done by sending a message to set address and length (only for P-bus). /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> override public OpenResult open() { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); m_readThread = new Thread(readMessages); m_readThread.Name = "CANUSBDevice.m_readThread"; if (!UseOnlyPBus) { Console.WriteLine("Getting handle"); if (TrionicECU == ECU.TRIONIC7) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(IntPtr.Zero, CAN_BAUD_BTR_47K, // T7 i-bus Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else if (TrionicECU == ECU.TRIONIC8) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(IntPtr.Zero, CAN_BAUD_BTR_33K, // GMLAN Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle != 0) { if (waitAnyMessage(1000, out msg) != 0) { if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } } if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); } if (m_deviceHandle != 0) { close(); } m_endThread = false; //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = Lawicel.CANUSB.canusb_Open(IntPtr.Zero, Lawicel.CANUSB.CAN_BAUD_500K, Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); if (m_deviceHandle == 0x00000000) { return(OpenResult.OpenError); } if (DisableCanConnectionCheck || boxIsThere()) { Console.WriteLine("Box is there, starting thread"); if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } Console.WriteLine("Box not there"); close(); return(OpenResult.OpenError); }
/// <summary> /// The open method tries to connect to both busses to see if one of them is connected and /// active. The first strategy is to listen for any CAN message. If this fails there is a /// check to see if the application is started after an interrupted flash session. This is /// done by sending a message to set address and length (only for P-bus). /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> public override OpenResult open() { Lawicel.CANUSB.CANMsg msg = new Lawicel.CANUSB.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); m_readThread = new Thread(readMessages); m_readThread.Name = "CANUSBDevice.m_readThread"; if (!UseOnlyPBus) { Console.WriteLine("Getting handle"); if (TrionicECU == ECU.TRIONIC7) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(IntPtr.Zero, CAN_BAUD_BTR_47K, // T7 i-bus Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } else if (TrionicECU == ECU.TRIONIC8) { m_deviceHandle = Lawicel.CANUSB.canusb_Open(IntPtr.Zero, CAN_BAUD_BTR_33K, // GMLAN Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); } if (m_deviceHandle != 0) { if (waitAnyMessage(1000, out msg) != 0) { if (m_readThread.ThreadState == ThreadState.Unstarted) m_readThread.Start(); return OpenResult.OK; } } if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); } if (m_deviceHandle != 0) { close(); } m_endThread = false; //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = Lawicel.CANUSB.canusb_Open(IntPtr.Zero, Lawicel.CANUSB.CAN_BAUD_500K, Lawicel.CANUSB.CANUSB_ACCEPTANCE_CODE_ALL, Lawicel.CANUSB.CANUSB_ACCEPTANCE_MASK_ALL, Lawicel.CANUSB.CANUSB_FLAG_TIMESTAMP); if (m_deviceHandle == 0x00000000) { return OpenResult.OpenError; } if(DisableCanConnectionCheck || boxIsThere()) { Console.WriteLine("Box is there, starting thread"); if (m_readThread.ThreadState == ThreadState.Unstarted) m_readThread.Start(); return OpenResult.OK; } Console.WriteLine("Box not there"); close(); return OpenResult.OpenError; }