C#读取串口数据,怎么得不到数据?急!!在线等!!!
namespaceSerial{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}pri...
namespace Serial
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.IO.Ports.SerialPort myserialPort = new System.IO.Ports.SerialPort();
string GPS_text = "aaaa";
private void myserialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
GPS_text = GPS_text + myserialPort.ReadExisting(); // 读取串口数据
label2.Text = GPS_text;
Invoke(new EventHandler(update_data)); // 通过Invoke方法执行update_data函数
}
private void update_data(object sender, EventArgs e)
{
string[] GPS_info = GPS_text.Split(','); // 按照逗号分隔把$GPRMC各种信号分割到字符串数组
GPS_text = "aa";
label2.Text = GPS_text;// 置空GPS_text以便存储新的串口接收到的字符串
}
private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
myserialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(myserialPort_DataReceived);
}
}
} 展开
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private System.IO.Ports.SerialPort myserialPort = new System.IO.Ports.SerialPort();
string GPS_text = "aaaa";
private void myserialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
GPS_text = GPS_text + myserialPort.ReadExisting(); // 读取串口数据
label2.Text = GPS_text;
Invoke(new EventHandler(update_data)); // 通过Invoke方法执行update_data函数
}
private void update_data(object sender, EventArgs e)
{
string[] GPS_info = GPS_text.Split(','); // 按照逗号分隔把$GPRMC各种信号分割到字符串数组
GPS_text = "aa";
label2.Text = GPS_text;// 置空GPS_text以便存储新的串口接收到的字符串
}
private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();
}
myserialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(myserialPort_DataReceived);
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询