/// <summary> /// Инициализировать соединение со slave-устройством по HART-протоколу. /// </summary> /// <param name="connector">Интерфейс обмена данными между устройствами.</param> /// <param name="isSecondaryMaster"><see langword="true"/>, если связь установлена со вторичным мастером.</param> /// <param name="frameFormat">Формат кадра.</param> public HartProtocol(IConnector connector, bool isSecondaryMaster, FrameFormats frameFormat) { _connector = connector; IsSecondaryMaster = isSecondaryMaster; FrameFormat = frameFormat; _connector.DataReceived += GetNewMessage; }
/// <summary> /// Получить адрес slave-устройства, отправившего ответ.. /// </summary> /// <param name="data">Данные для десериализации.</param> /// <param name="offset">Смещение в массиве <paramref name="data"/>, с которого начинается чтение данных.</param> /// <param name="frameFormat">Формат фрейма.</param> /// <returns></returns> private static byte[] SetAddress(byte[] data, int offset, FrameFormats frameFormat) { byte[] result = null; switch (frameFormat) { case FrameFormats.Short: result = new byte[1]; result[0] = data[offset]; break; case FrameFormats.Long: result = new byte[5]; for (var i = 0; i < 5; i++) { result[i] = data[i + offset]; } break; } return(result); }
public CANSpyViewModel(int line) { if (line == 1) { Title = "CAN Line 1"; } else { Title = "CAN Line 2"; } bitTimings = new List <int>(); bitTimings.Add(50000); bitTimings.Add(100000); bitTimings.Add(125000); bitTimings.Add(200000); bitTimings.Add(250000); bitTimings.Add(400000); bitTimings.Add(500000); bitTimings.Add(1000000); selectedBitTiming = 50000; samplingPoints = new List <double>(); samplingPoints.Add(75.00); samplingPoints.Add(87.50); selectedSamplingPoint = 75.00; frameFormats = new List <string>(); FrameFormats.Add(frameFormat11Bit); FrameFormats.Add(frameFormat29Bit); selectedFrameFormat = frameFormat11Bit; enableErrorReception = true; applyMask = false; lineNumber = line; isSpying = false; requestBufferTimer = new Timer(5000); requestBufferTimer.Elapsed += UpdateMonitorBuffer; requestBufferTimer.AutoReset = true; requestBufferTimer.Enabled = false; MonitorBuffer = new List <CANSpyMessage>(); StartCommand = new Command(async() => { try { var param = new CANSpyParameters(); param.BitTiming = SelectedBitTiming; //param.SamplingPoint = SelectedSamplingPoint; if (selectedFrameFormat == frameFormat11Bit) { param.FrameFormat = CANSpyParameters.SimpleFrameFormat; } else { param.FrameFormat = CANSpyParameters.LongFrameFormat; } param.ErrorReception = EnableErrorReception; param.ApplyMask = ApplyMask; param.Mask = Convert.ToUInt32(Mask, 16); param.ID = Convert.ToUInt32(ID, 16); if (lineNumber == 1) { await AnalyzerDevice.SetCANParametersAsync(Services.SpyType.CANSpyOne, param); await AnalyzerDevice.StartSpyAsync(Services.SpyType.CANSpyOne); } else if (lineNumber == 2) { await AnalyzerDevice.SetCANParametersAsync(Services.SpyType.CANSpyTwo, param); await AnalyzerDevice.StartSpyAsync(Services.SpyType.CANSpyTwo); } Device.BeginInvokeOnMainThread(() => { IsSpying = true; }); requestBufferTimer.Enabled = true; } catch (Exception ex) { MessagingCenter.Send <CANSpyViewModel, string>(this, "StartError", ex.Message); } }); StopCommand = new Command(async() => { try { if (lineNumber == 1) { await AnalyzerDevice.StopSpyAsync(Services.SpyType.CANSpyOne); } else if (lineNumber == 2) { await AnalyzerDevice.StopSpyAsync(Services.SpyType.CANSpyTwo); } Device.BeginInvokeOnMainThread(() => { IsSpying = false; }); requestBufferTimer.Enabled = false; } catch (Exception ex) { MessagingCenter.Send <CANSpyViewModel, string>(this, "StopError", ex.Message); } }); }