/* * Initiates the channel */ public void initChannel(int channel, string bitrate) { Canlib.canStatus status; Canlib.canInitializeLibrary(); int hnd = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL); if (hnd >= 0) { chanHandle = hnd; Dictionary <string, int> dicBitRates = new Dictionary <string, int>() { { "125 kb/s", Canlib.canBITRATE_125K }, { "250 kb/s", Canlib.canBITRATE_250K }, { "500 kb/s", Canlib.canBITRATE_500K }, { "1 Mb/s", Canlib.BAUD_1M } }; status = Canlib.canSetBusParams(chanHandle, dicBitRates[bitrate], 0, 0, 0, 0, 0); status = Canlib.canBusOn(chanHandle); if (status == Canlib.canStatus.canOK) { channelOn = true; } } }
private void OpenChannel(out int hnd, int bitrate) { logger.Debug("hnd = canlibCLSNET.Canlib.canOpenChannel()"); hnd = Canlib.canOpenChannel(ChannelNumber, 0); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(hnd)"); Canlib.canStatus statusSetParam = Canlib.canSetBusParams(hnd, bitrate, 0, 0, 0, 0, 0); logger.Debug("canlibCLSNET.Canlib.canBusOn(hnd)"); Canlib.canStatus statusOn = Canlib.canBusOn(hnd); Canlib.canIoCtl(hnd, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); }
//Sets the bitrate private void setBitrateButton_Click(object sender, RoutedEventArgs e) { int[] bitrates = new int[4] { Canlib.canBITRATE_125K, Canlib.canBITRATE_250K, Canlib.canBITRATE_500K, Canlib.BAUD_1M }; int bitrate = bitrates[bitrateBox.SelectedIndex]; Canlib.canStatus status = Canlib.canSetBusParams(handle, bitrate, 0, 0, 0, 0, 0); CheckStatus("Setting bitrate to " + ((ComboBoxItem)bitrateBox.SelectedValue).Content, status); }
//Sets the bitrate private void setBitrate_Button_Click(object sender, EventArgs e) { int[] bitrates = new int[4] { Canlib.canBITRATE_125K, Canlib.canBITRATE_250K, Canlib.canBITRATE_500K, Canlib.BAUD_1M }; int bitrate = bitrates[bitrateText.SelectedIndex]; Canlib.canStatus status = Canlib.canSetBusParams(handle, bitrate, 0, 0, 0, 0, 0); CheckStatus("Setting bitrate to " + bitrateText.SelectedValue, status); }
public bool CAN_SetBusParameter(int seg1, int seg2) { int sjw = 0; int nosampl = 0; int syncmode = 0; // 속도설정은 고정으로... Canlib.canStatus cs = Canlib.canSetBusParams(handle, Canlib.canBITRATE_500K, seg1, seg2, sjw, nosampl, syncmode); CheckStatus("CAN SetBusParameter", cs); if (cs == Canlib.canStatus.canOK) { return(true); } else { return(false); } }
//Goes on bus void busOnButton_Click(object sender, RoutedEventArgs e) { //Set Bitrate int[] bitrates = new int[] { Canlib.canBITRATE_125K, Canlib.canBITRATE_250K, Canlib.canBITRATE_500K, Canlib.BAUD_1M }; Canlib.canStatus senderStatus = Canlib.canSetBusParams(sendHandle, bitrates[bitrateComboBox.SelectedIndex], 0, 0, 0, 0, 0); Canlib.canStatus readerStatus = Canlib.canSetBusParams(readHandle, bitrates[bitrateComboBox.SelectedIndex], 0, 0, 0, 0, 0); CheckStatus("Setting bitrate to " + ((ComboBoxItem)bitrateComboBox.SelectedValue).Content, senderStatus); if (senderStatus != Canlib.canStatus.canOK || readerStatus != Canlib.canStatus.canOK) { return; } //Bus On senderStatus = Canlib.canBusOn(sendHandle); readerStatus = Canlib.canBusOn(readHandle); CheckStatus("Bus on with bitrate " + ((ComboBoxItem)bitrateComboBox.SelectedValue).Content, senderStatus); if (senderStatus == Canlib.canStatus.canOK && readerStatus == Canlib.canStatus.canOK) { isSenderBusOn = true; isReaderBusOn = true; //This starts the DumpMessageLoop method if (!messageReceiver.IsBusy) { messageReceiver.RunWorkerAsync(); } busOnButton.IsEnabled = false; bitrateComboBox.IsEnabled = false; busOffButton.IsEnabled = true; sendMessageButton.IsEnabled = true; clearMessageButton.IsEnabled = true; buildMessageButton.IsEnabled = true; } else { MessageBox.Show("Bus On failed. Please try again."); } }
public KvaserCAN(MainWindow wnd) //public constructor { this.wnd = wnd; Canlib.canInitializeLibrary(); hcan = Canlib.canOpenChannel(channel, Canlib.canOPEN_REQUIRE_INIT_ACCESS); if (hcan < 0) { string error; Canlib.canGetErrorText((Canlib.canStatus)hcan, out error); send2Terminal(error); } else { Canlib.canSetBusParams(hcan, bitr, 0, 0, 0, 0, 0); //parameters set by dafault based on bitr Canlib.canBusOn(hcan); send2Terminal("Can liin avatud"); //DumpMessageLoop(hcan); } }
} // 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 _OpenAllConnections() { if (ActiveCanDriver == Consts.INTERFACE_RS232) { if (PortConnection.OpenPort()) { dic_CanChanels.Clear(); foreach (var entry in Globals.Vm_scis) { dic_CanChanels[entry.VM_UC_Main.NodeId] = 0; } Task.Factory.StartNew(() => { int i = 0; do { Globals.Vm_scis[0].HighFrequencyOn = true; FrequencyManager.SendCCComand(vm_sci: Globals.Vm_scis[0]); Thread.Sleep(5); } while (!graphStarted && i < 500);//max 25 seconds }); } } else if (ActiveCanDriver == Consts.CAN_DRIVER_KVASER) { lock (LockCanIdsDic) { hasChangeInNodesList = true; Canlib.canInitializeLibrary(); foreach (var value in dic_CanChanels.Where(x => x.Value >= 0).Select(x => x.Value)) { Canlib.canBusOff(value); Canlib.canClose(value); } //lastOpenedChanel = 0; dic_CanChanels.Clear(); foreach (var entry in Globals.Vm_scis.Where(entry => entry.VM_UC_Main != null)) { dic_CanChanels[entry.VM_UC_Main.NodeId] = -1; } foreach (var key in dic_CanChanels.Select(x => x.Key).ToList()) { int openedChanel = Canlib.canOpenChannel(lastOpenedChanel + 1, key); if (openedChanel >= 0) { dic_CanChanels[key] = openedChanel; } } foreach (var entry in dic_CanChanels.Where(x => x.Value >= 0)) { Canlib.canSetBusParams( handle: entry.Value, freq: CANDesiredBitRate, tseg2: 0, tseg1: 0, sjw: 0, noSamp: 0, syncmode: 0 ); Canlib.canBusOn(entry.Value); //if (!setCanCardName() || !getCanBitRate()) { // CANStatus = Consts.BOOL_FAILED; // CANHandle = -1; //} //initialize the node Canlib.canWrite( handle: entry.Value, id: 0, msg: new byte[] { 1, (byte)entry.Key }, dlc: 2, //size of msg flag: 2 //we have defined this as const ); } } //get multiply value WriteToAll("AD", 0, true, true); //get ISR WriteToAll("AE", 0, true, true); graphStarted = false; //start Graph //todo fix for each nodeId Task.Factory.StartNew(() => { int i = 0; do { if (Globals.Vm_scis.Count > 0) { Globals.Vm_scis[0].HighFrequencyOn = true; FrequencyManager.SendCCComand(1, vm_sci: Globals.Vm_scis[0]); // start graph plot with 6k frequency. Thread.Sleep(5); } else { Debugger.Break(); } } while (!graphStarted && i < 500 && Globals.Vm_scis.Count > 0);//max 25 seconds }); //_can_status = RoundBoolLed.DISABLED; } }
//initialise the usb interface library based on which interface was selected public static void initialiseCAN() { for (int i = 0; i < CAN_Channel.numOfChan; i++) { switch (CAN_Channel._INTERFACEs[i]) { case CAN_Channel.CAN_INTERFACE.KVASER: if (!KvaserInit) { Canlib.canInitializeLibrary(); } if (CanInit) { Close(); } numOfKvaser++; canHandle[i] = Canlib.canOpenChannel(numOfKvaser - 1, Canlib.canOPEN_ACCEPT_VIRTUAL); KvaserInit = true; //check whether handle was gotten successfully ErrorControl(handle: canHandle[i], location: "canOpenChannel: initialise()"); switch (CAN_Channel._BAUDRATEs[i]) { case CAN_Channel.CAN_BAUDRATE._250K: status = Canlib.canSetBusParams(canHandle[i], Canlib.canBITRATE_250K, 0, 0, 0, 0, 0); break; case CAN_Channel.CAN_BAUDRATE._500K: status = Canlib.canSetBusParams(canHandle[i], Canlib.canBITRATE_500K, 0, 0, 0, 0, 0); break; case CAN_Channel.CAN_BAUDRATE._1M: status = Canlib.canSetBusParams(canHandle[i], Canlib.canBITRATE_1M, 0, 0, 0, 0, 0); break; } ErrorControl(status: status, location: "canSetBusParams: initialise()"); Canlib.canSetBusOutputControl(canHandle[i], Canlib.canDRIVER_NORMAL); //turn the bus on with a handle to the open channel to write data Canlib.canBusOn(canHandle[i]); break; case CAN_Channel.CAN_INTERFACE.PEAK: if (CanInit) { Close(); } numOfPeak++; if (numOfPeak == 1) { canHandle[i] = PCANBasic.PCAN_USBBUS1; } if (numOfPeak == 2) { canHandle[i] = PCANBasic.PCAN_USBBUS2; } switch (CAN_Channel._BAUDRATEs[i]) { case CAN_Channel.CAN_BAUDRATE._250K: pCANBaudrate[numOfPeak - 1] = TPCANBaudrate.PCAN_BAUD_250K; break; case CAN_Channel.CAN_BAUDRATE._500K: pCANBaudrate[numOfPeak - 1] = TPCANBaudrate.PCAN_BAUD_500K; break; case CAN_Channel.CAN_BAUDRATE._1M: pCANBaudrate[numOfPeak - 1] = TPCANBaudrate.PCAN_BAUD_1M; break; } if (PCANBasic.Initialize((ushort)canHandle[i], pCANBaudrate[numOfPeak - 1]) == TPCANStatus.PCAN_ERROR_INITIALIZE) { ErrorControl(-1, location: "PCANBasic.initialize: initialise()"); return; } break; } } CanInit = true; numOfPeak = 0; numOfKvaser = 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() { Canlib.canInitializeLibrary(); //Check if bus is connected if (isOpen()) { close(); } Thread.Sleep(200); m_readThread = new Thread(readMessages) { Name = "KvaserCANDevice.m_readThread" }; if (!UseOnlyPBus) { if (TrionicECU == ECU.TRIONIC7) { logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()"); handleWrite = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)"); Canlib.canStatus statusSetParam1 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_47K_btr0, CAN_BAUD_BTR_47K_btr1); logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)"); Canlib.canStatus statusOn1 = Canlib.canBusOn(handleWrite); Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); // logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()"); handleRead = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)"); Canlib.canStatus statusSetParam2 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_47K_btr0, CAN_BAUD_BTR_47K_btr1); logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)"); Canlib.canStatus statusOn2 = Canlib.canBusOn(handleRead); Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); } else if (TrionicECU == ECU.TRIONIC8) { logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()"); handleWrite = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)"); Canlib.canStatus statusSetParam1 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_33K_btr0, CAN_BAUD_BTR_33K_btr1); logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)"); Canlib.canStatus statusOn1 = Canlib.canBusOn(handleWrite); Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); // logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()"); handleRead = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)"); Canlib.canStatus statusSetParam2 = Canlib.canSetBusParamsC200(handleWrite, CAN_BAUD_BTR_33K_btr0, CAN_BAUD_BTR_33K_btr1); logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)"); Canlib.canStatus statusOn2 = Canlib.canBusOn(handleRead); Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); } if (handleWrite < 0 || handleRead < 0) { return(OpenResult.OpenError); } logger.Debug("I bus connected"); if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); } m_endThread = false; //Check if P bus is connected logger.Debug("handle1 = canlibCLSNET.Canlib.canOpenChannel()"); handleWrite = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleWrite)"); Canlib.canStatus statusSetParamWrite = Canlib.canSetBusParams(handleWrite, Canlib.canBITRATE_500K, 0, 0, 0, 0, 0); logger.Debug("canlibCLSNET.Canlib.canBusOn(handleWrite)"); Canlib.canStatus statusOnWrite = Canlib.canBusOn(handleWrite); Canlib.canIoCtl(handleWrite, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); // logger.Debug("handle2 = canlibCLSNET.Canlib.canOpenChannel()"); handleRead = Canlib.canOpenChannel(ChannelNumber, Canlib.canOPEN_ACCEPT_VIRTUAL); logger.Debug("canlibCLSNET.Canlib.canSetBusParams(handleRead)"); Canlib.canStatus statusSetParamRead = Canlib.canSetBusParams(handleRead, Canlib.canBITRATE_500K, 0, 0, 0, 0, 0); logger.Debug("canlibCLSNET.Canlib.canBusOn(handleRead)"); Canlib.canStatus statusOnRead = Canlib.canBusOn(handleRead); Canlib.canIoCtl(handleRead, Canlib.canIOCTL_SET_LOCAL_TXECHO, 0); if (handleWrite < 0 || handleRead < 0) { return(OpenResult.OpenError); } logger.Debug("P bus connected"); if (m_readThread.ThreadState == ThreadState.Unstarted) { m_readThread.Start(); } return(OpenResult.OK); }
/// <summary> /// 打开CAN通道 /// </summary> /// <param name="channel"></param> /// <param name="baud"></param> /// <returns></returns> public bool OpenChannel(int channel, string baud) { int canFreq; canHandler = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_VIRTUAL); if (canHandler != (int)Canlib.canStatus.canOK) { return(false); } switch (baud) { case "50000": canFreq = Canlib.BAUD_50K; break; case "62000": canFreq = Canlib.BAUD_62K; break; case "83000": canFreq = Canlib.BAUD_83K; break; case "100000": canFreq = Canlib.BAUD_100K; break; case "125000": canFreq = Canlib.BAUD_125K; break; case "250000": canFreq = Canlib.BAUD_250K; break; case "500000": canFreq = Canlib.BAUD_500K; break; case "1000000": canFreq = Canlib.BAUD_1M; break; default: canFreq = Canlib.BAUD_500K; break; } Canlib.canStatus canStatus = Canlib.canSetBusParams(canHandler, canFreq, 0x80, 0x3A, 1, 1, 0); if (canStatus != Canlib.canStatus.canOK) { return(false); } canStatus = Canlib.canBusOn(canHandler); if (canStatus != Canlib.canStatus.canOK) { return(false); } return(true); }
//Sets the bitrate public void setBitrate(int bitrate) { Canlib.canStatus status = Canlib.canSetBusParams(handle, bitrate, 0, 0, 0, 0, 0); }