private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { List <byte> listBuffer = new List <byte>(); ReadBuffer(ref listBuffer); Thread.Sleep(50); //再次读取 ReadBuffer(ref listBuffer); if (listBuffer.Any()) { LogD.Info($"收到的原始报文:{ByteHelper.ToHexString(listBuffer.ToArray())}"); var requestInfo = SubstatonFilter.Filter(listBuffer); if (requestInfo == null) { IsBitError = true; } else { IsBitError = false; //触发事件 } _sendCommand.IsReceiveResponse = true; DataReceivedEvent?.Invoke(this, new ChannelDataEventArgs(_sendCommand, requestInfo)); } } catch (Exception ex) { _sendCommand.IsReceiveResponse = false; LogD.Error($"SerialPort_DataReceived:{ex}"); } }
public void Start(PluginMonitor databaseMonitor, PluginMonitor dasMonitor) { try { #if DEBUG var hexString = "01 03 1E 00 00 00 00 00 00 00 00 42 C7 D0 13 00 00 41 D0 0A F1 00 00 41 EA 80 00 00 00 41 EE CF 38 6F 91"; var bytes = ByteHelper.HexStringToByteArray(hexString); var substationData = new SubStationData(1, bytes.Skip(3).Take(0x1E).ToArray()); #endif _databaseMonitor = databaseMonitor; _dasMonitor = dasMonitor; _dases.AddRange(InitDas()); _dases.ToList().ForEach(o => o.Start()); _customCommandService.Start(); _customCommandService.ConfigMonitoringServerEvent += CustomCommandService_ConfigMonitoringServerEvent; _customCommandService.ConfigSubstationEvent += CustomCommandService_ConfigSubstationEvent; _customCommandService.ConfigFluxEvent += CustomCommandService_ConfigFluxEvent; _dataBulkService.Start(_databaseMonitor); DasConfig.Repo.EndAlarmToday(); DasConfig.Repo.EndAnalogAlarm(); } catch (Exception ex) { LogD.Error(ex.ToString()); } }
public void Start() { Task.Factory.StartNew(() => { CultureInfoHelper.SetDateTimeFormat(); while (!_cts.IsCancellationRequested) { // das 循环发送命令, 并接受报文. try { //if (Comm == null || !Comm.IsConnect) //{ // Log($"网络断开,{Comm}重新连接."); // Comm?.Start(); // Thread.Sleep(3000); // continue; //} GatherData(); } catch (Exception e) { LogD.Error("轮询中出现错误:" + e); } finally { Thread.Sleep(50); } } }, _cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default); }
public void Stop() { try { _dases.ForEach(o => o.Stop()); _dataBulkService.Stop(); _customCommandService.Stop(); _customCommandService.ConfigMonitoringServerEvent -= CustomCommandService_ConfigMonitoringServerEvent; _customCommandService.ConfigSubstationEvent -= CustomCommandService_ConfigSubstationEvent; _customCommandService.ConfigFluxEvent -= CustomCommandService_ConfigFluxEvent; } catch (Exception ex) { LogD.Error(ex.ToString()); } }
public void Stop() { try { _cts.Cancel(); if (Comm != null) { Comm.DataReceivedEvent -= Comm_DataReceivedEvent; Comm.Close(); } } catch (Exception ex) { LogD.Error(ex.ToString()); } }