/// <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"); CANMessage msg1 = new CANMessage(0x220, 0, 8); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); msg1.setData(0x000040021100813f); if (!sendMessage(msg1)) { Console.WriteLine("Unable to send session request"); return(false); } if (waitForMessage(0x238, 1000, out msg) == 0x238) { Console.WriteLine("Message 0x238 seen"); //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); }
private void DumpCanMsg(LAWICEL.CANMsg r_canMsg, bool IsTransmit) { DateTime dt = DateTime.Now; try { using (StreamWriter sw = new StreamWriter(Path.Combine(m_startuppath, dt.Year.ToString("D4") + dt.Month.ToString("D2") + dt.Day.ToString("D2") + "-CanTrace.log"), true)) { if (IsTransmit) { // get the byte transmitted int transmitvalue = (int)(r_canMsg.data & 0x000000000000FF00); transmitvalue /= 256; sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " TX: id=" + r_canMsg.id.ToString("D2") + " len= " + r_canMsg.len.ToString("X8") + " data=" + r_canMsg.data.ToString("X16") + " " + r_canMsg.flags.ToString("X2") + " character = " + GetCharString(transmitvalue) + "\t ts: " + r_canMsg.timestamp.ToString("X16") + " flags: " + r_canMsg.flags.ToString("X2")); } else { // get the byte received int receivevalue = (int)(r_canMsg.data & 0x0000000000FF0000); receivevalue /= (256 * 256); sw.WriteLine(dt.ToString("dd/MM/yyyy HH:mm:ss") + " RX: id=" + r_canMsg.id.ToString("D2") + " len= " + r_canMsg.len.ToString("X8") + " data=" + r_canMsg.data.ToString("X16") + " " + r_canMsg.flags.ToString("X2") + " character = " + GetCharString(receivevalue) + "\t ts: " + r_canMsg.timestamp.ToString("X16") + " flags: " + r_canMsg.flags.ToString("X2")); } } } catch (Exception E) { Console.WriteLine("Failed to write to logfile: " + E.Message); } }
/// <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.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.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.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new LAWICEL.CANMsg(); return(0); }
/// <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.CANMsg r_canMsg) { CANMessage canMessage = new CANMessage(); string line = string.Empty; int nrOfWait = 0; while (nrOfWait < timeout) { m_port.Write("\r"); m_port.Write("P\r"); bool endofFrames = false; while (!endofFrames) { Console.WriteLine("reading line"); line = m_port.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.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.setTimeStamp(r_canMsg.timestamp); canMessage.setFlags(0); canMessage.setData(r_canMsg.data); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } return((uint)r_canMsg.id); } } } Thread.Sleep(1); nrOfWait++; } r_canMsg = new LAWICEL.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() { Console.WriteLine("******* CANUSB: Opening CANUSB"); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); if (!_useOnlyPBus) { Console.WriteLine("Getting handle"); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, "0xcb:0x9a", // Slow LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.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(); } //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, LAWICEL.CAN_BAUD_500K, //500Kb/s LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); if (boxIsThere()) { if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } close(); return(OpenResult.OpenError); }
/*private void AddToCanTrace(string line) * { * if (m_EnableCanLog) * { * DateTime dtnow = DateTime.Now; * using (StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\CanTraceCANUSBDevice.txt", true)) * { * sw.WriteLine(dtnow.ToString("dd/MM/yyyy HH:mm:ss") + " - " + line); * } * } * }*/ /// <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.CANMsg msg = new LAWICEL.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.flags = LAWICEL.CANMSG_EXTENDED; // Test for now msg.data = a_message.getData(); int writeResult; //AddToCanTrace("Sending message"); AddToCanTrace("TX: " + msg.id.ToString("X6") + " " + msg.data.ToString("X16")); writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg); if (writeResult == LAWICEL.ERROR_CANUSB_OK) { //AddToCanTrace("Message sent successfully"); txCount++; bitsPerSecond += 109; return(true); } else { errCount++; switch (writeResult) { case LAWICEL.ERROR_CANUSB_COMMAND_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM"); break; case LAWICEL.ERROR_CANUSB_INVALID_PARAM: AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM"); break; case LAWICEL.ERROR_CANUSB_NO_MESSAGE: AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE"); break; case LAWICEL.ERROR_CANUSB_NOT_OPEN: AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN"); break; case LAWICEL.ERROR_CANUSB_OPEN_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM"); break; case LAWICEL.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); } }
/*private void CastInformationEvent(CANMessage message) * { * if (onReceivedAdditionalInformationFrame != null) * { * onReceivedAdditionalInformationFrame(this, new InformationFrameEventArgs(message)); * } * } * * private bool MessageContainsInformationForRealtime(uint msgId) * { * bool retval = false; * switch (msgId) * { * case 0x1A0: //1A0h - Engine information * case 0x280: //280h - Pedals, reverse gear * case 0x290: //290h - Steering wheel and SID buttons * case 0x2F0: //2F0h - Vehicle speed * case 0x320: //320h - Doors, central locking and seat belts * case 0x370: //370h - Mileage * case 0x3A0: //3A0h - Vehicle speed * case 0x3B0: //3B0h - Head lights * case 0x3E0: //3E0h - Automatic Gearbox * case 0x410: //410h - Light dimmer and light sensor * case 0x430: //430h - SID beep request (interesting for Knock indicator?) * case 0x460: //460h - Engine rpm and speed * case 0x4A0: //4A0h - Steering wheel, Vehicle Identification Number * case 0x520: //520h - ACC, inside temperature * case 0x530: //530h - ACC * case 0x5C0: //5C0h - Coolant temperature, air pressure * case 0x630: //630h - Fuel usage * case 0x640: //640h - Mileage * case 0x7A0: //7A0h - Outside temperature * retval = true; * break; * } * return retval; * }*/ /// <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(bool is500KB) { Console.WriteLine("******* CANUSB: Opening CANUSB"); rxCount = 0; txCount = 0; errCount = 0; LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); m_readThread = new Thread(readMessages); if (m_deviceHandle != 0) { close(); } m_endThread = false; string speed = "250"; if (is500KB) { speed = "500"; } Console.WriteLine(speed); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, speed, LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); Console.WriteLine("Checking box presence"); Console.WriteLine("Handle: " + m_deviceHandle.ToString("X8")); if (m_deviceHandle == 0x00000000) { return(OpenResult.OpenError); } if (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> /// Check if there is connection with a CAN bus. /// </summary> /// <returns>true on connection, otherwise false</returns> private bool boxIsThere() { LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); if (waitAnyMessage(2000, out msg) != 0) { return(true); } if (sendSessionRequest()) { return(true); } return(false); }
/// <summary> /// Check if there is connection with a CAN bus. /// </summary> /// <returns>true on connection, otherwise false</returns> private bool boxIsThere() { LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); Console.WriteLine("in Box is there"); 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> /// 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.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { return((uint)r_canMsg.id); } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010> nrOfWait++; } } r_canMsg = new LAWICEL.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.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected close(); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, "0xcb:0x9a", LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); if (waitAnyMessage(1000, out msg) != 0) { if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } close(); //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, LAWICEL.CAN_BAUD_500K, LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); if (boxIsThere()) { if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } close(); return(OpenResult.OpenError); }
/// <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.CANMsg msg = new LAWICEL.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); if (m_DoLogging) { DumpCanMsg(msg, true); } if (m_port.IsOpen) { m_port.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"; m_port.Write(txstring); // Console.WriteLine("Send: " + txstring); return(true); } return(false); /* * int writeResult; * writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg); * if (writeResult == LAWICEL.ERROR_CANUSB_OK) * return true; * else * 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.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); while (true) { lock (m_synchObject) { if (m_endThread) { return; } } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { 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) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } } }
/// <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.CANMsg msg = new LAWICEL.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); if (m_DoLogging) { DumpCanMsg(msg, true); } int writeResult; writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg); if (writeResult == LAWICEL.ERROR_CANUSB_OK) { return(true); } else { 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.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); while (true) { lock (m_synchObject) { if (m_endThread) return; } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { 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) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } } }
/// <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.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected close(); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, "0xcb:0x9a", LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); if (waitAnyMessage(1000, out msg) != 0) { if (m_readThread.ThreadState == ThreadState.Unstarted) m_readThread.Start(); return OpenResult.OK; } close(); //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, LAWICEL.CAN_BAUD_500K, LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); if (boxIsThere()) { if(m_readThread.ThreadState == ThreadState.Unstarted) m_readThread.Start(); return OpenResult.OK; } close(); return OpenResult.OpenError; }
/// <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) { /* int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); canMsg = new CANMessage(); readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { //Console.WriteLine("rx id: 0x" + r_canMsg.id.ToString("X4")); if (r_canMsg.id != a_canID) { nrOfWait++; continue; } else { canMsg.setID(r_canMsg.id); canMsg.setData(r_canMsg.data); canMsg.setFlags(r_canMsg.flags); return (uint)r_canMsg.id; } } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } canMsg = new CANMessage(); return 0;*/ LAWICEL.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new LAWICEL.CANMsg(); readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.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.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new LAWICEL.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.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { if (r_canMsg.id != a_canID) continue; return (uint)r_canMsg.id; } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010> nrOfWait++; } } r_canMsg = new LAWICEL.CANMsg(); return 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() { int readResult = 0; LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); /* Stopwatch while (true) { lock (m_synchObject) { if (m_endThread) return; } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult > 0) { 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); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } lock (m_listeners) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); } }*/ while (true) { /*if ((thrdcnt++ % 1000) == 0) { Console.WriteLine("Reading messages"); }*/ lock (m_synchObject) { if (m_endThread) { m_endThread = false; return; } } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult > 0) { 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); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } lock (m_listeners) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010> } } }
/// <summary> /// Send a message that starts a session. This is used to test if there is /// a connection. /// </summary> /// <returns></returns> private bool sendSessionRequest() { CANMessage msg1 = new CANMessage(0x220, 0, 8); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); msg1.setData(0x000040021100813f); if (!sendMessage(msg1)) 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); return true; } 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.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); /* Stopwatch * while (true) * { * lock (m_synchObject) * { * if (m_endThread) * return; * } * readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); * if (readResult > 0) * { * 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); * if (m_DoLogging) * { * DumpCanMsg(r_canMsg, false); * } * lock (m_listeners) * { * foreach (ICANListener listener in m_listeners) * { * listener.handleMessage(canMessage); * } * } * } * else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) * { * Thread.Sleep(1); * } * }*/ while (true) { /*if ((thrdcnt++ % 1000) == 0) * { * Console.WriteLine("Reading messages"); * }*/ lock (m_synchObject) { if (m_endThread) { m_endThread = false; return; } } readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult > 0) { 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); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } lock (m_listeners) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact <GS-13122010> } } }
/// <summary> /// Check if there is connection with a CAN bus. /// </summary> /// <returns>true on connection, otherwise false</returns> private bool boxIsThere() { LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); Console.WriteLine("in Box is there"); 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> public override bool sendMessage(CANMessage a_message) { LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); if (m_DoLogging) { DumpCanMsg(msg, true); } if (m_port.IsOpen) { m_port.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"; m_port.Write(txstring); // Console.WriteLine("Send: " + txstring); return true; } return false; /* int writeResult; writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg); if (writeResult == LAWICEL.ERROR_CANUSB_OK) return true; else return false; */ }
/*private void CastInformationEvent(CANMessage message) { if (onReceivedAdditionalInformationFrame != null) { onReceivedAdditionalInformationFrame(this, new InformationFrameEventArgs(message)); } } private bool MessageContainsInformationForRealtime(uint msgId) { bool retval = false; switch (msgId) { case 0x1A0: //1A0h - Engine information case 0x280: //280h - Pedals, reverse gear case 0x290: //290h - Steering wheel and SID buttons case 0x2F0: //2F0h - Vehicle speed case 0x320: //320h - Doors, central locking and seat belts case 0x370: //370h - Mileage case 0x3A0: //3A0h - Vehicle speed case 0x3B0: //3B0h - Head lights case 0x3E0: //3E0h - Automatic Gearbox case 0x410: //410h - Light dimmer and light sensor case 0x430: //430h - SID beep request (interesting for Knock indicator?) case 0x460: //460h - Engine rpm and speed case 0x4A0: //4A0h - Steering wheel, Vehicle Identification Number case 0x520: //520h - ACC, inside temperature case 0x530: //530h - ACC case 0x5C0: //5C0h - Coolant temperature, air pressure case 0x630: //630h - Fuel usage case 0x640: //640h - Mileage case 0x7A0: //7A0h - Outside temperature retval = true; break; } return retval; }*/ /// <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(bool is500KB) { Console.WriteLine("******* CANUSB: Opening CANUSB"); rxCount = 0; txCount = 0; errCount = 0; LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); m_readThread = new Thread(readMessages); if (m_deviceHandle != 0) { close(); } m_endThread = false; string speed = "250"; if (is500KB) speed = "500"; Console.WriteLine(speed); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, speed, LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); Console.WriteLine("Checking box presence"); Console.WriteLine("Handle: "+ m_deviceHandle.ToString("X8")); if (m_deviceHandle == 0x00000000) { return OpenResult.OpenError; } if (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; }
/*private void CastInformationEvent(CANMessage message) { if (onReceivedAdditionalInformationFrame != null) { onReceivedAdditionalInformationFrame(this, new InformationFrameEventArgs(message)); } } private bool MessageContainsInformationForRealtime(uint msgId) { bool retval = false; switch (msgId) { case 0x1A0: //1A0h - Engine information case 0x280: //280h - Pedals, reverse gear case 0x290: //290h - Steering wheel and SID buttons case 0x2F0: //2F0h - Vehicle speed case 0x320: //320h - Doors, central locking and seat belts case 0x370: //370h - Mileage case 0x3A0: //3A0h - Vehicle speed case 0x3B0: //3B0h - Head lights case 0x3E0: //3E0h - Automatic Gearbox case 0x410: //410h - Light dimmer and light sensor case 0x430: //430h - SID beep request (interesting for Knock indicator?) case 0x460: //460h - Engine rpm and speed case 0x4A0: //4A0h - Steering wheel, Vehicle Identification Number case 0x520: //520h - ACC, inside temperature case 0x530: //530h - ACC case 0x5C0: //5C0h - Coolant temperature, air pressure case 0x630: //630h - Fuel usage case 0x640: //640h - Mileage case 0x7A0: //7A0h - Outside temperature retval = true; break; } return retval; }*/ /// <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() { Console.WriteLine("******* CANUSB: Opening CANUSB"); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); m_readThread = new Thread(readMessages); if (!this.UseOnlyPBus) { Console.WriteLine("Getting handle"); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, "0xcb:0x9a", // Slow LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.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; Console.WriteLine("No I bus, checking P bus"); //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, LAWICEL.CAN_BAUD_500K, //500Kb/s LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); Console.WriteLine("Checking box presence"); Console.WriteLine("Handle: "+ m_deviceHandle.ToString("X8")); if (m_deviceHandle == 0x00000000) { return OpenResult.OpenError; } if (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> /// 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(0x7E0, 0, 8); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); msg1.setData(0x000040021100813f); if (!sendMessage(msg1)) { Console.WriteLine("Unable to send session request"); return false; } if (waitForMessage(0x7E8, 1000, out msg) == 0x7E8) { Console.WriteLine("Message 0x7E8 seen"); //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> /// 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.CANMsg msg = new LAWICEL.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); int writeResult; AddToCanTrace("Sending message"); writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg); if (writeResult == LAWICEL.ERROR_CANUSB_OK) { AddToCanTrace("Message sent successfully"); return true; } else { switch (writeResult) { case LAWICEL.ERROR_CANUSB_COMMAND_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM"); break; case LAWICEL.ERROR_CANUSB_INVALID_PARAM: AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM"); break; case LAWICEL.ERROR_CANUSB_NO_MESSAGE: AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE"); break; case LAWICEL.ERROR_CANUSB_NOT_OPEN: AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN"); break; case LAWICEL.ERROR_CANUSB_OPEN_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM"); break; case LAWICEL.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> /// Check if there is connection with a CAN bus. /// </summary> /// <returns>true on connection, otherwise false</returns> private bool boxIsThere() { LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); if (waitAnyMessage(2000, out msg) != 0) return true; if (sendSessionRequest()) return true; 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) { /* * int readResult = 0; * int nrOfWait = 0; * while (nrOfWait < timeout) * { * LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); * canMsg = new CANMessage(); * readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); * if (readResult == LAWICEL.ERROR_CANUSB_OK) * { * //Console.WriteLine("rx id: 0x" + r_canMsg.id.ToString("X4")); * if (r_canMsg.id != a_canID) * { * nrOfWait++; * continue; * } * else * { * canMsg.setID(r_canMsg.id); * canMsg.setData(r_canMsg.data); * canMsg.setFlags(r_canMsg.flags); * return (uint)r_canMsg.id; * } * } * else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) * { * Thread.Sleep(1); * nrOfWait++; * } * } * canMsg = new CANMessage(); * return 0;*/ LAWICEL.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new LAWICEL.CANMsg(); readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.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.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new LAWICEL.CANMsg(); return(0); }
/// <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.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = LAWICEL.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { return (uint)r_canMsg.id; } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new LAWICEL.CANMsg(); return 0; }
// 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() { //int readResult = 0; LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); while (true) { lock (m_synchObject) { if (m_endThread) { m_endThread = false; return; } } if (m_port.IsOpen) { // read the status? string line = string.Empty; m_port.Write("\r"); m_port.Write("A\r"); // poll for all pending CAN messages //Console.WriteLine("Polled for frames"); Thread.Sleep(0); bool endofFrames = false; while (!endofFrames) { try { if (m_port.BytesToRead > 0) { line = m_port.ReadLine(); Console.Write("RX: "); for (int tel = 0; tel < line.Length; tel++) { byte b = (byte)line[tel]; Console.Write(b.ToString("X2") + " "); } Console.WriteLine(""); Thread.Sleep(0); if (line.Length > 0) { if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A') { //Console.WriteLine("End of messages"); endofFrames = true; } else { //t00C8C6003E0000000000 //Console.WriteLine("line: " + line + " len: " + line.Length.ToString()); if (line.Length == 21) { // 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.setTimeStamp(r_canMsg.timestamp); canMessage.setFlags(r_canMsg.flags); canMessage.setData(r_canMsg.data); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } lock (m_listeners) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } } } } } catch (Exception E) { //Console.WriteLine("Failed to read frames from CANbus: " + E.Message); } Thread.Sleep(0); } } Thread.Sleep(1); } }
/// <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.CANMsg msg = new LAWICEL.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); if (m_DoLogging) { DumpCanMsg(msg, true); } int writeResult; writeResult = LAWICEL.canusb_Write(m_deviceHandle, ref msg); if (writeResult == LAWICEL.ERROR_CANUSB_OK) return true; else 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.CANMsg r_canMsg = new LAWICEL.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_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { //Console.WriteLine(r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16")); //if (MessageContainsInformationForRealtime(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) { bitsPerSecond += 109; AddToCanTrace("RX: " + r_canMsg.id.ToString("X6") + " " + r_canMsg.data.ToString("X16")); rxCount++; foreach (ICANListener listener in m_listeners) { //while (listener.messagePending()) ; // dirty, make this better listener.handleMessage(canMessage); } CastInformationEvent("", rxCount, txCount, errCount); // <GS-05042011> re-activated this function } //Thread.Sleep(1); } // cast event to application to process message //if (MessageContainsInformationForRealtime(r_canMsg.id)) //{ //TODO: process all other known msg id's into the realtime view // CastInformationEvent(canMessage); // <GS-05042011> re-activated this function //} } else if (readResult == LAWICEL.ERROR_CANUSB_NO_MESSAGE) { // Console.WriteLine("No message"); Thread.Sleep(1); } else { Console.WriteLine("Result: " + readResult.ToString("X8")); } /*int stat = LAWICEL.canusb_Status(m_deviceHandle); * if (stat != 0) * { * Console.WriteLine("status: " + stat.ToString("X4")); * }*/ } }
// 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() { //int readResult = 0; LAWICEL.CANMsg r_canMsg = new LAWICEL.CANMsg(); CANMessage canMessage = new CANMessage(); while (true) { lock (m_synchObject) { if (m_endThread) { m_endThread = false; return; } } if (m_port.IsOpen) { // read the status? string line = string.Empty; m_port.Write("\r"); m_port.Write("A\r"); // poll for all pending CAN messages //Console.WriteLine("Polled for frames"); Thread.Sleep(0); bool endofFrames = false; while(!endofFrames) { try { if (m_port.BytesToRead > 0) { line = m_port.ReadLine(); Console.Write("RX: "); for (int tel = 0; tel < line.Length; tel++) { byte b = (byte)line[tel]; Console.Write(b.ToString("X2") + " " ); } Console.WriteLine("") ; Thread.Sleep(0); if (line.Length > 0) { if (line[0] == '\x07' || line[0] == '\r' || line[0] == 'A') { //Console.WriteLine("End of messages"); endofFrames = true; } else { //t00C8C6003E0000000000 //Console.WriteLine("line: " + line + " len: " + line.Length.ToString()); if (line.Length == 21) { // 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.setTimeStamp(r_canMsg.timestamp); canMessage.setFlags(r_canMsg.flags); canMessage.setData(r_canMsg.data); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } lock (m_listeners) { foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } } } } } catch (Exception E) { //Console.WriteLine("Failed to read frames from CANbus: " + E.Message); } Thread.Sleep(0); } } Thread.Sleep(1); } }
/// <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.CANMsg r_canMsg = new LAWICEL.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_Read(m_deviceHandle, out r_canMsg); if (readResult == LAWICEL.ERROR_CANUSB_OK) { // Console.WriteLine(r_canMsg.id.ToString("X3") + " " + r_canMsg.data.ToString("X8")); if (MessageContainsInformationForRealtime(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("RX: " + r_canMsg.id.ToString("X4") + " " + r_canMsg.data.ToString("X16")); foreach (ICANListener listener in m_listeners) { //while (listener.messagePending()) ; // dirty, make this better listener.handleMessage(canMessage); } //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function } //Thread.Sleep(1); } // cast event to application to process message //if (MessageContainsInformationForRealtime(r_canMsg.id)) //{ //TODO: process all other known msg id's into the realtime view // CastInformationEvent(canMessage); // <GS-05042011> re-activated this function //} } else if (readResult == LAWICEL.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> private uint waitForMessage(uint a_canID, uint timeout, out LAWICEL.CANMsg r_canMsg) { CANMessage canMessage = new CANMessage(); string line = string.Empty; int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { m_port.Write("\r"); m_port.Write("P\r"); bool endofFrames = false; while (!endofFrames) { Console.WriteLine("reading line"); line = m_port.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.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.setTimeStamp(0); canMessage.setFlags(0); canMessage.setData(r_canMsg.data); if (m_DoLogging) { DumpCanMsg(r_canMsg, false); } if (r_canMsg.id != a_canID) continue; return (uint)r_canMsg.id; } } } Thread.Sleep(1); nrOfWait++; } r_canMsg = new LAWICEL.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> public override OpenResult open() { Console.WriteLine("******* CANUSB: Opening CANUSB"); LAWICEL.CANMsg msg = new LAWICEL.CANMsg(); //Check if I bus is connected if (m_deviceHandle != 0) { close(); } Thread.Sleep(200); if (!_useOnlyPBus) { Console.WriteLine("Getting handle"); m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, "0xcb:0x9a", // Slow LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.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(); } //I bus wasn't connected. //Check if P bus is connected m_deviceHandle = LAWICEL.canusb_Open(IntPtr.Zero, LAWICEL.CAN_BAUD_500K, //500Kb/s LAWICEL.CANUSB_ACCEPTANCE_CODE_ALL, LAWICEL.CANUSB_ACCEPTANCE_MASK_ALL, LAWICEL.CANUSB_FLAG_TIMESTAMP); if (boxIsThere()) { if (m_readThread.ThreadState == ThreadState.Unstarted) m_readThread.Start(); return OpenResult.OK; } close(); return OpenResult.OpenError; }