【问题标题】:Add message to ignore list if this message was repeated如果此消息重复,则将消息添加到忽略列表
【发布时间】:2015-10-26 21:11:03
【问题描述】:

如果此消息重复,也许有人可以提供如何忽略该消息的最佳方法,我应该使用什么来忽略,将消息保存在列表中并遍历列表或将所有消息保存到文件中,最好的方法是什么?我正在使用此代码从串口读取数据:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace canSniff
    {
    class PortDataReceived
    {
        public static void Main()
        {
            SerialPort mySerialPort = new SerialPort("COM4");

            mySerialPort.BaudRate = 115200;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;

            mySerialPort.DataReceived += new  SerialDataReceivedEventHandler(DataReceivedHandler);

            mySerialPort.Open();

            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();

        }

        private static void DataReceivedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);
        }
    }
}

【问题讨论】:

  • 要忽略上一条消息,清除缓冲区:mySerialPort.DiscardInBuffer() 更多信息,请看这里的答案:stackoverflow.com/questions/11571522/…
  • 我需要忽略 5 分钟,例如,当所有标准消息将被忽略时,我将按下按钮并且新代码将到达,我应该处理此按钮代码
  • 你能告诉我吗,这将像这样工作:所有收到的消息我将存储到缓冲区中,如果 100 条不同的消息之一将再次到达并到达缓冲区中的相同消息,则此消息将是忽略?

标签: c# serial-port filtering


【解决方案1】:

您可以使用HashSet<string> 来存储收到的消息。 如果您应该将其保存为文件,取决于应用程序的使用方式。只要应用程序正在运行,下面的代码就知道收到的消息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;

namespace canSniff
    {
    class PortDataReceived
    {

        private HashSet<string> _messages = new HashSet<string>();

        public static void Main()
        {
            SerialPort mySerialPort = new SerialPort("COM4");

            mySerialPort.BaudRate = 115200;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None;
            mySerialPort.RtsEnable = true;

            mySerialPort.DataReceived += new  SerialDataReceivedEventHandler(DataReceivedHandler);

            mySerialPort.Open();

            Console.WriteLine("Press any key to continue...");
            Console.WriteLine();
            Console.ReadKey();

        }

        private static void DataReceivedHandler(
                            object sender,
                            SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();

            if(_messages.Add(indata))
            {
                 // the message was added 
                 Console.WriteLine("Data Received:");
                 Console.Write(indata);
            }
            else
            {
                  // do something with the omitted message that was allready in the list
            }
        }
    }
}

【讨论】:

  • 谢谢,我会稍微了解一下 HashSet。你是对的,当应用程序运行时,消息应该被忽略一次
  • 如果这是您期望的答案,您可以投票并接受它作为答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-23
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 2019-05-24
相关资源
最近更新 更多