基本信息
源码名称:c# 简易版串口助手源码
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2017-07-25
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
简易版的串口助手,可以收发数据,汉字,还有十六进制接。
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;//忽略程序跨越线程运行导致的错误
}
//给端口号列表赋值
private void Form1_Load(object sender, EventArgs e)
{
//检查是否含有串口
string[] str = SerialPort.GetPortNames();
if (str == null)
{
MessageBox.Show("本机没有串口!", "Error");
return;
}
else
{
//添加串口项目(自动检索电脑可用的串口)
foreach (string outcom in SerialPort.GetPortNames())
{
//获取有多少个COM口
comboBox1.Items.Add(outcom);
}
comboBox1.Text = str[0];
}
}
//接收事件
private void post_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (checkBox1 .Checked == true)//十六进制方式读取数据
{
byte data;
data = (byte)serialPort1.ReadByte();
string str = Convert.ToString(data, 16).ToUpper();
textBox_receive.AppendText("0x" (str.Length == 1 ? "0" str : str) " ");
}
else//字符串方式读数据
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] readBytes = new byte[serialPort1.BytesToRead];
serialPort1.Read(readBytes, 0, readBytes.Length);
string decodedString = utf8.GetString(readBytes);
textBox_receive.AppendText(decodedString);
}
}
//当按下打开串口按钮时
private void button_open_Click(object sender, EventArgs e)
{
//检查是否含有串口
if (comboBox1.Text=="")
{
MessageBox.Show("本机没有可用串口!", "Error");
return;
}
else
{
if (serialPort1.IsOpen == false)//如果串口是关的
{
serialPort1.PortName = comboBox1.Text;//将选中的端口号赋给串口
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);//波特率转换并赋值
float f = Convert.ToSingle(comboBox3.Text.Trim());//停止位
if (f == 0)//设置停止位
serialPort1.StopBits = StopBits.None;
else if (f == 1.5)
serialPort1.StopBits = StopBits.OnePointFive;
else if (f == 1)
serialPort1.StopBits = StopBits.One;
else if (f == 2)
serialPort1.StopBits = StopBits.Two;
else
serialPort1.StopBits = StopBits.One;
//设置数据位
serialPort1.DataBits = Convert.ToInt32(comboBox4.Text);
//设置奇偶校验位
string s = comboBox5.Text;
if (s.CompareTo("无") == 0)
serialPort1.Parity = Parity.None;
else if (s.CompareTo("奇校验") == 0)
serialPort1.Parity = Parity.Odd;
else if (s.CompareTo("偶校验") == 0)
serialPort1.Parity = Parity.Even;
else
serialPort1.Parity = Parity.None;
try
{
serialPort1.Open();
button_open.Text = "关闭串口";
comboBox1.Enabled = false;//使能配置
comboBox2.Enabled = false;
comboBox3.Enabled = false;
comboBox4.Enabled = false;
comboBox5.Enabled = false;
}
catch
{
MessageBox.Show("串口打开错误", "消息提示框");
}
}
else //如果串口是关闭的
{
serialPort1.Close();
button_open.Text = "打开串口";
comboBox1.Enabled = true;//使能配置
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
}
}
}
//当send按下时
private void button_send_Click(object sender, EventArgs e)
{
//发送数据
if (serialPort1.IsOpen)//如果串口开启
{
if (textBox_send.Text.Trim() != "")//如果框内不为空则
{
//UnicodeEncoding ue = new UnicodeEncoding();
UTF8Encoding utf8 = new UTF8Encoding();
byte[] writeBytes = utf8.GetBytes(textBox_send.Text);
serialPort1.Write(writeBytes, 0, writeBytes.Length);
}
else
{
MessageBox.Show("发送框没有数据", "提示框");
}
}
else
{
MessageBox.Show("串口未打开", "提示框");
}
}
//清空接收区数据
private void button2_Click(object sender, EventArgs e)
{
textBox_receive.Clear();//清空接收框
}
//清空发送区数据
private void button3_Click(object sender, EventArgs e)
{
textBox_send.Clear();//清空发送框
}
简易版的串口助手,可以收发数据,汉字,还有十六进制接。
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;//忽略程序跨越线程运行导致的错误
}
//给端口号列表赋值
private void Form1_Load(object sender, EventArgs e)
{
//检查是否含有串口
string[] str = SerialPort.GetPortNames();
if (str == null)
{
MessageBox.Show("本机没有串口!", "Error");
return;
}
else
{
//添加串口项目(自动检索电脑可用的串口)
foreach (string outcom in SerialPort.GetPortNames())
{
//获取有多少个COM口
comboBox1.Items.Add(outcom);
}
comboBox1.Text = str[0];
}
}
//接收事件
private void post_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (checkBox1 .Checked == true)//十六进制方式读取数据
{
byte data;
data = (byte)serialPort1.ReadByte();
string str = Convert.ToString(data, 16).ToUpper();
textBox_receive.AppendText("0x" (str.Length == 1 ? "0" str : str) " ");
}
else//字符串方式读数据
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] readBytes = new byte[serialPort1.BytesToRead];
serialPort1.Read(readBytes, 0, readBytes.Length);
string decodedString = utf8.GetString(readBytes);
textBox_receive.AppendText(decodedString);
}
}
//当按下打开串口按钮时
private void button_open_Click(object sender, EventArgs e)
{
//检查是否含有串口
if (comboBox1.Text=="")
{
MessageBox.Show("本机没有可用串口!", "Error");
return;
}
else
{
if (serialPort1.IsOpen == false)//如果串口是关的
{
serialPort1.PortName = comboBox1.Text;//将选中的端口号赋给串口
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);//波特率转换并赋值
float f = Convert.ToSingle(comboBox3.Text.Trim());//停止位
if (f == 0)//设置停止位
serialPort1.StopBits = StopBits.None;
else if (f == 1.5)
serialPort1.StopBits = StopBits.OnePointFive;
else if (f == 1)
serialPort1.StopBits = StopBits.One;
else if (f == 2)
serialPort1.StopBits = StopBits.Two;
else
serialPort1.StopBits = StopBits.One;
//设置数据位
serialPort1.DataBits = Convert.ToInt32(comboBox4.Text);
//设置奇偶校验位
string s = comboBox5.Text;
if (s.CompareTo("无") == 0)
serialPort1.Parity = Parity.None;
else if (s.CompareTo("奇校验") == 0)
serialPort1.Parity = Parity.Odd;
else if (s.CompareTo("偶校验") == 0)
serialPort1.Parity = Parity.Even;
else
serialPort1.Parity = Parity.None;
try
{
serialPort1.Open();
button_open.Text = "关闭串口";
comboBox1.Enabled = false;//使能配置
comboBox2.Enabled = false;
comboBox3.Enabled = false;
comboBox4.Enabled = false;
comboBox5.Enabled = false;
}
catch
{
MessageBox.Show("串口打开错误", "消息提示框");
}
}
else //如果串口是关闭的
{
serialPort1.Close();
button_open.Text = "打开串口";
comboBox1.Enabled = true;//使能配置
comboBox2.Enabled = true;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
comboBox5.Enabled = true;
}
}
}
//当send按下时
private void button_send_Click(object sender, EventArgs e)
{
//发送数据
if (serialPort1.IsOpen)//如果串口开启
{
if (textBox_send.Text.Trim() != "")//如果框内不为空则
{
//UnicodeEncoding ue = new UnicodeEncoding();
UTF8Encoding utf8 = new UTF8Encoding();
byte[] writeBytes = utf8.GetBytes(textBox_send.Text);
serialPort1.Write(writeBytes, 0, writeBytes.Length);
}
else
{
MessageBox.Show("发送框没有数据", "提示框");
}
}
else
{
MessageBox.Show("串口未打开", "提示框");
}
}
//清空接收区数据
private void button2_Click(object sender, EventArgs e)
{
textBox_receive.Clear();//清空接收框
}
//清空发送区数据
private void button3_Click(object sender, EventArgs e)
{
textBox_send.Clear();//清空发送框
}