} // End KvaserCanRateReturn // *************************************** // DetectCanInterfaces // Pulls all of the available adapters for the CAN bus // *************************************** public static void DetectCanInterfaces() { // Detect Process for Kvaser CAN devices try { int nrOfChannels; object o = new object(); Canlib.canInitializeLibrary(); //List available channels Canlib.canGetNumberOfChannels(out nrOfChannels); for (int i = 0; i < nrOfChannels; i++) { Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CHANNEL_NAME, out o); // Display of data in ListView as follows // Network; Type; Mfg; Channel; Status; canHandle BusInterface.AddInterface("CAN;" + o.ToString() + ";" + "Kvaser" + ";" + i, -1); ErrorLog.NewLogEntry("CAN", "Detect CAN: " + o.ToString()); } } catch (Exception) { ErrorLog.NewLogEntry("Adapter", "Kvaser library not found"); } } // End DetectCanInterfaces
} // End GenericCanInitilize // *************************************** // DetectCanInterfaces // Pulls all of the available adapters for the CAN bus // *************************************** public static void DetectCanInterfaces() { // Detect Process for Intrepid devices try { int iResult; neoCSNet2003.NeoDevice ndNeoToOpen = new neoCSNet2003.NeoDevice(); //Struct holding detected hardware information int iNumberOfDevices; //Number of hardware devices to look for string neoDevice; //Set the number of devices to find iNumberOfDevices = 1; // Revision needed to search for multiple devices //Search for connected hardware iResult = neoCSNet2003.icsNeoDll.icsneoFindNeoDevices(65535, ref ndNeoToOpen, ref iNumberOfDevices); // Obtain the name of the Interpid device -- Device names from Intrepid Sample code switch (ndNeoToOpen.DeviceType) { case 1: neoDevice = "neoVI Blue SN " + Convert.ToString(ndNeoToOpen.SerialNumber); break; case 4: neoDevice = "Value CAN 2 SN " + Convert.ToString(ndNeoToOpen.SerialNumber); break; case 8: neoDevice = "neoVI FIRE SN " + Convert.ToString(ndNeoToOpen.SerialNumber); break; case 16: neoDevice = "ValueCAN 3 SN " + Convert.ToString(ndNeoToOpen.SerialNumber); break; default: neoDevice = "Unknown neoVI SN " + Convert.ToString(ndNeoToOpen.SerialNumber); break; } if (iNumberOfDevices < 1 || iResult == 0) { ErrorLog.NewLogEntry("CAN", "Detect CAN: No Intrepid devices detected "); } else { BusInterface.AddInterface("CAN;" + neoDevice + ";" + "Intrepid" + ";" + 0, -1); ErrorLog.NewLogEntry("CAN", "Detect CAN: " + neoDevice); } } catch (Exception) { ErrorLog.NewLogEntry("Adapter", "Intrepid library not found"); } } // End DetectCanInterfaces
// ********************** // Turns canInterface off // ********************** public static void CanBusOff(string canInterface) { string hardwareString0 = canInterface.Replace(" ", ""); string[] msgOutput = hardwareString0.Split(';'); // Need Error Checking to ensure that device closes ECOMLibrary.CloseDevice((UInt32)BusInterface.ReturnHandle(canInterface)); ErrorLog.NewLogEntry("CAN", "Bus Off Success/Failed: " + msgOutput[1]); }
} // End GenericCanTransmitSingle //************************************ // CAN Transmit for multiple transmissions //************************************ public static void CanTransmitMultiple(CanData can) { can.handle = BusInterface.ReturnHandle(can.hardware); KvaserWriteCan writeCan = new KvaserWriteCan(); Thread t2 = new Thread(delegate() { writeCan.KvaserWriteMultiple(can); }); t2.IsBackground = true; t2.Start(); } // End GenericCanTransmitMultiple
} // End DetectCanInterfaces // *************************************** // GenericCanBusOn // Turns canInterface on based on User Selection // *************************************** public static int CanBusOn(string canInterface, string bitRateSetting) { string hardwareString0 = canInterface.Replace(" ", ""); string[] msgOutput = hardwareString0.Split(';'); Byte ReturnError = 0; //This will be used for all error return status codes UInt32 EcomHandle; //This will be the HANDLE to our ECOM device used for all function calls byte EComCANBitrate = ECOMLibrary.CAN_BAUD_500K; ECOMLibrary.DeviceInfo di = new ECOMLibrary.DeviceInfo(); // Assigns settings for the adapter if (bitRateSetting == "1M") { EComCANBitrate = ECOMLibrary.CAN_BAUD_1MB; } else if (bitRateSetting == "500K") { EComCANBitrate = ECOMLibrary.CAN_BAUD_500K; } else if (bitRateSetting == "250K") { EComCANBitrate = ECOMLibrary.CAN_BAUD_250K; } else if (bitRateSetting == "125K") { EComCANBitrate = ECOMLibrary.CAN_BAUD_125K; } else if (bitRateSetting == "100K" || msgOutput[4] == "62K" || msgOutput[4] == "50K" || msgOutput[4] == "33K") { MainWindow.ErrorDisplayString("ECom Adapter does not support low speed CAN (100K, 62K, 50K, 33K)"); return(-1); } EcomHandle = ECOMLibrary.CANOpen(Convert.ToUInt32(msgOutput[3]), EComCANBitrate, ref ReturnError); //MainWindow.ErrorDisplayString("ECom Handle: " + Convert.ToString(EcomHandle)); BusInterface.AddHandle(canInterface, (int)EcomHandle); if (EcomHandle == 0 || ReturnError != 0) { StringBuilder ErrMsg = new StringBuilder(400); ECOMLibrary.GetFriendlyErrorMessage(ReturnError, ErrMsg, 400); MainWindow.ErrorDisplayString("CANOpen failed with error message: " + ErrMsg); return(-1); } ECOMLibrary.GetDeviceInfo(EcomHandle, ref di); ErrorLog.NewLogEntry("CAN", "Bus On: " + di.SerialNumber); return(1); }
//****************** // Load Interfaces -- This loads the selected interfaces on the machine into the interfacebox //****************** private void LoadInterfaces() { string bitRate = null; string displayRow; // Sets up the multi-column box InterfaceBox.Items.Clear(); InterfaceBox.Columns.Clear(); InterfaceBox.Columns.Add("Network", 55, HorizontalAlignment.Left); InterfaceBox.Columns.Add("Type", 155, HorizontalAlignment.Left); InterfaceBox.Columns.Add("Mfg", 85, HorizontalAlignment.Left); InterfaceBox.Columns.Add("Channel", 35, HorizontalAlignment.Left); InterfaceBox.Columns.Add("BitRate", 55, HorizontalAlignment.Left); InterfaceBox.Columns.Add("Status", 75, HorizontalAlignment.Right); // Reset Interfaces in Bus Intefaces // BusInterface.ResetInterfaces(); GenericCanBus.DetectCanInterfaces(); // Find all of the LIN bus interfaces and adds to the list in busInterface // GenericLinBus.DetectLinInterfaces(); // Find all of the FlexRay bus interfaces and adds to the list in busInterface // GenericFlexRayBus.DetectFlexRayInterfaces(); // Returns the interfaces string[] interfaces = BusInterface.ReturnInterfaces(); for (int i = 0; i < interfaces.Length; i++) { bitRate = GenericCanBus.GenericCanRateReturn(interfaces[i]); // Creates the row with the message data and inserts the row in the ListView if (bitRate == null || bitRate == "" || bitRate == "0") { displayRow = ";NA;Bus Off"; } else { displayRow = ";" + bitRate + ";Bus On"; } ListViewItem item = new ListViewItem(CommonUtils.ConvertStringtoStringArray(interfaces[i] + displayRow)); InterfaceBox.Items.AddRange(new ListViewItem[] { item }); } }
// Pulls all of the available adapters for the CAN bus public static void DetectLinInterfaces(BusInterface busInterface) { int nrOfChannels; Canlib.canInitializeLibrary(); //List available channels Canlib.canGetNumberOfChannels(out nrOfChannels); object o = new object(); for (int i = 0; i < nrOfChannels; i++) { Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CHANNEL_NAME, out o); // Display of data in ListView as follows // Network; Type; Mfg; Channel; Status BusInterface.AddInterface("CAN" + ";" + o.ToString() + ";" + "Kvasier" + ";" + i + ";" + "Pending", -1); } }
// ********************** // Turns canInterface off // ********************** public static void CanBusOff(string canInterface) { string hardwareString0 = canInterface.Replace(" ", ""); string[] msgOutput = hardwareString0.Split(';'); int iResult; int iNumberOfErrors = 0; iResult = neoCSNet2003.icsNeoDll.icsneoClosePort(BusInterface.ReturnHandle(canInterface), ref iNumberOfErrors); if (iResult == 1) { ErrorLog.NewLogEntry("CAN", "Bus Off Success: " + msgOutput[1]); } else { ErrorLog.NewLogEntry("CAN", "Bus Off Failed: " + msgOutput[1]); } }
// Pulls all of the available adapters for the CAN bus public static void DetectLinInterfaces(BusInterface busInterface) { int nrOfChannels; Canlib.canInitializeLibrary(); //List available channels Canlib.canGetNumberOfChannels(out nrOfChannels); object o = new object(); for (int i = 0; i < nrOfChannels; i++) { Canlib.canGetChannelData(i, Canlib.canCHANNELDATA_CHANNEL_NAME, out o); // Display of data in ListView as follows // Network; Type; Mfg; Channel; Status BusInterface.AddInterface("CAN" + ";" + o.ToString() + ";" + "Kvasier" + ";" + i + ";" + "Pending", -1); } }
} // End GenericCanRateReturn // *************************************** // DetectCanInterfaces // Pulls all of the available adapters for the CAN bus // *************************************** public static void DetectCanInterfaces() { // Detect Process for ECom devices try { ECOMLibrary.DeviceInfo di = new ECOMLibrary.DeviceInfo(); UInt32 deviceSearch = ECOMLibrary.StartDeviceSearch(ECOMLibrary.FIND_ALL); while (deviceSearch != 0 && ECOMLibrary.FindNextDevice(deviceSearch, ref di) == ECOMLibrary.ECI_NO_ERROR) { BusInterface.AddInterface("CAN;" + "CANCapture" + ";" + "ECom" + ";" + di.SerialNumber, -1); ErrorLog.NewLogEntry("CAN", "Detect CAN: " + di.SerialNumber); } ECOMLibrary.CloseDeviceSearch(deviceSearch); } catch (Exception) { ErrorLog.NewLogEntry("Adapter", "ECom library not found"); } } // End DetectCanInterfaces
//**************************** // Update Interface Boxes //**************************** private void UpdateInterfaceBox() { string bitRate = "-1"; // Populate the availabe interfaces for transmission/receiving string[] interfaces = BusInterface.ReturnInterfaces(); for (int i = 0; i < interfaces.Length; i++) { bitRate = GenericCanBus.GenericCanRateReturn(interfaces[i]); // Creates the row with the message data and inserts the row in the ListView if (bitRate != null && bitRate != "" && bitRate != "0" && bitRate != "-1") { TransmitInterfaceBox.Items.Add(interfaces[i]); } } if (TransmitInterfaceBox.Items.Count > 0) { TransmitInterfaceBox.SelectedIndex = 0; } }
} // End GenericCanTransmitMultiple // CAN Receive public static bool CanReceive(CanData can) { // string hardwareString0 = can.hardware.Replace(" ", ""); // string[] msgOutput = hardwareString0.Split(';'); // to pull the can.handle from the hardware selection can.handle = BusInterface.ReturnHandle(can.hardware); //MainWindow.ErrorDisplayString("can.handle: " + can.handle + " ; msg: " + msgOutput[3]); // Kvaser CAN Read Array.Clear(can.msg, 0, 8); can.status = Canlib.canRead(can.handle, out can.id, can.msg, out can.dlc, out can.flags, out can.time); // MainWindow.ErrorDisplayString("can.handle in receive: " + can.handle); CheckFlags(can); if (can.status != Canlib.canStatus.canOK) { return(false); } else { return(true); } } // End Read
//************************************ // CAN Transmit for a single transmission //************************************ public static void CanTransmitSingle(CanData can) { string hardwareString0 = can.hardware.Replace(" ", ""); string[] msgOutput = hardwareString0.Split(';'); can.handle = BusInterface.ReturnHandle(can.hardware); // Threading Part /*CanWrite writeCan = new CanWrite(); * * // Starts the Kvaser threading * Thread t2 = new Thread(delegate() * { * writeCan.KvaserWrite(can); * }); * t2.Start(); */ // Non-threading write option Canlib.canWriteWait(can.handle, can.id, can.msg, can.dlc, can.flags, 1000); CheckFlags(can); } // End GenericCanTransmitSingle
} // End DetectCanInterfaces // *************************************** // GenericCanBusOn // Turns canInterface on based on User Selection // *************************************** public static int CanBusOn(string canInterface, string bitRateSetting) { string hardwareString0 = canInterface.Replace(" ", ""); string[] msgOutput = hardwareString0.Split(';'); int canHandle; int channelFlags; int KvaserCANBitrate = Canlib.canBITRATE_500K; Canlib.canStatus status; // Assigns settings for the adapter if (bitRateSetting == "1M") { KvaserCANBitrate = Canlib.canBITRATE_1M; } else if (bitRateSetting == "500K") { KvaserCANBitrate = Canlib.canBITRATE_500K; } else if (bitRateSetting == "250K") { KvaserCANBitrate = Canlib.canBITRATE_250K; } else if (bitRateSetting == "125K") { KvaserCANBitrate = Canlib.canBITRATE_125K; } else if (bitRateSetting == "100K") { KvaserCANBitrate = Canlib.canBITRATE_100K; } else if (bitRateSetting == "62K") { KvaserCANBitrate = Canlib.canBITRATE_62K; } else if (bitRateSetting == "50K") { KvaserCANBitrate = Canlib.canBITRATE_50K; } else if (bitRateSetting == "33K") { KvaserCANBitrate = 33000; } // Checks for Virtual Flag if (msgOutput[1].IndexOf("Virtual") != -1) { channelFlags = Canlib.canOPEN_ACCEPT_VIRTUAL; } else { channelFlags = 0; } // Opens CAN channel canHandle = Canlib.canOpenChannel(Convert.ToInt32(msgOutput[3]), channelFlags); // Sets parameters for the CAN channel; special parameters for 33K, single-wire operation if (bitRateSetting == "33K") { status = Canlib.canSetBusParams(canHandle, 33000, 5, 2, 2, 1, 0); } else { // Standard settings for other operations status = Canlib.canSetBusParams(canHandle, KvaserCANBitrate, 0, 0, 0, 0, 0); } if (status < 0) { ErrorLog.NewLogEntry("CAN", "Kvaser bus setting parameters failed: " + KvaserCANBitrate); return(-1); } else { ErrorLog.NewLogEntry("CAN", "Kvaser bus setting parameters success: " + KvaserCANBitrate); } // possible configuration for 33K single-wire operation; above settings work //Canlib.canSetBusParamsC200(canHandle, 0x5D, 0x05); status = Canlib.canBusOn(canHandle); if (status < 0) { ErrorLog.NewLogEntry("CAN", "Bus On Failed: " + msgOutput[1]); return(-1); } else { ErrorLog.NewLogEntry("CAN", "Bus On Success: " + msgOutput[1]); } // Associates the int holder for the Kvaser interface back with the interface dictionary structure BusInterface.AddHandle(canInterface, canHandle); //MainWindow.ErrorDisplayString("Bus On - BusInterface: " + canInterface + " ; Handle: " + canHandle); return(1); }
public void ECOMWriteMultiple(CanData can) { for (int x = 0; x < can.number; x++) { can.handle = BusInterface.ReturnHandle(can.hardware); //MainWindow.ErrorDisplayString("ECom Handle: " + Convert.ToString(can.handle)); ECOMLibrary.SFFMessage txMessage = new ECOMLibrary.SFFMessage(); ECOMLibrary.EFFMessage etxMessage = new ECOMLibrary.EFFMessage(); // Pulls the top 3 bits for IDH and the bottom 8 bits for IDL txMessage.IDH = (byte)(can.id & 0x700); txMessage.IDL = (byte)(can.id & 0x0FF); // Converts the entire id into an unit for extended sending etxMessage.ID = (UInt32)can.id; txMessage.DataLength = (byte)can.dlc; etxMessage.DataLength = (byte)can.dlc; txMessage.options = (byte)can.flags; etxMessage.options = (byte)can.flags; // Puts the individual bytes into the can.msg byte array if (can.dlc > 0) { txMessage.data1 = can.msg[0]; etxMessage.data1 = can.msg[0]; } if (can.dlc > 1) { txMessage.data2 = can.msg[1]; etxMessage.data2 = can.msg[1]; } if (can.dlc > 2) { txMessage.data3 = can.msg[2]; etxMessage.data3 = can.msg[2]; } if (can.dlc > 3) { txMessage.data4 = can.msg[3]; etxMessage.data4 = can.msg[3]; } if (can.dlc > 4) { txMessage.data5 = can.msg[4]; etxMessage.data5 = can.msg[4]; } if (can.dlc > 5) { txMessage.data6 = can.msg[5]; etxMessage.data6 = can.msg[5]; } if (can.dlc > 6) { txMessage.data7 = can.msg[6]; etxMessage.data7 = can.msg[6]; } if (can.dlc > 7) { txMessage.data8 = can.msg[7]; etxMessage.data8 = can.msg[7]; } if (can.flags == 4) { ECOMLibrary.CANTransmitMessageEx((UInt32)can.handle, ref etxMessage); } else { ECOMLibrary.CANTransmitMessage((UInt32)can.handle, ref txMessage); } System.Threading.Thread.Sleep(can.timeBtw); if (can.increment == true) { can.id++; } } }
} // End GenericCanTransmitMultiple // CAN Receive public static bool CanReceive(CanData can) { Byte ReturnError = 0; ECOMLibrary.SFFMessage RxMessage = new ECOMLibrary.SFFMessage(); ECOMLibrary.EFFMessage ExMessage = new ECOMLibrary.EFFMessage(); try { //ECOM library call to get a standard message ReturnError = ECOMLibrary.CANReceiveMessage((UInt32)BusInterface.ReturnHandle(can.hardware), ref RxMessage); } catch { ErrorLog.NewLogEntry("ECOM", Convert.ToString(ReturnError)); } if (ReturnError == 0) { // Converts the ID low and ID high can.id = Convert.ToInt32(RxMessage.IDL + RxMessage.IDH); can.dlc = Convert.ToInt32(RxMessage.DataLength); can.time = Convert.ToInt64(RxMessage.TimeStamp); // Puts the individual bytes into the can.msg byte array if (can.dlc > 0) { can.msg[0] = RxMessage.data1; } if (can.dlc > 1) { can.msg[1] = RxMessage.data2; } if (can.dlc > 2) { can.msg[2] = RxMessage.data3; } if (can.dlc > 3) { can.msg[3] = RxMessage.data4; } if (can.dlc > 4) { can.msg[4] = RxMessage.data5; } if (can.dlc > 5) { can.msg[5] = RxMessage.data6; } if (can.dlc > 6) { can.msg[6] = RxMessage.data7; } if (can.dlc > 7) { can.msg[7] = RxMessage.data8; } // Revisions Needed -- Need to pull flag information can.flags = Convert.ToInt32(RxMessage.options); //MainWindow.ErrorDisplayString(Convert.ToString(can.flags)); // For error testing // MainWindow.ErrorDisplayByteArray(can.msg); //MainWindow.ErrorDisplayString(Convert.ToString(can.dlc)); } else { ReturnError = ECOMLibrary.CANReceiveMessageEx((UInt32)BusInterface.ReturnHandle(can.hardware), ref ExMessage); can.id = Convert.ToInt32(ExMessage.ID); can.dlc = Convert.ToInt32(ExMessage.DataLength); can.time = Convert.ToInt64(ExMessage.TimeStamp); // Puts the individual bytes into the can.msg byte array if (can.dlc > 0) { can.msg[0] = ExMessage.data1; } if (can.dlc > 1) { can.msg[1] = ExMessage.data2; } if (can.dlc > 2) { can.msg[2] = ExMessage.data3; } if (can.dlc > 3) { can.msg[3] = ExMessage.data4; } if (can.dlc > 4) { can.msg[4] = ExMessage.data5; } if (can.dlc > 5) { can.msg[5] = ExMessage.data6; } if (can.dlc > 6) { can.msg[6] = ExMessage.data7; } if (can.dlc > 7) { can.msg[7] = ExMessage.data8; } // Revisions Needed -- Need to pull flag information can.flags = Convert.ToInt32(ExMessage.options); // For error testing // MainWindow.ErrorDisplayByteArray(can.msg); } if (ReturnError != 0) { return(false); } else { return(true); } } // End Read
//************************************ // CAN Transmit for a single transmission //************************************ public static void CanTransmitSingle(CanData can) { // Implementation for Standard Message & Extended Message can.handle = BusInterface.ReturnHandle(can.hardware); //MainWindow.ErrorDisplayString("ECom Handle: " + Convert.ToString(can.handle)); ECOMLibrary.SFFMessage txMessage = new ECOMLibrary.SFFMessage(); ECOMLibrary.EFFMessage etxMessage = new ECOMLibrary.EFFMessage(); // Pulls the top 3 bits for IDH and the bottom 8 bits for IDL txMessage.IDH = (byte)(can.id & 0x700); txMessage.IDL = (byte)(can.id & 0x0FF); // Converts the entire id into an unit for extended sending etxMessage.ID = (UInt32)can.id; txMessage.DataLength = (byte)can.dlc; etxMessage.DataLength = (byte)can.dlc; txMessage.options = (byte)can.flags; etxMessage.options = (byte)can.flags; // Puts the individual bytes into the can.msg byte array if (can.dlc > 0) { txMessage.data1 = can.msg[0]; etxMessage.data1 = can.msg[0]; } if (can.dlc > 1) { txMessage.data2 = can.msg[1]; etxMessage.data2 = can.msg[1]; } if (can.dlc > 2) { txMessage.data3 = can.msg[2]; etxMessage.data3 = can.msg[2]; } if (can.dlc > 3) { txMessage.data4 = can.msg[3]; etxMessage.data4 = can.msg[3]; } if (can.dlc > 4) { txMessage.data5 = can.msg[4]; etxMessage.data5 = can.msg[4]; } if (can.dlc > 5) { txMessage.data6 = can.msg[5]; etxMessage.data6 = can.msg[5]; } if (can.dlc > 6) { txMessage.data7 = can.msg[6]; etxMessage.data7 = can.msg[6]; } if (can.dlc > 7) { txMessage.data8 = can.msg[7]; etxMessage.data8 = can.msg[7]; } // Non-threading write option if (can.flags == 4) { ECOMLibrary.CANTransmitMessageEx((UInt32)can.handle, ref etxMessage); } else { ECOMLibrary.CANTransmitMessage((UInt32)can.handle, ref txMessage); } CheckFlags(can); } // End GenericCanTransmitSingle