serialport连续实时数据c#

时间:2022-11-22 21:30:09

Hi I'm trying to program a simple C# WPF that displays time information on a virtual scoreboard in real time from a timing system. I'm fairly new to programming so in depth explanation would be appreciated.

嗨,我正在尝试编写一个简单的C#WPF,它可以从计时系统实时显示虚拟记分板上的时间信息。我对编程很新,所以深入解释将不胜感激。

I have created a new thread to handle the incoming data from the COM port and as the app is developed this data will be interpreted. For now I just wanted to display the raw information (in hex) that is coming from the timer into a textbox. This works but not as intended. I am receiving tons of duplicate information, my only explanation is I am reading the data too slowly or its reading the same byte over and over. What I would like to happen is to take out each byte and display them, all controlled by one start/stop button.

我创建了一个新线程来处理来自COM端口的传入数据,并且在开发应用程序时,将解释此数据。现在我只想将来自计时器的原始信息(以十六进制)显示到文本框中。这可行但不是预期的。我收到了大量的重复信息,我唯一的解释是我读的数据太慢或者一遍又一遍地读取相同的字节。我想要发生的是取出每个字节并显示它们,所有这些都由一个开始/停止按钮控制。

Possible solutions include storing the entire buffer in a list or array which I'm not quite sure of yet, I don't want to add so many threads that the program freezes everything up.

可能的解决方案包括将整个缓冲区存储在我还不太确定的列表或数组中,我不想添加这么多线程,程序会冻结所有内容。

Here is my code so far (I'm new to pretty much all the code I have written here, so if anything is bad practice please let me know):

这是我到目前为止的代码(我几乎是我在这里编写的所有代码的新手,所以如果有什么不好的做法请告诉我):

public partial class MainWindow : Window
{
    SerialPort comms;
    Thread commThread;

    bool flag;
    string message;


    public MainWindow()
    {
        InitializeComponent();
        comms = new SerialPort();
    }

    private void PortControl_Click(object sender, RoutedEventArgs e)
    {
        if (!comms.IsOpen)
        {
            PortControl.Content = "Stop";
            comms.PortName = "COM1";
            comms.BaudRate = 9600;
            comms.DataBits = 8;
            comms.StopBits = StopBits.One;
            comms.Parity = Parity.Even;
            comms.ReadTimeout = 500;
            comms.ReceivedBytesThreshold = 1;

            commThread = new Thread(new ThreadStart(Handle));

            comms.Open();

            comms.DataReceived += new SerialDataReceivedEventHandler(ReadIn);
        }
        else
        {
            PortControl.Content = "Start";
            flag = false;
            comms.DataReceived -= ReadIn;

            commThread.Join();
            comms.Close();
        }
    }

    private void ReadIn(object sender, SerialDataReceivedEventArgs e)
    {
        if (!commThread.IsAlive)
        {
            flag = true;
            commThread.Start();
        }
    }

      private void Handle()
      {
          while (flag)
          {
              if (comms.IsOpen)
              {
                  try
                  {
                      message = comms.ReadByte().ToString("X2");

                      Dispatcher.BeginInvoke((Action)(() =>
                      {
                          ConsoleBox.Text += message + " ";
                      }));
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
              }
          }
      }


}

1 个解决方案

#1


0  

Here is one solution.

这是一个解决方案。

The serial port is receiving the data in its own thread, and you should read the incoming bytes in the data received handler.

串行端口在其自己的线程中接收数据,您应该读取数据接收处理程序中的传入字节。

I propose to read the data and add it to a thread-safe FIFO list in the data received handler and read the data from the list in the main thread.

我建议读取数据并将其添加到数据接收处理程序中的线程安全FIFO列表中,并从主线程中的列表中读取数据。

See my solution in post Serial port reading + Threads or something better?

在帖子串口读取+线程或更好的东西中查看我的解决方案?

#1


0  

Here is one solution.

这是一个解决方案。

The serial port is receiving the data in its own thread, and you should read the incoming bytes in the data received handler.

串行端口在其自己的线程中接收数据,您应该读取数据接收处理程序中的传入字节。

I propose to read the data and add it to a thread-safe FIFO list in the data received handler and read the data from the list in the main thread.

我建议读取数据并将其添加到数据接收处理程序中的线程安全FIFO列表中,并从主线程中的列表中读取数据。

See my solution in post Serial port reading + Threads or something better?

在帖子串口读取+线程或更好的东西中查看我的解决方案?