目录
本节示范使用C#实现一个上位机工具,来实现USB/BT通讯工具,实现如串口通讯等功能。
一、【程序实现】
步骤1、先设计窗口,把各种需要的控制布局在窗口内
步骤2、制定应用的功能方案
(1)、首先,我们需要按下按键,开始计时,进度条与时间同步,
(2)、按下按键2停止计时
步骤3、选择相应控件
(1)、comboBox1控件(下拉框):属性:DropDownList
(2)、容器:panel1控件:将单选放在一起
步骤4、制定后台逻辑
具体程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports; /非常重要///
namespace serial_communication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)//打开串口按钮
{
try
{
serialPort1.PortName = comboBox1.Text;//串口名直接赋值comboBox1的文本Text
serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);//波特率,comboBox2控件的Text字符串转换十进制数据转换
serialPort1.Open();
button1.Enabled = false;//”打开串口“按钮就不可用了
button2.Enabled = true;//“关闭按钮”可以
}
catch
{
MessageBox.Show("端口错误,请检查串口", "错误");
}
}
private void button2_Click(object sender, EventArgs e)//关闭串口按钮
{
try
{
serialPort1.Close();//关闭串口
button1.Enabled = true;//”打开串口“按钮可用
button2.Enabled = false;//“关闭串口”按钮不可用
}
catch (Exception err)//一般情况下关闭串口不会出错,所以不需要添加处理程序
{
}
}
private void Form1_Load(object sender, EventArgs e) //窗口创建初始化过程
{
for (int i = 1; i < 20; i++)
{
comboBox1.Items.Add("COM" + i.ToString());//下拉框创建串口号,可选串口1到19(i = 1; i < 20)
}
comboBox1.Text = "COM";//初始值
comboBox2.Text = "4800";//初始值
/非常重要///手动添加/
//串口接收事件(必须手动添加事件处理)
serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
//这个函数是控件生成不了的,在声明(using System.IO.Ports; )之后,手动写入这个函数
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件
{
//字符接收模式
if (!radioButton3.Checked) //如果接收模式为字符模式
//判断单选框选的是什么,先判断”接收模式“的数值Button3
//.Checked返回true、false
//!就是3不被选,也就是4被选,字符模式
{
string str = serialPort1.ReadExisting();//字符串方式读
textBox1.AppendText(str);//接收框textBox1添加内容,.AppendText:在textBox1控件的Text添加内容str(往后排)
}
//数据接收模式
else //如果接收模式为数值接收
{
byte data;//定义一个数据
data = (byte)serialPort1.ReadByte();
//.ReadByte()专门函数:串口读这个位,返回是一个整型的数
//前面加(byte)进行强制转换
//此处需要强制类型转换,将(int)整型数据转换为(byte)字符型数据
//转换在接收框的内容
string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串
textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");//得到的16进制数,空位补“0”如果是0xA则显示0x0A
//上一句等同为:if(str.length==1)
// str="0"+str;
// else
// str=str;
// textbox1.appendtext("0x"+str)
}
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
}
//发送按钮
private void button3_Click(object sender, EventArgs e)
{
byte[] Data = new byte[1];//只存储一个字节
if (serialPort1.IsOpen)//判断串口是否打开,如果打开就执行下一步
{
if(textBox2.Text!=" ")//如果发送文本是空的,也不执行
{
//发送模式是字符模式
if (!radioButton1.Checked)//如果发送模式是字符模式
{
try
{
serialPort1.WriteLine(textBox2.Text);//写数据
}
catch (Exception err)
{
MessageBox.Show("串口数据写入错误", "错误");//出错提示
serialPort1.Close();//关闭串口
button1.Enabled = true;//”打开串口“按钮可用
button2.Enabled = false;//“关闭串口”按钮不可用
}
}
//发送模式是数据模式
else
{
//处理偶数个字符
for (int i = 0; i < (textBox2.Text.Length - textBox2.Text.Length % 2) / 2; i++)
//取余,运算作用是防止
{
Data[0] = Convert.ToByte(textBox2.Text.Substring(i * 2, 2), 16);
serialPort1.Write(Data, 0, 1);//循环发送(如果输入字符是0A0BB,则发送0A,0B)
}
//处理奇数个字符
if (textBox2.Text.Length % 2 != 0)//奇数位(% 2 != 0)剩下一位单独处理
{
Data[0] = Convert.ToByte(textBox2.Text.Substring(textBox2.Text.Length - 1,1), 16);//单独发送B
serialPort1.Write(Data, 0, 1);//串口发送函数
}
}
}
}
}
}
}
二、【运行效果】
在Visual Studio中按下F5运行该程序,运行正常!
版权声明:本文为m0_72676510原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。