C# 中最快的串行端口通信

本文关键字:串行端口 通信 | 更新日期: 2023-09-27 17:56:18

我是这个论坛的新手,我没有很多用C#编程的经验。我用C#构建了简单的Windows应用程序,以便与我的朋友给我的一些电子板进行通信。他在微控制器中定义了38400的串行端口波特率。我想尽快通过串行端口发送和接收字节。使用 C# 中现有的串行端口工具,我只能读取字节,但即使在自己的线程中使用事件处理程序,速度仍然不够快。我的缓冲区[4096]在几秒钟内就满了。

我的问题是是否存在一些其他不占用太多处理器时间的串口通信功能或工具。我需要在不到 200 个我们的时间里读取一个字节。我在嵌入式系统方面有更多的经验,这不是问题。

感谢大家的一些建议或想法。

托马兹

C# 中最快的串行端口通信

感谢所有回复。我可以读取来自板的所有字节,但我不能足够快地回复。这是我的代码。我使用了一个事件来读取缓冲区。如果我尝试在事件中回复,则下一个字节包含错误的值。

#region namespace usings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;
using Zaber.Serial.Core;
using Diagnostics = System.Diagnostics;
#endregion

命名空间Lucifire_aplication{

public partial class Form1 : Form
{

    int k = 1;
    string number_of_bytes;
    byte checksum;
    byte[] buffer = new byte[11];
    byte[] buffer_old = new byte[11];

    public Form1()
    {
        InitializeComponent();
        configure_button.Enabled = false;
        start_button.Enabled = false;
        foreach (string s in SerialPort.GetPortNames())
        {
            serial_port_name.Items.Add(s);
        }
        serial_port_name.Text = "Select port";
        for (int i = 4800; i <= 38400; i = i * 2)
        {
            baud_rate.Items.Add(i);
        }
        for (int i = 57600; i <= 921600; i = i * 2)
        {
            baud_rate.Items.Add(i);
        }
        serial_port_name.DropDownStyle = ComboBoxStyle.DropDownList;
        baud_rate.SelectedIndex = 3;
        baud_rate.DropDownStyle = ComboBoxStyle.DropDownList;

    }
    private void serial_port_name_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (serial_port_name.Text != "Select port")
        {
            configure_button.Enabled = true;
        }
    }
    private void configure_button_Click(object sender, EventArgs e)
    {
        serialPort1.PortName = serial_port_name.Text;
        serialPort1.BaudRate = Int32.Parse(baud_rate.Text);
        serialPort1.DataBits = 8;
        serialPort1.StopBits = StopBits.One;
        //serialPort1.Handshake = Handshake.None;
        serialPort1.ReceivedBytesThreshold = 44;
        serialPort1.ReadBufferSize = 8192;
        MessageBox.Show("Communication is set on " + serialPort1.PortName + " with baud rate: " + serialPort1.BaudRate);
        start_button.Enabled = true;
    }
    private void start_button_Click(object sender, EventArgs e)
    {
        if (serialPort1.IsOpen)
        {
            serialPort1.Close();
            start_button.Text = "START";
            read_button.Enabled = false;
            //read_write.Enabled = false;
            comport_status.Text = "DISCONNECTED";
        }
        else
        {
            serialPort1.Open();
            start_button.Text = "STOP";
            read_button.Enabled = true;
            //read_write.Enabled = true;
            comport_status.Text = "CONNECTED";
        }
    }
    private void quitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Application.Exit();
    }
    private void read_button_Click(object sender, EventArgs e)
    {
        bytes_to_read.Text = serialPort1.BytesToRead.ToString();
        read_data.Text = serialPort1.ReadExisting().ToString();
        read_data.Text += k.ToString() + "   ";
        if (buffer_old[0] == 4)
        {
            serialPort1.ReadByte();
        }
        serialPort1.Read(buffer, 0, 11);
        for (int j = 0; j < 11; j++)
        {
            read_data.Text += buffer[j].ToString() + "  ";
        }
        read_data.Text += "'n";
        k++;
        buffer_old = buffer;
    }
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        serialPort1 = (SerialPort)sender;
        number_of_bytes = serialPort1.BytesToRead.ToString();
        if (buffer_old[0] == 4)
        {
            serialPort1.ReadByte();
        }
        serialPort1.Read(buffer, 0, 11);
        buffer_old = buffer;
        this.Invoke(new EventHandler(displaytext));
        Thread.Sleep(20);
    }
    private void displaytext(object sender, EventArgs e)
    {
        checksum = 0;
        bytes_to_read.Text = number_of_bytes;
        read_data.Text += k.ToString() + "'t";
        for (int j = 0; j < 11; j++)
        {
            read_data.Text += buffer[j].ToString() + "  ";
            checksum += buffer[j]; 
        }
        checksum -= buffer[10];
        read_data.Text += "  't" + checksum.ToString();
        read_data.Text += "'n";
        k++;
    }
}

}

请看:如果必须使用 .NET System.IO.Ports.SerialPort

Ben Voigt(作者)给出了一个关于如何使用SerialPort固有BaseStream成员的很好的例子,这对于使用底层Win32 API更有效。

Port = new SerialPort("COM" + PORT_NUMBER.ToString()); 
...
private void ReadEvent()  
{      
    byte[] buffer = new byte[2000];  
    Action kickoffRead = null;  
    kickoffRead = (Action)(() => Port.BaseStream.BeginRead(buffer, 0, buffer.Length, delegate(IAsyncResult ar)  
    {  
        try  
        {  
            int count = Port.BaseStream.EndRead(ar);  
            byte[] dst = new byte[count];  
            Buffer.BlockCopy(buffer, 0, dst, 0, count);  
            RaiseAppSerialDataEvent(dst);  
        }  
        catch (Exception exception)  
        {  
            MessageBox.Show(ERROR == > " + exception.ToString());    
        }  
        kickoffRead();  
    }, null)); kickoffRead();  
}

问候。