[英]Visual Studio serial communications
IMAGEEEEEEE I'm using Visual Studio c# forms and I can't get the label to display everything the serial port reads, is there an easy way to fix this? 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();
}
}
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.