/// <summary> /// Close channel. Need first connected to owner. /// </summary> public void Close() { if (!Owner.IsConnected) { IsOpen = false; throw new Exception("open device connection first"); } SetChannelInfo data = new SetChannelInfo(); data.ChannelId = Index; data.Status = CanState.Closed; byte[] buf = (byte[])data; lock (_port) { _port.Write(buf, 0, buf.Length); } }
/// <summary> /// Open channel. Need first connected to owner. /// If Channel already opened and params isn't equals then channel will be close and reopen. /// </summary> /// <param name="bitrate">Bitrate CAN bus in kbps.</param> /// <param name="isListenOnly">Flag which indicate CAN transiever mode.</param> public void Open(int bitrate = 500, bool isListenOnly = true) { if (!Owner.IsConnected) { IsOpen = false; throw new Exception("open device connection first"); } if (IsOpen) { if (isListenOnly == this.IsListenOnly && Bitrate == bitrate) { return; } else { Close(); } } SetChannelInfo data = new SetChannelInfo(); data.ChannelId = Index; data.Status = isListenOnly ? CanState.OpenedListenOnly : CanState.OpenedNormal; //set bitrate switch (bitrate) { case 10: data.BitrateType = BitrateType.kpbs10; break; case 20: data.BitrateType = BitrateType.kpbs20; break; case 50: data.BitrateType = BitrateType.kpbs50; break; case 83: case 84: data.BitrateType = BitrateType.kpbs83; break; case 100: data.BitrateType = BitrateType.kpbs100; break; case 125: data.BitrateType = BitrateType.kpbs125; break; case 250: data.BitrateType = BitrateType.kpbs250; break; case 500: data.BitrateType = BitrateType.kpbs500; break; case 800: data.BitrateType = BitrateType.kpbs800; break; case 1000: data.BitrateType = BitrateType.kpbs1000; break; default: throw new ArgumentException("invalid bitrate. Support only 10, 20, 50, 100, 125, 250, 500, 800 and 1000 kbps"); } byte[] buf = (byte[])data; lock (_port) { _port.Write(buf, 0, buf.Length); } }