[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;
using System.IO.Ports;
namespace 环境测量系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
string str = serialPort1.ReadExisting();
label3.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "打开端口")
{
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
comboBox1.Enabled = false;
comboBox2.Enabled = false;
button1.BackColor = Color.Red;
button1.Text = "关闭端口";
}
catch
{
MessageBox.Show("端口打开失败!请检查!");
}
}
else
{
try
{
serialPort1.Close();
comboBox1.Enabled = true;
comboBox2.Enabled = true;
button1.BackColor = Color.Lime;
button1.Text = "打开端口";
}
catch
{
MessageBox.Show("端口打开失败!");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
serialPort1.Close();
this.Close();
}
private void button4_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen)
{
byte [] data = new byte [1];
data[0] = 01;
serialPort1.Write(data, 0, 1);
int length = serialPort1.BytesToRead;
byte[] getdata = new byte[length];
serialPort1.Read(getdata, 0, length);
//byte[] value = new byte[length];
//serialPort1.Read(value,0,length);
//string str = Convert.ToString(value[length], 16).ToUpper();
}
}
private void button2_Click(object sender, EventArgs e)
{
label10.Text = "校准中!请稍后!";
}
}
}
|