C#串口通信中COM口的缓存为4096,假设1个2MB的数据通过此COM口,怎么样实现全部读取,而没有遗漏或错误?
这是我的实现代码,发现数据一多(大概>5000字节)就只能读取前面数据,后面的数据就无法读取,最后发现是com缓存过小的问题,怎么改进?下面是部分代码:publicpar...
这是我的实现代码,发现数据一多(大概>5000字节)就只能读取前面数据,后面的数据就无法读取,最后发现是com缓存过小的问题,怎么改进?
下面是部分代码:
public partial class SerialportSampleForm : Form
{
private SerialPort comm = new SerialPort();
private StringBuilder builder = new StringBuilder();
private long received_count = 0;
private long send_count = 0;
public SerialportSampleForm()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
Array.Sort(ports);
comboPortName.Items.AddRange(ports);
comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
comboBaudrate.SelectedIndex = comboBaudrate.Items.IndexOf("9600");
comm.Parity = Parity.None;
comm.StopBits = StopBits.One;
comm.DataBits = 8;
comm.Handshake = Handshake.None;
comm.NewLine = "\r\n";
comm.RtsEnable = true;
comm.DataReceived += new SerialDataReceivedEventHandler(comm_DataReceived);
}
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = comm.BytesToRead;
byte[] buf = new byte[n];
received_count += n;
comm.Read(buf, 0, n);
builder.Clear();
this.Invoke((EventHandler)(delegate
{
if (checkBoxHexView.Checked)
{
foreach (byte b in buf)
{
builder.Append(b.ToString("X2") + " ");
}
}
else
{
builder.Append(Encoding.ASCII.GetString(buf));
}
this.txGet.AppendText(builder.ToString());
labelGetCount.Text = "接收字节:" + received_count.ToString();
}));
} 展开
下面是部分代码:
public partial class SerialportSampleForm : Form
{
private SerialPort comm = new SerialPort();
private StringBuilder builder = new StringBuilder();
private long received_count = 0;
private long send_count = 0;
public SerialportSampleForm()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
Array.Sort(ports);
comboPortName.Items.AddRange(ports);
comboPortName.SelectedIndex = comboPortName.Items.Count > 0 ? 0 : -1;
comboBaudrate.SelectedIndex = comboBaudrate.Items.IndexOf("9600");
comm.Parity = Parity.None;
comm.StopBits = StopBits.One;
comm.DataBits = 8;
comm.Handshake = Handshake.None;
comm.NewLine = "\r\n";
comm.RtsEnable = true;
comm.DataReceived += new SerialDataReceivedEventHandler(comm_DataReceived);
}
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = comm.BytesToRead;
byte[] buf = new byte[n];
received_count += n;
comm.Read(buf, 0, n);
builder.Clear();
this.Invoke((EventHandler)(delegate
{
if (checkBoxHexView.Checked)
{
foreach (byte b in buf)
{
builder.Append(b.ToString("X2") + " ");
}
}
else
{
builder.Append(Encoding.ASCII.GetString(buf));
}
this.txGet.AppendText(builder.ToString());
labelGetCount.Text = "接收字节:" + received_count.ToString();
}));
} 展开
展开全部
发送数据大于缓冲区时,一般采用分片多次发送的方法,另外可以另外开辟一块缓冲区,将缓冲区数据读完之后立刻刷新等待后续数据,但是此种方法依然可能造成数据丢失。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在程序中创建缓冲区,串口只管接收,接收后保存在缓冲区中,操作缓冲区的数据就行了。
更多追问追答
追问
怎么创建呢?有没有参考资料贴个啊?谢谢了!
追答
///
/// 接收字节缓存(默认分配1页内存,并始终限制不允许超过)
///
private List bufferRecive = new List(4096);
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (isColsing) { return; } //如果正在停止数据采集 则return 快速的结束本次数据解析
bool data_1_catched = false;
Thread.Sleep(700);
int count = port.BytesToRead;
byte[] bytes = new byte[count];
binary_data = new byte[24];
port.Read(bytes, 0, count);//读取缓冲数据
bufferRecive.AddRange(bytes); //向缓存中添加当前接收到的数据
if (bufferRecive.Count = 240)//至少要包含一个完整的协议数据内容
{
//处理bufferRecive中的一个完整协议,处理后移除处理过后的字节。
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询