IMAGEEEEEEE我正在使用Visual Studio c#表单,我无法使用标签显示串口读取的所有内容,是否有一种简单的方法可以解决此问题?
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;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.Open();
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
Label1.Text = serialPort1.ReadLine();
}
}
}
答案 0 :(得分:0)
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
var dataLength = serialPort1.BytesToRead;
var data = new byte[dataLength];
var nbrDataRead = serialPort1.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
Label1.Text = System.Text.Encoding.UTF8.GetString(data, 0, data.Length);
}