public void readMessages() { CANMessage canMessage = new CANMessage(); byte[] receiveBuffer = new byte[1024]; // circular buffer for reception of data string receiveString = string.Empty; Console.WriteLine("readMessages started"); while (true) { lock (m_synchObject) { if (m_endThread) { Console.WriteLine("readMessages ended"); return; } } if (m_serialPort != null) { if (m_serialPort.IsOpen) { if (m_serialPort.BytesToRead > 0) { string rxString = m_serialPort.ReadExisting(); if (rxString.Length > 0) AddToSerialTrace("SERRX: " + rxString); receiveString += rxString; //Console.WriteLine("BUF1: " + receiveString); receiveString = receiveString.Replace(">", ""); // remove prompt characters... we don't need that stuff receiveString = receiveString.Replace("NO DATA", ""); // remove prompt characters... we don't need that stuff while (receiveString.StartsWith("\n") || receiveString.StartsWith("\r")) { receiveString = receiveString.Substring(1, receiveString.Length - 1); } while (receiveString.Contains('\r')) { // process the line int idx = receiveString.IndexOf('\r'); string rxMessage = receiveString.Substring(0, idx); receiveString = receiveString.Substring(idx + 1, receiveString.Length - idx - 1); while (receiveString.StartsWith("\n") || receiveString.StartsWith("\r")) { receiveString = receiveString.Substring(1, receiveString.Length - 1); } //Console.WriteLine("BUF2: " + receiveString); // is it a valid line if (rxMessage.Length >= 6) { try { uint id = Convert.ToUInt32(rxMessage.Substring(0, 3), 16); if (MessageContainsInformationForRealtime(id)) { canMessage.setID(id); canMessage.setLength(8); // TODO: alter to match data canMessage.setData(0x0000000000000000); // reset message content byte b1 = Convert.ToByte(rxMessage.Substring(4, 2), 16); if (b1 < 7) { canMessage.setCanData(b1, 0); //Console.WriteLine("Byte 1: " + Convert.ToByte(rxMessage.Substring(4, 2), 16).ToString("X2")); if (b1 >= 1) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(7, 2), 16), 1); if (b1 >= 2) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(10, 2), 16), 2); if (b1 >= 3) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(13, 2), 16), 3); if (b1 >= 4) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(16, 2), 16), 4); if (b1 >= 5) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(19, 2), 16), 5); if (b1 >= 6) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(22, 2), 16), 6); if (b1 >= 7) canMessage.setCanData(Convert.ToByte(rxMessage.Substring(25, 2), 16), 7); } else { canMessage.setCanData(b1, 0); //Console.WriteLine("Byte 1: " + Convert.ToByte(rxMessage.Substring(4, 2), 16).ToString("X2")); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(7, 2), 16), 1); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(10, 2), 16), 2); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(13, 2), 16), 3); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(16, 2), 16), 4); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(19, 2), 16), 5); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(22, 2), 16), 6); canMessage.setCanData(Convert.ToByte(rxMessage.Substring(25, 2), 16), 7); } lock (m_listeners) { AddToCanTrace("RX: " + canMessage.getData().ToString("X16")); //Console.WriteLine("MSG: " + rxMessage); foreach (ICANListener listener in m_listeners) { listener.handleMessage(canMessage); } } } } catch (Exception) { Console.WriteLine("MSG: " + rxMessage); } } } } else { Thread.Sleep(1); // give others some air } } } } // parse the receive string /*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.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> 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> /// 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); } } }
//--------------------------------------------------------------------------------------------- /** 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; }
private void SendMessage(uint id, ulong data) { if(canUsbDevice is CANELM327Device) return; CANMessage msg = new CANMessage(); msg.setID(id); msg.setLength(8); msg.setData(data); if (!canUsbDevice.sendMessage(msg)) { Console.WriteLine("Failed to send message"); } }
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 (MessageContainsInformationForRealtime(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); } //CastInformationEvent(canMessage); // <GS-05042011> re-activated this function } //Thread.Sleep(1); } //else { // Thread.Sleep(1); // give others some air } } } catch (Exception) { Console.WriteLine("MSG: " + rxMessage); } } }