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 串口通讯练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button_打开串口1_Click(object sender, EventArgs e)
{
//open_sp1();
if (!sp1.IsOpen)
{
try
{
sp1.PortName = textBox1.Text;
sp1.Open();
button_打开串口1.Text = "关闭串口1";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
//tmSend.Enabled = false;
return;
}
}
else
{
sp1.Close();
button_打开串口1.Text = "打开串口1";
}
}
void open_sp1()
{
//serialPort1.IsOpen
if (!sp1.IsOpen)
{
try
{
//设置串口号
//string serialName = "com10";
string serialName = textBox1.Text;
sp1.PortName = serialName;
//设置各“串口设置”
//string strBaudRate = cbBaudRate_1.Text;
//string strDateBits = cbDataBits_1.Text;
//string strStopBits = cbStop_1.Text;
//Int32 iBaudRate = Convert.ToInt32("9600");
//Int32 iDateBits = Convert.ToInt32("8");
//sp1.BaudRate = iBaudRate; //波特率
//sp1.DataBits = iDateBits; //数据位
//switch (cbStop_1.Text) //停止位
//{
// case "1":
// sp1.StopBits = StopBits.One;
// break;
// case "1.5":
// sp1.StopBits = StopBits.OnePointFive;
// break;
// case "2":
// sp1.StopBits = StopBits.Two;
// break;
// default:
// MessageBox.Show("Error:参数不正确!", "Error");
// break;
//}
//switch (cbParity_1.Text) //校验位
//{
// case "无":
// sp1.Parity = Parity.None;
// break;
// case "奇校验":
// sp1.Parity = Parity.Odd;
// break;
// case "偶校验":
// sp1.Parity = Parity.Even;
// break;
// default:
// MessageBox.Show("Error:参数不正确!", "Error");
// break;
//}
if (sp1.IsOpen == true)//如果打开状态,则先关闭一下
{
sp1.Close();
}
//设置必要控件不可用
//cbSerial_1.Enabled = false;
//cbBaudRate_1.Enabled = false;
//cbDataBits_1.Enabled = false;
//cbStop_1.Enabled = false;
//cbParity_1.Enabled = false;
sp1.Open(); //打开串口
//timer1.Enabled = true;
button_打开串口1.Text = "关闭串口";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
//tmSend.Enabled = false;
return;
}
//gl_发送字节数 = 0;
//gl_接收字节数 = 0;
//sta_发送.Text = "发送: 0";
//sta_接收.Text = "接收: 0";
}
else
{
//恢复控件功能
//设置必要控件不可用
//cbSerial_1.Enabled = true;
//cbBaudRate_1.Enabled = true;
//cbDataBits_1.Enabled = true;
//cbStop_1.Enabled = true;
//cbParity_1.Enabled = true;
//timer1.Enabled = false;
sp1.Close(); //关闭串口
button_打开串口1.Text = "打开串口";
//tmSend.Enabled = false; //关闭计时器
}
}
private void button_发送1_Click(object sender, EventArgs e)
{
sp1.Write(textBox_发送1.Text);
}
private void textBox_发送1_TextChanged(object sender, EventArgs e)
{
}
private void sp1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
textBox_发送1.Text = sp1.ReadExisting();
}
private void button_打开串口2_Click(object sender, EventArgs e)
{
//open_sp2();
if (!sp2.IsOpen)
{
try
{
sp2.PortName = textBox2.Text;
sp2.Open();
button_打开串口2.Text = "关闭串口1";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
//tmSend.Enabled = false;
return;
}
}
else
{
sp2.Close();
button_打开串口2.Text = "打开串口1";
}
}
private void button_发送2_Click(object sender, EventArgs e)
{
sp2.Write(textBox_发送2.Text);
}
private void textBox_发送2_TextChanged(object sender, EventArgs e)
{
}
private void sp2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
String str = sp2.ReadExisting();
textBox_发送2.AppendText(str);
}
}
}
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 串口通讯练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
private void button_打开串口1_Click(object sender, EventArgs e)
{
//open_sp1();
if (!sp1.IsOpen)
{
try
{
sp1.PortName = textBox1.Text;
sp1.Open();
button_打开串口1.Text = "关闭串口1";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
//tmSend.Enabled = false;
return;
}
}
else
{
sp1.Close();
button_打开串口1.Text = "打开串口1";
}
}
void open_sp1()
{
//serialPort1.IsOpen
if (!sp1.IsOpen)
{
try
{
//设置串口号
//string serialName = "com10";
string serialName = textBox1.Text;
sp1.PortName = serialName;
//设置各“串口设置”
//string strBaudRate = cbBaudRate_1.Text;
//string strDateBits = cbDataBits_1.Text;
//string strStopBits = cbStop_1.Text;
//Int32 iBaudRate = Convert.ToInt32("9600");
//Int32 iDateBits = Convert.ToInt32("8");
//sp1.BaudRate = iBaudRate; //波特率
//sp1.DataBits = iDateBits; //数据位
//switch (cbStop_1.Text) //停止位
//{
// case "1":
// sp1.StopBits = StopBits.One;
// break;
// case "1.5":
// sp1.StopBits = StopBits.OnePointFive;
// break;
// case "2":
// sp1.StopBits = StopBits.Two;
// break;
// default:
// MessageBox.Show("Error:参数不正确!", "Error");
// break;
//}
//switch (cbParity_1.Text) //校验位
//{
// case "无":
// sp1.Parity = Parity.None;
// break;
// case "奇校验":
// sp1.Parity = Parity.Odd;
// break;
// case "偶校验":
// sp1.Parity = Parity.Even;
// break;
// default:
// MessageBox.Show("Error:参数不正确!", "Error");
// break;
//}
if (sp1.IsOpen == true)//如果打开状态,则先关闭一下
{
sp1.Close();
}
//设置必要控件不可用
//cbSerial_1.Enabled = false;
//cbBaudRate_1.Enabled = false;
//cbDataBits_1.Enabled = false;
//cbStop_1.Enabled = false;
//cbParity_1.Enabled = false;
sp1.Open(); //打开串口
//timer1.Enabled = true;
button_打开串口1.Text = "关闭串口";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
//tmSend.Enabled = false;
return;
}
//gl_发送字节数 = 0;
//gl_接收字节数 = 0;
//sta_发送.Text = "发送: 0";
//sta_接收.Text = "接收: 0";
}
else
{
//恢复控件功能
//设置必要控件不可用
//cbSerial_1.Enabled = true;
//cbBaudRate_1.Enabled = true;
//cbDataBits_1.Enabled = true;
//cbStop_1.Enabled = true;
//cbParity_1.Enabled = true;
//timer1.Enabled = false;
sp1.Close(); //关闭串口
button_打开串口1.Text = "打开串口";
//tmSend.Enabled = false; //关闭计时器
}
}
private void button_发送1_Click(object sender, EventArgs e)
{
sp1.Write(textBox_发送1.Text);
}
private void textBox_发送1_TextChanged(object sender, EventArgs e)
{
}
private void sp1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
textBox_发送1.Text = sp1.ReadExisting();
}
private void button_打开串口2_Click(object sender, EventArgs e)
{
//open_sp2();
if (!sp2.IsOpen)
{
try
{
sp2.PortName = textBox2.Text;
sp2.Open();
button_打开串口2.Text = "关闭串口1";
}
catch (System.Exception ex)
{
MessageBox.Show("Error:" + ex.Message, "Error");
//tmSend.Enabled = false;
return;
}
}
else
{
sp2.Close();
button_打开串口2.Text = "打开串口1";
}
}
private void button_发送2_Click(object sender, EventArgs e)
{
sp2.Write(textBox_发送2.Text);
}
private void textBox_发送2_TextChanged(object sender, EventArgs e)
{
}
private void sp2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
String str = sp2.ReadExisting();
textBox_发送2.AppendText(str);
}
}
}