/// <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> private uint waitForMessage(uint a_canID, uint timeout, out EASYSYNC.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { 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; } return((uint)r_canMsg.id); } else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new EASYSYNC.CANMsg(); return(0); }
override public int GetNumberOfAdapters() { //char[] buff = new char[32]; StringBuilder adapter = new StringBuilder(10); return(EASYSYNC.canusb_getFirstAdapter(adapter, 32)); }
/// <summary> /// The close method closes the CANUSB device. /// </summary> /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns> override public CloseResult close() { Console.WriteLine("Close called in CANUSBDevice"); if (m_readThread != null) { if (m_readThread.ThreadState != ThreadState.Stopped && m_readThread.ThreadState != ThreadState.StopRequested) { lock (m_synchObject) { m_endThread = true; } // m_readThread.Abort(); } } int res = EASYSYNC.canusb_Close(m_deviceHandle); m_deviceHandle = 0; if (EASYSYNC.ERROR_CANUSB_OK == res) { return(CloseResult.OK); } else { return(CloseResult.CloseError); } }
/// <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() { EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg(); //Check if I bus is connected close(); /*m_deviceHandle = EASYSYNC.canusb_Open(IntPtr.Zero, * "0xcb:0x9a", // Slow * EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL, * EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL, * EASYSYNC.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 /* char[] adapter = new char[16]; * int result = EASYSYNC.canusb_getFirstAdapter(adapter, 16); * Console.WriteLine("Number of adapters found: " + result.ToString("X8"));*/ m_deviceHandle = EASYSYNC.canusb_Open(IntPtr.Zero, "500", IntPtr.Zero, IntPtr.Zero, 1); /* m_deviceHandle = EASYSYNC.canusb_Open( null, * EASYSYNC.CAN_BAUD_500K, //500Kb/s * null,//EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL, * null,//EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL, * EASYSYNC.CANUSB_FLAG_TIMESTAMP);*/ int errorval = (int)m_deviceHandle; Console.WriteLine(errorval.ToString()); if (m_deviceHandle < 0) { Console.WriteLine("Invalid handle received"); return(OpenResult.OpenError); } if (DisableCanConnectionCheck || 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) { EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg(); msg.id = a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); msg.data = a_message.getData(); //Console.WriteLine("Data to send: " + msg.data.ToString("X16")); //Console.WriteLine("Data (original) to send: " + a_message.getData().ToString("X16")); int writeResult; AddToCanTrace("Sending message"); writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg); if (writeResult == EASYSYNC.ERROR_CANUSB_OK) { AddToCanTrace("Message sent successfully"); return(true); } else { switch (writeResult) { case EASYSYNC.ERROR_CANUSB_COMMAND_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_COMMAND_SUBSYSTEM"); break; case EASYSYNC.ERROR_CANUSB_INVALID_PARAM: AddToCanTrace("Message failed to send: ERROR_CANUSB_INVALID_PARAM"); break; case EASYSYNC.ERROR_CANUSB_NO_MESSAGE: AddToCanTrace("Message failed to send: ERROR_CANUSB_NO_MESSAGE"); break; case EASYSYNC.ERROR_CANUSB_NOT_OPEN: AddToCanTrace("Message failed to send: ERROR_CANUSB_NOT_OPEN"); break; case EASYSYNC.ERROR_CANUSB_OPEN_SUBSYSTEM: AddToCanTrace("Message failed to send: ERROR_CANUSB_OPEN_SUBSYSTEM"); break; case EASYSYNC.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> /// 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 EASYSYNC.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.ERROR_CANUSB_OK) { return((uint)r_canMsg.id); } else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact nrOfWait++; } } r_canMsg = new EASYSYNC.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; 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> /// The close method closes the CANUSB device. /// </summary> /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns> override public CloseResult close() { int res = 0; try { res = EASYSYNC.canusb_Close(m_deviceHandle); } catch (DllNotFoundException e) { return(CloseResult.CloseError); } m_deviceHandle = 0; if (EASYSYNC.ERROR_CANUSB_OK == res) { return(CloseResult.OK); } else { return(CloseResult.CloseError); } }
public override void Flush() { EASYSYNC.canusb_Flush(m_deviceHandle); }
override public void clearTransmitBuffer() { EASYSYNC.canusb_Flush(m_deviceHandle); }
private void DumpCanMsg(EASYSYNC.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> /// 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) { EASYSYNC.CANMsg msg = new EASYSYNC.CANMsg(); msg.id = (ushort)a_message.getID(); msg.len = a_message.getLength(); msg.flags = a_message.getFlags(); ulong msgdata = a_message.getData(); // store in data (ulong) /*byte databyte = a_message.getCanData(7); * msg.data_1 = GetDataMSBADCII(databyte); * msg.data_2 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(6); * msg.data_3 = GetDataMSBADCII(databyte); * msg.data_4 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(5); * msg.data_5 = GetDataMSBADCII(databyte); * msg.data_6 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(4); * msg.data_7 = GetDataMSBADCII(databyte); * msg.data_8 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(3); * msg.data_9 = GetDataMSBADCII(databyte); * msg.data_10 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(2); * msg.data_11 = GetDataMSBADCII(databyte); * msg.data_12 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(1); * msg.data_13 = GetDataMSBADCII(databyte); * msg.data_14 = GetDataLSBADCII(databyte); * databyte = a_message.getCanData(0); * msg.data_15 = GetDataMSBADCII(databyte); * msg.data_16 = GetDataLSBADCII(databyte); */ msg.data = a_message.getData(); // this data should be in ascii: unsigned char data[16]; // Databytes 0...7 // example: /* * msg.data[0]='A'; * msg.data[1]='1'; * * msg.data[2]='B'; * msg.data[3]='2'; * * msg.data[4]='C'; * msg.data[5]='3'; * * msg.data[6]='D'; * msg.data[7]='4'; * * msg.data[8]='E'; * msg.data[9]='5'; * * msg.data[10]='F'; * msg.data[11]='6'; * * msg.data[12]='1'; * msg.data[13]='2'; * * msg.data[14]='3'; * msg.data[15]='4'; * */ if (m_DoLogging) { DumpCanMsg(msg, true); } int writeResult; Console.WriteLine("Writing to handle: " + m_deviceHandle.ToString("X8")); writeResult = EASYSYNC.canusb_Write(m_deviceHandle, ref msg); if (writeResult == EASYSYNC.ERROR_CANUSB_OK) { return(true); } else { //EASYSYNC.canusb_Flush(m_deviceHandle); Console.WriteLine("Failed to send message: " + writeResult.ToString("X8")); return(false); } }
/// <summary> /// The open method tries to connect to both busses to see if one of them is connected and /// active. The first strategy is to listen for any CAN message. If this fails there is a /// check to see if the application is started after an interrupted flash session. This is /// done by sending a message to set address and length (only for P-bus). /// </summary> /// <returns>OpenResult.OK is returned on success. Otherwise OpenResult.OpenError is /// returned.</returns> override public OpenResult open() { if (m_deviceHandle != 0) { close(); } // get first adapter //char[] adapter = new char[10]; StringBuilder adapter = new StringBuilder(10); int result = EASYSYNC.canusb_getFirstAdapter(adapter, 10); if (result > 0) { Console.WriteLine("Number of adapters found: " + result.ToString("X8")); Console.WriteLine("Adapter serialnumber: " + adapter); m_deviceHandle = EASYSYNC.canusb_Open(null, //"0x00:0xB9:0x07", //"00:B9:07", "00B907", //"600", //null, null, //EASYSYNC.CANUSB_ACCEPTANCE_CODE_ALL, null, //EASYSYNC.CANUSB_ACCEPTANCE_MASK_ALL, EASYSYNC.CANUSB_FLAG_TIMESTAMP); Console.WriteLine("Handle: " + m_deviceHandle.ToString("X8")); if (m_deviceHandle > 0 && m_deviceHandle != 0xFFFFFFFD) { EASYSYNC.canusb_Flush(m_deviceHandle); Console.WriteLine("Creating new reader thread"); StringBuilder sb = new StringBuilder(100); EASYSYNC.canusb_VersionInfo(m_deviceHandle, sb); Console.WriteLine("Versionifo: " + sb.ToString()); Console.WriteLine("Status: " + EASYSYNC.canusb_Status(m_deviceHandle).ToString("X8")); m_readThread = new Thread(readMessages); try { m_readThread.Priority = ThreadPriority.Normal; // realtime enough } catch (Exception E) { Console.WriteLine(E.Message); } if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } else { // second try after unload of dlls? //close(); return(OpenResult.OpenError); } } return(OpenResult.OpenError); }
/// <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(); /* Stopwatch * while (true) * { * lock (m_synchObject) * { * if (m_endThread) * return; * } * readResult = EASYSYNC.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 == EASYSYNC.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 = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult > 0) { canMessage.setID((uint)r_canMsg.id); canMessage.setLength(r_canMsg.len); canMessage.setTimeStamp((uint)r_canMsg.timestamp); canMessage.setFlags(r_canMsg.flags); //TODO: data hier nog vullen 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 == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact } } }
/// <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 EASYSYNC.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { readResult = EASYSYNC.canusb_Read(m_deviceHandle, out r_canMsg); if (readResult == EASYSYNC.ERROR_CANUSB_OK) { return (uint)r_canMsg.id; } else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); // changed to 0 to see performance impact 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> private uint waitForMessage(uint a_canID, uint timeout, out EASYSYNC.CANMsg r_canMsg) { int readResult = 0; int nrOfWait = 0; while (nrOfWait < timeout) { 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; return (uint)r_canMsg.id; } else if (readResult == EASYSYNC.ERROR_CANUSB_NO_MESSAGE) { Thread.Sleep(1); nrOfWait++; } } r_canMsg = new EASYSYNC.CANMsg(); return 0; }
override public void clearReceiveBuffer() { EASYSYNC.canusb_Flush(m_deviceHandle); }