串口通信中,解决serialport.DataReceived接收到的数据不完整的问题

关键是需要解决这句话 byte[] data = new byte[serialPort1.BytesToRead];
问题在于串口返回数据时,由于程序执行的速度是非常快的,而串口通信返回的速度可能不会太快。
也就是说,当执行到Read语句时,串口根本没来得及返回那么多的数据,只返回了几个字节,即便你指定bytesToRead大小也没有用。
解决办法:只能通过控制代码来实现你想要的功能:
一、设置一个大小为serialPort1.BytesToRead的缓存,写一个while循环,只有从串口读满serialPort1.BytesToRead个字节的数据时才退出while循环,否则继续读取……
byte[] data = new byte[serialPort1.BytesToRead];
do
{
data = new byte[serialPort1.BytesToRead];
} while (data.Length != 5);//5是已知设置好的通信数据的长度
serialPort1.Read(data, 0, data.Length);

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Imports System Imports System.IO.Ports Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '获取计算机有效串口 Dim ports As String() = SerialPort.GetPortNames() Dim port As String For Each port In ports ComboBox1.Items.Add(port) Next ComboBox1.Text = ComboBox1.Items(0) ComboBox2.Items.Add(1200) ComboBox2.Items.Add(4800) ComboBox2.Items.Add(9600) ComboBox2.Text = ComboBox2.Items(2) Serial_Port1() TextBox2.Text = SerialPort1.IsOpen TextBox1.Text = "串口未连接" TextBox1.ForeColor = Color.Red TextBox3.Text = "123" CheckBox1.Text = "" End Sub Private Sub Serial_Port1() SerialPort1.BaudRate = ComboBox2.Text SerialPort1.PortName = ComboBox1.Text SerialPort1.DataBits = 8 SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.Parity = IO.Ports.Parity.None End Sub '关闭串口连接 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try SerialPort1.Close() TextBox2.Text = SerialPort1.IsOpen If SerialPort1.IsOpen = False Then TextBox1.Text = "串口未连接" TextBox1.ForeColor = Color.Red TextBox4.Text = "" End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try SerialPort1.Open() TextBox2.Text = SerialPort1.IsOpen If SerialPort1.IsOpen = True Then TextBox1.Text = "串口已连接" TextBox1.ForeColor = Color.Green End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Try SerialPort1.Write(TextBox3.Text) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived Me.Invoke(New EventHandler(AddressOf Sp_DataReceiving)) End Sub Public Sub Sp_DataReceiving(ByVal sender As Object, ByVal e As EventArgs) Dim strIncoming As String Try CheckBox1.Text = Str(Val(CheckBox1.Text) + SerialPort1.BytesToRead) If SerialPort1.BytesToRead > 0 Then Threading.Thread.Sleep(100) strIncoming = SerialPort1.ReadExisting.ToString SerialPort1.DiscardInBuffer() TextBox4.Text = strIncoming End If Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub End Class
SerialPort 是 C# 用于串口通信的类。DataReceivedSerialPort的一个事件,用于在接收数据时触发相应的操作。 当 SerialPort 对象接收数据时,DataReceived 事件会自动触发。在事件处理程序,可以获取接收到的数据并进行相应的处理。例如,可以将数据显示在界面上,或者进行数据解析等操作。 以下是 DataReceived 事件的基本用法: 1. 创建 SerialPort 对象并设置相应的属性(如串口号、波特率等)。 2. 通过 DataReceived 事件添加事件处理程序。 3. 在事件处理程序,使用 SerialPort 对象的 Read 方法获取接收到的数据,并进行相应的处理。 以下是一个简单的示例代码: ``` private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { // 获取 SerialPort 对象 SerialPort sp = (SerialPort)sender; // 读取接收到的数据 string data = sp.ReadExisting(); // 在界面上显示数据 textBox1.Invoke(new Action(() => { textBox1.AppendText(data); })); } ``` 在该示例,当 SerialPort 对象接收数据时,会触发 DataReceived 事件,该事件处理程序会将接收到的数据显示在一个 TextBox 控件。 需要注意的是,在使用 DataReceived 事件时,可能会出现数据接收完整或者丢失的情况。为了避免这种情况,建议使用 SerialPort的 ReadLine 或者 ReadExisting 方法来读取数据,并根据具体情况进行相应的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

环瑀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值