一个上位机程序(未全部完成)
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _上位机
{
public partial class Form1 : Form
{
string Rdata;
string channelA, channelB, channelC, channelD;
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
}
else
MessageBox.Show("串口已关闭");
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("COM1");
comboBox1.Items.Add("COM2");
comboBox1.Items.Add("COM3");
comboBox1.Items.Add("COM4");
comboBox1.Items.Add("COM5");
comboBox1.Items.Add("COM6");
comboBox1.Items.Add("COM7");
comboBox1.Items.Add("COM8");
comboBox1.Items.Add("COM9");
comboBox2.Items.Add("4800");
comboBox2.Items.Add("9600");
comboBox2.Items.Add("14400");
comboBox2.Items.Add("19200");
comboBox2.Items.Add("38400");
comboBox2.Items.Add("56000");
comboBox2.Items.Add("115200");
}
private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.PortName = Convert.ToString(comboBox1.SelectedItem);
serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem);
serialPort1.Open();
}
else
MessageBox.Show("串口已开启");
}
private void Receive_data(object sender, EventArgs e)
{
if (Rdata.Contains("[")&&Rdata.Contains("]"))
{
string Box=Rdata.Trim('[',']');
string[] box =Box.Split(new char[]{'-'},StringSplitOptions.RemoveEmptyEntries);
textBox1.Text = box[0];
textBox2.Text = box[1];
textBox3.Text = box[2];
textBox4.Text = box[3];
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen) serialPort1.Close();
}
private void serialPort1_DataReceived(object sender, e)
{
Rdata = serialPort1.ReadExisting();
this.Invoke(new EventHandler(Receive_data));
}
}
}
在textBox控件理不能显示数据,估计要用到多线程。