using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace cj1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if(this.button1.Text == "打开串口")
{
this.serialPort1.PortName = comboBox1.Text;
try
{
this.serialPort1.Open();
if (this.serialPort1.IsOpen)
{
this.button1.Text = "关闭串口";
}
}
catch( Exception ee)
{
MessageBox.Show("串口打开失败!");
}
}
else
{
this.serialPort1.Close();
if (!this.serialPort1.IsOpen)
{
this.button1.Text = "打开串口";
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (!this.serialPort1.IsOpen)
{
MessageBox.Show("串口未打开!");
return;
}
if(this.textBox2.Text == "")
{
MessageBox.Show("数据为空!");
return;
}
this.serialPort1.Write(this.textBox2.Text);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int len = this.serialPort1.ReceivedBytesThreshold;
Byte[] buffer = new Byte[5000];
this.serialPort1.Read(buffer, 0, 5000-1);
this.textBox3.Text += Encoding.Default.GetString(buffer);
//MessageBox.Show("收到1次");
// string response = Encoding.ASCII.GetString(ss_port.Read(128));
}
}
}