我是这个论坛的新手,我在C#编程方面没有太多经验。我用C#构建了一个简单的Windows应用程序来与我朋友给我的一些电子板进行通信。他在38400的微控制器中定义了串口波特率。我想尽快通过串口发送和接收字节。使用C#中的现有serialport工具,我只能读取字节,但即使在自己的线程中使用eventhandler,仍然不够快。我的缓冲区[4096]在几秒钟内就满了。
我的问题是,是否存在一些用于串行端口通信的其他功能或工具,这些功能或工具不会占用太多处理器时间。我需要读取少于200 us的一个字节。我对嵌入式系统有更多经验,这不是问题。
感谢大家提出一些建议或想法。
托马日
答案 0 :(得分:0)
感谢所有回复。我可以读取来自电路板的所有字节,但我无法快速回复。这是我的代码。我使用了一个事件来读取缓冲区。如果我尝试在事件中回复,则下一个字节包含错误的值。
#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++;
}
}
}
答案 1 :(得分:0)
请查看: If you must use .NET System.IO.Ports.SerialPort
Ben Voigt(作者)提供了一个很好的示例,说明了如何使用BaseStream
的固有SerialPort
成员,这对于使用基础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();
}
致谢。