/// <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 canMessage) { canMessage = new CANMessage(); Debug.Assert(canMessage != null); int wait_cnt = 0; uint id; byte length; ulong data; while (wait_cnt < timeout) { if (MctAdapter_ReceiveMessage(out id, out length, out data)) { // message received canMessage.setID(id); canMessage.setLength(length); canMessage.setData(data); if (canMessage.getID() != a_canID) { continue; } return((uint)canMessage.getID()); } // wait a bit Thread.Sleep(1); ++wait_cnt; } // nothing was received 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 CANMessage canMessage) { canMessage = new CANMessage(); Debug.Assert(canMessage != null); int wait_cnt = 0; uint id; byte length; ulong data; while (wait_cnt < timeout) { if (MctAdapter_ReceiveMessage(out id, out length, out data)) { // message received canMessage.setID(id); canMessage.setLength(length); canMessage.setData(data); return(id); } // wait a bit Thread.Sleep(1); ++wait_cnt; } // nothing was received 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> public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { EASYSYNC.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new EASYSYNC.CANMsg(); readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.ERROR_CANUSB_OK) { 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 == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new EASYSYNC.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> 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); }
public void readMessages() { CANMessage canMessage = new CANMessage(); string rxMessage = string.Empty; Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } try { if (m_serialPort.IsOpen) { do { rxMessage = m_serialPort.ReadLine(); rxMessage = rxMessage.Replace("\r", ""); // remove prompt characters... we don't need that stuff rxMessage = rxMessage.Replace("\n", ""); // remove prompt characters... we don't need that stuff } while (rxMessage.StartsWith("w") == false); uint id = Convert.ToUInt32(rxMessage.Substring(1, 3), 16); if (acceptMessageId(id)) { canMessage.setID(id); canMessage.setLength(8); canMessage.setData(0x0000000000000000); for (uint i = 0; i < 8; i++) { canMessage.setCanData(Convert.ToByte(rxMessage.Substring(5 + (2 * (int)i), 2), 16), i); } lock (m_listeners) { AddToCanTrace("RX: " + canMessage.getID().ToString("X3") + " " + canMessage.getLength().ToString("X1") + " " + canMessage.getData().ToString("X16")); //Console.WriteLine("MSG: " + rxMessage); foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } } } catch (Exception) { Console.WriteLine("MSG: " + rxMessage); } } }
//------------------------------------------------------------------------- /** * Handles incoming messages. */ private void read_messages() { uint id; byte length; ulong data; CANMessage msg = new CANMessage(); Debug.Assert(msg != null); // main loop while (true) { // check for thread termination request Debug.Assert(this.term_mutex != null); lock (this.term_mutex) { if (this.term_requested) { return; } } // receive messages while (MctAdapter_ReceiveMessage(out id, out length, out data)) { if (acceptMessageId(id)) { // convert message msg.setID(id); msg.setLength(length); msg.setData(data); // pass message to listeners lock (this.m_listeners) { AddToCanTrace("RX: " + id.ToString("X4") + " " + data.ToString("X16")); foreach (ICANListener listener in this.m_listeners) { listener.handleMessage(msg); } } } } // give up CPU for a moment Thread.Sleep(1); } }
public void readMessages() { CANMessage canMessage = new CANMessage(); string rxMessage = string.Empty; logger.Debug("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { logger.Debug("readMessages ended"); return; } } try { if (m_serialPort.IsOpen) { do { rxMessage = m_serialPort.ReadLine(); rxMessage = rxMessage.Replace("\r", ""); // remove prompt characters... we don't need that stuff rxMessage = rxMessage.Replace("\n", ""); // remove prompt characters... we don't need that stuff } while (rxMessage.StartsWith("w") == false); uint id = Convert.ToUInt32(rxMessage.Substring(1, 3), 16); if (acceptMessageId(id)) { canMessage.setID(id); canMessage.setLength(8); canMessage.setData(0x0000000000000000); for (uint i = 0; i < 8; i++) { canMessage.setCanData(Convert.ToByte(rxMessage.Substring(5 + (2 * (int)i), 2), 16), i); } receivedMessage(canMessage); } } } catch (Exception) { logger.Debug("MSG: " + rxMessage); } } }
/// <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); } } }
//--------------------------------------------------------------------------------------------- /** * Waits for arrival of a specific CAN message or any message if ID = 0. * * @param a_canID message ID * @param timeout timeout, ms * @param canMsg message * * @return message ID */ public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { canMsg = new CANMessage(); Debug.Assert(canMsg != null); canMsg.setID(0); caCombiAdapter.caCANFrame frame = new caCombiAdapter.caCANFrame(); if (combi.CAN_GetMessage(ref frame, timeout) && (frame.id == a_canID || a_canID == 0)) { // message received canMsg.setID(frame.id); canMsg.setLength(frame.length); canMsg.setData(frame.data); return(frame.id); } // timed out return(0); }
//--------------------------------------------------------------------------------------------- /** * Handles incoming messages. */ private void read_messages() { caCombiAdapter.caCANFrame frame = new caCombiAdapter.caCANFrame(); // main loop while (true) { // check for thread termination request Debug.Assert(term_mutex != null); lock (term_mutex) { if (term_requested) { // exit logger.Debug("Reader thread ended"); return; } } // receive messages if (combi.CAN_GetMessage(ref frame, 1000)) { if (acceptMessageId(frame.id)) { // convert message in_msg.setID(frame.id); in_msg.setLength(frame.length); in_msg.setData(frame.data); receivedMessage(in_msg); } } else { 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.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> /// 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; EASYSYNC.CANMsg r_canMsg = new EASYSYNC.CANMsg(); CANMessage canMessage = new CANMessage(); while (true) { lock (m_synchObject) { if (m_endThread) { return; } } readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.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 == EASYSYNC.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); }
public void readMessages() { CANMessage canMessage = new CANMessage(); string rxMessage = string.Empty; Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } try { if (m_serialPort.IsOpen) { do { rxMessage = m_serialPort.ReadLine(); rxMessage = rxMessage.Replace("\r", ""); // remove prompt characters... we don't need that stuff rxMessage = rxMessage.Replace("\n", ""); // remove prompt characters... we don't need that stuff } while (rxMessage.StartsWith("w") == false); uint id = Convert.ToUInt32(rxMessage.Substring(1, 3), 16); if (acceptMessageId(id)) { canMessage.setID(id); canMessage.setLength(8); canMessage.setData(0x0000000000000000); for (uint i = 0; i < 8; i++) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(5 + (2 * (int)i), 2), 16), i); lock (m_listeners) { AddToCanTrace("RX: " + canMessage.getID().ToString("X3") + " " + canMessage.getLength().ToString("X1") + " " + canMessage.getData().ToString("X16")); //Console.WriteLine("MSG: " + rxMessage); foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } } } catch (Exception) { Console.WriteLine("MSG: " + rxMessage); } } }
// 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 } }
//--------------------------------------------------------------------------------------------- /** Waits for arrival of a specific CAN message or any message if ID = 0. @param a_canID message ID @param timeout timeout, ms @param canMsg message @return message ID */ public override uint waitForMessage(uint a_canID, uint timeout, out CANMessage canMsg) { canMsg = new CANMessage(); Debug.Assert(canMsg != null); canMsg.setID(0); caCombiAdapter.caCANFrame frame = new caCombiAdapter.caCANFrame(); if (this.combi.CAN_GetMessage(ref frame, timeout) && (frame.id == a_canID || a_canID == 0)) { // message received canMsg.setID(frame.id); canMsg.setLength(frame.length); canMsg.setData(frame.data); return frame.id; } // timed out 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.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> /// 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) { EASYSYNC.CANMsg r_canMsg; canMsg = new CANMessage(); int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { r_canMsg = new EASYSYNC.CANMsg(); readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.ERROR_CANUSB_OK) { 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 == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new EASYSYNC.CANMsg(); return 0; }
public void readMessages() { CANMessage canMessage = new CANMessage(); StringBuilder receiveText = new StringBuilder(); Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } receiveDataSemaphore.WaitOne(); if (m_serialPort != null) { if (m_serialPort.IsOpen) { if (m_serialPort.BytesToRead > 0) { string rawString = m_serialPort.ReadExisting(); //AddToSerialTrace("RAW RX: " + rawString.Replace("\r",m_cr_sequence)); string rxString = rawString.Replace("\n", "").Replace(">", "");// remove prompt characters... we don't need that stuff bool isStopped = false; if (rxString.Length > 0) { rxString = rxString.Replace("\r", m_cr_sequence); //replace , because stringbuilder cannot handle \r receiveText.Append(rxString); //AddToSerialTrace("RECEIVE TEXT: " + receiveText.ToString()); //System.Diagnostics.Debug.WriteLine("SERMSG: " + receiveText); var lines = ExtractLines(ref receiveText); foreach (var rxMessage in lines) { if (rxMessage.StartsWith("STOPPED")) { isStopped = true; } else if (rxMessage.StartsWith("NO DATA")) { } //skip it else if (rxMessage.StartsWith("CAN ERROR")) { //handle error? } else if (rxMessage.StartsWith("ELM")) { isStopped = false; } //skip it, this is a trick to stop ELM from listening to more messages and send ready char else if (rxMessage.StartsWith("?")) { isStopped = false; } else if (rxMessage.StartsWith("NO DATA")) { AddToSerialTrace("NO DATA"); Console.WriteLine("NO DATA"); } else if (rxMessage.Length == 19) // is it a valid line { try { rxMessage.Replace(" ", "");//remove all whitespaces uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16); if (acceptMessageId(id)) { canMessage.setID(id); canMessage.setLength(8); // TODO: alter to match data //canMessage.setData(0x0000000000000000); // reset message content canMessage.setData(ExtractDataFromString(rxMessage)); 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); } } } } catch (Exception) { //Console.WriteLine("MSG: " + rxMessage); } } //disable whitespace logging if (rxMessage.Length > 0) { AddToSerialTrace("SERRX: " + rxMessage); } } } if (rawString.Contains(">") && !isStopped) { AddToSerialTrace("SERIAL READY"); sendDataSempahore.WaitOne(0); sendDataSempahore.Release(); interfaceBusy = 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 CANMessage canMessage) { canMessage = new CANMessage(); Debug.Assert(canMessage != null); int wait_cnt = 0; uint id; byte length; ulong data; while (wait_cnt < timeout) { if (MctAdapter_ReceiveMessage(out id, out length, out data)) { // message received canMessage.setID(id); canMessage.setLength(length); canMessage.setData(data); return id; } // wait a bit Thread.Sleep(1); ++wait_cnt; } // nothing was received return 0; }
//------------------------------------------------------------------------- /** Handles incoming messages. */ private void read_messages() { uint id; byte length; ulong data; CANMessage msg = new CANMessage(); Debug.Assert(msg != null); // main loop while (true) { // check for thread termination request Debug.Assert(this.term_mutex != null); lock (this.term_mutex) { if (this.term_requested) { return; } } // receive messages while (MctAdapter_ReceiveMessage(out id, out length, out data)) { if (acceptMessageId(id)) { // convert message msg.setID(id); msg.setLength(length); msg.setData(data); // pass message to listeners lock (this.m_listeners) { AddToCanTrace("RX: " + id.ToString("X4") + " " + data.ToString("X16")); foreach (ICANListener listener in this.m_listeners) { listener.handleMessage(msg); } } } } // give up CPU for a moment Thread.Sleep(1); } }
public void readMessages() { CANMessage canMessage = new CANMessage(); Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } if (rawString != null) { logger.Trace(String.Format("Raw Data: {0}", rawString)); //AddToSerialTrace("RAW RX: " + rawString.Replace("\r",m_cr_sequence)); string rxString = rawString.Replace("\r", m_cr_sequence); //replace , because stringbuilder cannot handle \r bool isStopped = false; if (rxString.Length > 0) { //AddToSerialTrace("RECEIVE TEXT: " + rxString); //System.Diagnostics.Debug.WriteLine("SERMSG: " + rxString); var lines = ExtractLines(rxString); foreach (var rxMessage in lines) { if (rxMessage.StartsWith("STOPPED")) { isStopped = true; } else if (rxMessage.StartsWith("NO (MORE?) DATA")) { } //skip it else if (rxMessage.StartsWith("CAN ERROR")) { } //handle error? else if (rxMessage.StartsWith("ELM")) { isStopped = false; } //skip it, this is a trick to stop ELM from listening to more messages and send ready char else if (rxMessage.StartsWith("?")) { isStopped = false; } else if (rxMessage.Length == 19) // is it a valid line { try { rxMessage.Replace(" ", "");//remove all whitespaces uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16); if (acceptMessageId(id)) { canMessage.setID(id); canMessage.setLength(8); // TODO: alter to match data //canMessage.setData(0x0000000000000000); // reset message content canMessage.setData(ExtractDataFromString(rxMessage)); receivedMessage(canMessage); } } catch (Exception) { //Console.WriteLine("MSG: " + rxMessage); } } //disable whitespace logging if (rxMessage.Length > 0) { logger.Debug("TELNET: " + rxMessage); } } } if (!isStopped) { logger.Debug("TELNET READY"); interfaceBusy = false; } rawString = null; } } }
/// <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; EASYSYNC.CANMsg r_canMsg = new EASYSYNC.CANMsg(); CANMessage canMessage = new CANMessage(); while (true) { lock (m_synchObject) { if (m_endThread) return; } readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.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 == EASYSYNC.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); 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; }
// 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> /// 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 canMessage) { canMessage = new CANMessage(); Debug.Assert(canMessage != null); int wait_cnt = 0; uint id; byte length; ulong data; while (wait_cnt < timeout) { if (MctAdapter_ReceiveMessage(out id, out length, out data)) { // message received canMessage.setID(id); canMessage.setLength(length); canMessage.setData(data); if (canMessage.getID() != a_canID) continue; return (uint)canMessage.getID(); } // wait a bit Thread.Sleep(1); ++wait_cnt; } // nothing was received return 0; }
public void readMessages() { CANMessage canMessage = new CANMessage(); StringBuilder receiveText = new StringBuilder(); logger.Debug("readMessages started"); while (true) { receiveDataSemaphore.WaitOne(); lock (m_synchObject) { if (m_endThread) { logger.Debug("readMessages ended"); return; } } if (m_serialPort != null) { if (m_serialPort.IsOpen && m_serialPort.BytesToRead > 0) { string rawString = string.Empty; try { rawString = m_serialPort.ReadExisting(); } catch (Exception e) { logger.Debug("CANELM372Device ReadExisting()" + e.Message); } //AddToSerialTrace("RAW RX: " + rawString.Replace("\r",m_cr_sequence)); string rxString = rawString.Replace("\n", "").Replace(">", "");// remove prompt characters... we don't need that stuff bool isStopped = false; if (rxString.Length > 0) { rxString = rxString.Replace("\r", m_cr_sequence); //replace , because stringbuilder cannot handle \r receiveText.Append(rxString); //AddToSerialTrace("RECEIVE TEXT: " + receiveText.ToString()); //System.Diagnostics.Debug.WriteLine("SERMSG: " + receiveText); var lines = ExtractLines(ref receiveText); foreach (var rxMessage in lines) { if (rxMessage.StartsWith("STOPPED")) { isStopped = true; } else if (rxMessage.StartsWith("NO DATA")) { } //skip it else if (rxMessage.StartsWith("CAN ERROR")) { //handle error? } else if (rxMessage.StartsWith("ELM")) { isStopped = false; } //skip it, this is a trick to stop ELM from listening to more messages and send ready char else if (rxMessage.StartsWith("?")) { isStopped = false; } else if (rxMessage.StartsWith("NO DATA")) { logger.Debug("NO DATA"); } else if (rxMessage.Length == 19) // is it a valid line { try { rxMessage.Replace(" ", "");//remove all whitespaces uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16); if (acceptMessageId(id)) { canMessage.setID(id); canMessage.setLength(8); // TODO: alter to match data //canMessage.setData(0x0000000000000000); // reset message content canMessage.setData(ExtractDataFromString(rxMessage)); receivedMessage(canMessage); } } catch (Exception e) { logger.Debug(e, rxMessage); } } // Catch weird message-lengths from Z22SE else if (rxMessage.StartsWith("7E8") && rxMessage.Length > 6) { try { rxMessage.Replace(" ", "");//remove all whitespaces uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16); byte len = (byte)(rxMessage.Length - 3); len = (byte)(((len & 1) == 1 ? len + 1 : len) / 2); if (acceptMessageId(id) && len <= 8) { canMessage.setID(id); canMessage.setLength(len); canMessage.setData(ExtractDataFromString2(len, rxMessage)); receivedMessage(canMessage); } } catch (Exception e) { logger.Debug(e, rxMessage); } } //disable whitespace logging if (rxMessage.Length > 0) { logger.Debug("SERRX: " + rxMessage + " Len:" + rxMessage.Length); } } } if (rawString.Contains(">") && !isStopped) { logger.Debug("SERIAL READY"); sendDataSempahore.WaitOne(0); sendDataSempahore.Release(); interfaceBusy = false; } } } } }
private void SendMessage(uint id, ulong data) { CANMessage msg = new CANMessage(); msg.setID(id); msg.setLength(8); msg.setData(data); if (!canUsbDevice.sendMessage(msg)) { Console.WriteLine("Failed to send message"); } }