//close the can bus after transmission public static void Close() { for (int i = 0; i < CAN_Channel.numOfChan; i++) { switch (CAN_Channel._INTERFACEs[i]) { case CAN_Channel.CAN_INTERFACE.KVASER: Canlib.canBusOff(canHandle[i]); status = Canlib.canClose(canHandle[i]); canHandle[i] = 0; KvaserInit = false; ErrorControl(status: status, location: "canClose: Close()"); break; case CAN_Channel.CAN_INTERFACE.PEAK: PCANBasic.Uninitialize((ushort)canHandle[i]); break; } } CanInit = false; }
void closeChannelButton_Click(object sender, RoutedEventArgs e) { if (sendHandle >= 0 && readHandle >= 0) { if (isSenderBusOn || isReaderBusOn) { busOffButton_Click(sender, e); } Canlib.canStatus senderStatus = Canlib.canClose(sendHandle); Canlib.canStatus readerStatus = Canlib.canClose(readHandle); CheckStatus("Closing channel " + channelName, senderStatus); sendHandle = -1; readHandle = -1; closeChannelButton.IsEnabled = false; busOnButton.IsEnabled = false; busOffButton.IsEnabled = false; bitrateComboBox.IsEnabled = false; sendMessageButton.IsEnabled = false; clearMessageButton.IsEnabled = false; buildMessageButton.IsEnabled = false; openChannelButton.IsEnabled = true; //channelBox.IsEnabled = true; channelComboBox.IsEnabled = true; } else { MessageBox.Show("Closing Channel " + channelName + " failed. Please try again."); } }
public static void CloseAllConnections() { new Thread( () => { lock (LockCanIdsDic) { foreach (var entry in Instance.dic_CanChanels) { byte[] packet = Utils.makePacket(entry.Key, "CC", 0, false, false, "0"); for (int i = 0; i < 3; i++) { Canlib.canWrite(entry.Value, 0x37f, packet, packet.Length, 2); Write(packet); Thread.Sleep(50); } Canlib.canWrite( handle: entry.Value, id: 0, msg: new byte[] { 0x80, (byte)entry.Key }, dlc: 2, //size of msg flag: 2 //we have defined this as const ); Canlib.canBusOff(entry.Value); Canlib.canClose(entry.Value); } } }).Start(); }
// private CAN_Common(){ //#if DEBUG // //Task.Factory.StartNew( // // action: () =>{ // // Thread.Sleep(2000); // // float f = 0; // // Stopwatch sw = Stopwatch.StartNew(); // // Random rand = new Random(); // // FrequencyManager.SendCCComand(1, Globals.Vm_scis[0]); // // while (true){ // // short sin = (short)((Math.Sin(f) * Math.Pow(2, 15))); // // byte[] data = new byte[12]; // // Buffer.BlockCopy(Utils.makePacket("FT", 1, false, true, (Math.Sin(f) * 100).ToString()),0,data,0,8); // // data[5] = (byte)(sin >> 8); // // data[4] = (byte)sin; // // data[6] = 0; // // data[7] = 0; // // innerTranmit(data); // // //SaveToDisk.Write(data); // // while (sw.ElapsedMilliseconds < f *5) ; // // f+=0.01f; // // } // // }); //#endif // } //private CAN_Common(){ // //ActiveCANDriver = Tbl_CanDriver.ActiveCANDriver; // Task.Factory.StartNew( // action: () =>{ // ActiveCANDriver = Static_Utils.ActiveCANDriver; // if (ActiveCANDriver.Equals(Consts.CAN_DRIVER_KVASER)){ // initForKvaser(); // } else if (ActiveCANDriver.Equals(Consts.CAN_DRIVER_GINKGO)){ // initForGinkgo(); // } // initLoop(); // }); // //Active = true; //} #endregion #region METHODS public static void CloseConnection(int NodeId) { if (!Instance.dic_CanChanels.ContainsKey(NodeId)) { return; } byte[] packet = Utils.makePacket(NodeId, "CC", 0, false, false, "0"); for (int i = 0; i < 3; i++) { Canlib.canWrite(Instance.dic_CanChanels[NodeId], 0x37f, packet, packet.Length, 2); Write(packet); Thread.Sleep(50); } Canlib.canWrite( handle: Instance.dic_CanChanels[NodeId], id: 0, msg: new byte[] { 0x80, (byte)NodeId }, dlc: 2, //size of msg flag: 2 //we have defined this as const ); Canlib.canBusOff(Instance.dic_CanChanels[NodeId]); Canlib.canClose(Instance.dic_CanChanels[NodeId]); }
public void deinitCan() { if (hcan >= 0) { Canlib.canBusOff(hcan); Canlib.canClose(hcan); } hcan = -1; }
public FuctionResult Close() { if (handle != -1) { Canlib.canStatus cs = Canlib.canBusOff(handle); CheckStatus("Bus off", cs); } onBus = false; Canlib.canStatus cs1 = Canlib.canClose(handle); CheckStatus("Closing channel", cs1); handle = -1; return(FuctionResult.OK); }
// ********************** // Turns canInterface off // ********************** public static void CanBusOff(string canInterface) { string hardwareString0 = canInterface.Replace(" ", ""); string[] msgOutput = hardwareString0.Split(';'); Canlib.canStatus status; // MainWindow.ErrorDisplayString("Bus Off - BusInterface: " + canInterface + " ; Handle: " + BusInterface.ReturnHandle(canInterface)); // Canlib.canClose(BusInterface.ReturnHandle(canInterface)); // status = Canlib.canBusOff(BusInterface.ReturnHandle(canInterface)); Canlib.canClose(Convert.ToInt32(msgOutput[3])); status = Canlib.canBusOff(Convert.ToInt32(msgOutput[3])); if (status < 0) { ErrorLog.NewLogEntry("CAN", "Bus Off Failed: " + msgOutput[1]); } else { ErrorLog.NewLogEntry("CAN", "Bus Off Success: " + msgOutput[1]); } }
/// <summary> /// The close method closes the device. /// </summary> /// <returns>CloseResult.OK on success, otherwise CloseResult.CloseError.</returns> override public CloseResult close() { m_endThread = true; Canlib.canStatus statusBusOff1 = Canlib.canStatus.canOK; Canlib.canStatus statusBusOff2 = Canlib.canStatus.canOK; Canlib.canStatus statusCanClose1 = Canlib.canStatus.canOK; Canlib.canStatus statusCanClose2 = Canlib.canStatus.canOK; if (handleWrite >= 0) { statusBusOff1 = Canlib.canBusOff(handleWrite); logger.Debug("canlibCLSNET.Canlib.canBusOff(handleWrite)"); statusCanClose1 = Canlib.canClose(handleWrite); logger.Debug("canlibCLSNET.Canlib.canClose(handleWrite)"); } if (handleRead >= 0) { statusBusOff2 = Canlib.canBusOff(handleRead); logger.Debug("canlibCLSNET.Canlib.canBusOff(handleRead)"); statusCanClose2 = Canlib.canClose(handleRead); logger.Debug("canlibCLSNET.Canlib.canClose(handleRead)"); } handleWrite = -1; handleRead = -1; if (Canlib.canStatus.canOK == statusBusOff1 && Canlib.canStatus.canOK == statusBusOff2 && Canlib.canStatus.canOK == statusCanClose1 && Canlib.canStatus.canOK == statusCanClose2) { return(CloseResult.OK); } else { return(CloseResult.CloseError); } }
private void closeButton_Click(object sender, RoutedEventArgs e) { Canlib.canStatus status = Canlib.canClose(handle); CheckStatus("Closing channel", status); handle = -1; }
public bool CanLongMsgSend(int id, byte[] data, int datalength, int retrycount, double timeout, ref int EcuID, ref byte[] READDATA, ref string errmsg) { if ((datalength < 9) || (data.Length < 9)) { errmsg = "Command Data Length <=8"; return(false); } bool _LoopChk = true; int _EcuID; byte[] _EcuData = new byte[8]; int _EcuDlc; int _EcuFlags; long _EcuTime; int _Ecu_FS = -1; // 정상적으로 수신시 확인되는 값, FS FlowStatus, BS BlockSize, STmin int _Ecu_BS = 0; int _Ecu_STmin = 0; double _Ecu_Stmin_sec; // 소수점단위 계산 bool _Ecu_FlowControl_Receive_OK = false; byte[] _Data_FF = new byte[8]; byte[] _Data_CF = new byte[8]; int _LastSendDataIndex = 0; Canlib.canStatus status; Stopwatch _stopwatch_total = new Stopwatch(); Stopwatch _stopwatch_send = new Stopwatch(); // 명령 만들기 _Data_FF[0] = 0x10; Int32 _DataLength_12bit = 0x00000000; _DataLength_12bit = (Int32)datalength; _DataLength_12bit = _DataLength_12bit & 0x00000FFF; byte byte1Lownibble; byte byte2; byte1Lownibble = (byte)((_DataLength_12bit & 0x00000F00) >> 16); byte2 = (byte)(_DataLength_12bit & 0x000000FF); _Data_FF[0] = (byte)(0x10 | byte1Lownibble); _Data_FF[1] = byte2; _Data_FF[2] = data[0]; _Data_FF[3] = data[1]; _Data_FF[4] = data[2]; _Data_FF[5] = data[3]; _Data_FF[6] = data[4]; _Data_FF[7] = data[5]; _LastSendDataIndex = 6; // //Canlib.canBusOn(handle); //status = Canlib.canWrite(handle, id, _Data_FF, 8, flags); //Open up a new handle for reading readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_LARGE_DLC); status = Canlib.canAccept(readHandle, 0x7DF, Canlib.canFILTER_SET_CODE_STD); status = Canlib.canAccept(readHandle, 0x7D0, Canlib.canFILTER_SET_MASK_STD); status = Canlib.canBusOn(readHandle); if (status == Canlib.canStatus.canOK) { onBus = true; } else { onBus = false; } if (readHandle < 0) { errmsg = "canOpenChannel Handle create error"; return(false); } if (!onBus) { errmsg = "onBus false"; return(false); } _stopwatch_total.Reset(); _stopwatch_total.Start(); _stopwatch_send.Reset(); _stopwatch_send.Start(); // Receive Channel Open -> SEND Canlib.canBusOn(handle); status = Canlib.canWrite(handle, id, _Data_FF, 8, flags); while (_LoopChk) { // TimeOut Check long st = _stopwatch_total.ElapsedMilliseconds; double _durationtime = st / 1000.0; // ms -> sec if (_durationtime > timeout) { // 설정시간안에 메세지 및 처리를 못했을 경우 종료 errmsg = "timeout error"; _LoopChk = false; break; } // Message Recieve Check bool _RecvOK = false; status = Canlib.canReadWait(readHandle, out _EcuID, _EcuData, out _EcuDlc, out _EcuFlags, out _EcuTime, 100); // 100ms timeout if (status == Canlib.canStatus.canOK) { if ((_EcuFlags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME) { errmsg = "***ERROR FRAME RECEIVED***"; } else { errmsg = String.Format("ID={0:X4} {1} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2} {10}\r", _EcuID, _EcuDlc, _EcuData[0], _EcuData[1], _EcuData[2], _EcuData[3], _EcuData[4], _EcuData[5], _EcuData[6], _EcuData[7], _EcuTime); if (_EcuID == 0x07D9) { _RecvOK = true; } } } else if (status != Canlib.canStatus.canERR_NOMSG) { errmsg = "TimeOut:NO MSG"; } //CheckStatus("Read message " + errmsg, status); //Canlib.canBusOff(readHandle); if ((_EcuID == 0x07D9) && (_RecvOK)) { byte EcuFC_Check = _EcuData[0]; EcuFC_Check = (byte)((EcuFC_Check & 0x00F0) >> 4); if (EcuFC_Check == 3) { _LoopChk = false; _Ecu_FS = (_EcuData[0] & 0x000F); _Ecu_BS = (int)(_EcuData[1] & 0x00FF); _Ecu_STmin = (int)(_EcuData[2] & 0x00FF); if (_Ecu_STmin <= 0x7F) { _Ecu_Stmin_sec = _Ecu_STmin * 0.001; } if (_Ecu_STmin >= 0xF1) { _Ecu_Stmin_sec = (_Ecu_STmin - 0xF0) * 0.0001; } _Ecu_FlowControl_Receive_OK = true; } string sndmsg = String.Format("ID={0:X4} {1} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2}\r", id, 8, _Data_FF[0], _Data_FF[1], _Data_FF[2], _Data_FF[3], _Data_FF[4], _Data_FF[5], _Data_FF[6], _Data_FF[7]); CheckStatus("PC->ECU " + sndmsg, Canlib.canStatus.canOK); CheckStatus("ECU->PC " + errmsg, status); } } Canlib.canClose(readHandle); if (!_Ecu_FlowControl_Receive_OK) { errmsg = "Ecu FlowControl not arrived"; return(false); } if (_Ecu_FS > 0) { errmsg = "Ecu FlowControl signal Busy"; return(false); } // ECU에서 FC를 정상적으로 수신후 데이터 전송.... _stopwatch_total.Reset(); _stopwatch_total.Start(); _stopwatch_send.Reset(); _stopwatch_send.Start(); _LoopChk = true; int _SequenceNumber = 1; byte _SN = (byte)_SequenceNumber; int _BlockSendLoopCount = _Ecu_BS; Canlib.canBusOn(handle); bool _EcuLastReceiveOK = false; // CF 전송후 수신확인. //Open up a new handle for reading readHandle = Canlib.canOpenChannel(channel, Canlib.canOPEN_ACCEPT_LARGE_DLC); status = Canlib.canAccept(readHandle, 0x7DF, Canlib.canFILTER_SET_CODE_STD); status = Canlib.canAccept(readHandle, 0x7D0, Canlib.canFILTER_SET_MASK_STD); status = Canlib.canBusOn(readHandle); if (status == Canlib.canStatus.canOK) { onBus = true; } else { onBus = false; } if (readHandle < 0) { errmsg = "canOpenChannel Handle create error"; return(false); } if (!onBus) { errmsg = "onBus false"; return(false); } while (_LoopChk) { // TimeOut Check long st = _stopwatch_total.ElapsedMilliseconds; double _durationtime = st / 1000.0; // ms -> sec if (_durationtime > timeout) { // 설정시간안에 메세지 및 처리를 못했을 경우 종료 errmsg = "timeout error"; _LoopChk = false; break; } if (datalength < _LastSendDataIndex) { _LoopChk = false; break; } _stopwatch_send.Reset(); _stopwatch_send.Start(); // CF 전송 for (int hs = 0; hs < _BlockSendLoopCount; hs++) { // FF전송후 CF 데이터 전송 준비 _Data_CF[0] = 0x20; _Data_CF[0] = (byte)(_Data_CF[0] + _SN); for (int i = 0; i < 7; i++) { if (_LastSendDataIndex < datalength) { _Data_CF[1 + i] = data[_LastSendDataIndex]; } else { _Data_CF[1 + i] = 0x00; // 0xAA; } _LastSendDataIndex++; } _SN++; if (_SN > 16) { _SN = 0; } status = Canlib.canWrite(handle, id, _Data_CF, 8, flags); long _dt = _stopwatch_send.ElapsedMilliseconds; string sndmsg = String.Format("ID={0:X4} {1} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2} {10:D5}ms\r", id, 8, _Data_CF[0], _Data_CF[1], _Data_CF[2], _Data_CF[3], _Data_CF[4], _Data_CF[5], _Data_CF[6], _Data_CF[7], _dt); CheckStatus("PC->ECU " + sndmsg, status); break; // Only One Run-----------------------------------------------실제 전송될 데이터 길이만큼만 전송함---------------------------------------------------------- } // Message Recieve Check bool _RecvOK = false; bool _FinalReceiveCheck = true; Stopwatch _Final = new Stopwatch(); _Final.Reset(); _Final.Start(); while (_FinalReceiveCheck) { if ((_Final.ElapsedMilliseconds / 1000.0) > 3.0) { errmsg = "Finnal ECU Receive Msg TIMEOUT"; _FinalReceiveCheck = false; break; } status = Canlib.canReadWait(readHandle, out _EcuID, _EcuData, out _EcuDlc, out _EcuFlags, out _EcuTime, 100); // 100ms timeout if (status == Canlib.canStatus.canOK) { if ((_EcuFlags & Canlib.canMSG_ERROR_FRAME) == Canlib.canMSG_ERROR_FRAME) { errmsg = "***ERROR FRAME RECEIVED***"; } else { errmsg = String.Format("ID={0:X4} {1} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2} {10}\r", _EcuID, _EcuDlc, _EcuData[0], _EcuData[1], _EcuData[2], _EcuData[3], _EcuData[4], _EcuData[5], _EcuData[6], _EcuData[7], _EcuTime); if (_EcuID == 0x07D9) { _RecvOK = true; CheckStatus("ECU->PC " + errmsg, status); } } } else if (status != Canlib.canStatus.canERR_NOMSG) { errmsg = "TimeOut:NO MSG"; } //CheckStatus("Read message " + errmsg, status); //Canlib.canBusOff(readHandle); if ((_EcuID == 0x07D9) && (_RecvOK)) { Canlib.canBusOn(handle); byte[] temp = new byte[8] { 0x30, 0x00, 0x02, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA }; status = Canlib.canWrite(handle, 0x07D1, _Data_FF, 8, flags); CheckStatus("PC->ECU " + "PC AutoResponse(0x30,0x00,0x02,0xAA,0xAA,0xAA,0xAA,0xaa", status); Canlib.canBusOff(handle); status = Canlib.canReadWait(readHandle, out _EcuID, _EcuData, out _EcuDlc, out _EcuFlags, out _EcuTime, 100); if (status == Canlib.canStatus.canOK) { errmsg = String.Format("ID={0:X4} {1} {2:X2} {3:X2} {4:X2} {5:X2} {6:X2} {7:X2} {8:X2} {9:X2} {10}\r", _EcuID, _EcuDlc, _EcuData[0], _EcuData[1], _EcuData[2], _EcuData[3], _EcuData[4], _EcuData[5], _EcuData[6], _EcuData[7], _EcuTime); if (_EcuID == 0x07D9) { _RecvOK = true; CheckStatus("ECU->PC " + errmsg, status); } } _FinalReceiveCheck = false; _EcuLastReceiveOK = true; _LoopChk = false; break; } } } if (readHandle >= 0) { Canlib.canClose(readHandle); } if (_EcuLastReceiveOK) { if (_EcuData[1] == 0x62) { return(true); } return(false); } else { return(false); } }
public void CAN_CLOSE() { Canlib.canStatus status = Canlib.canClose(handle); CheckStatus("Closing channel", status); handle = -1; }
/* * Closes the channel. Also stops any automatic transmission. */ public void closeChannel() { channelOn = false; Canlib.canBusOff(chanHandle); Canlib.canClose(chanHandle); }
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; } }
/// <summary> /// 关闭CAN通道 /// </summary> /// <returns></returns> public bool CloseChannel() { Canlib.canBusOff(canHandler); Canlib.canClose(canHandler); return(true); }
public void closeChannel() { Canlib.canStatus status = Canlib.canClose(handle); handle = -1; }