C#桌面应用程序--串口数据的发送

继续学习C#的桌面应用程序。

这次根据视频学习,简单的做了一下串口发送的小应用程序,能够根据设定的串口号发送选择好的一个字节数据,可选范围为256个。

首先是应用程序的界面,


另外添加了一个串口的控件,


串口控件的属性中,可以设置波特率、数据位、串口号等相关的设定。而且是,十分必要的!

然后是程序代码,如下,

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;

namespace 串口1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i;
            string str;                 //用于存储i转换为的16进制字符
            for (i = 0; i < 256; i++)   //共计256个数据
            {
                str = i.ToString("x").ToUpper();        //i转换为16进制,并且转换为大写
                if (str.Length == 1)            //如果转换成的字符,只有1位
                {
                    str = "0" + str;            //那么,在str前边补0
                }
                comboBox1.Items.Add("0x"+str);  //统一在前边添加0x
            }
            comboBox1.Text = "0x00";            //初始值显示0x00
        }

        private void button1_Click(object sender, EventArgs e)  //按键单击事件
        {
            string str = comboBox1.Text;        //存储按下时的值
            string convertdata = str.Substring(2,2);    //把字符分开,存储从第2位开始的两位数据
            byte[] buffer = new byte[1];        //新建byte位,存储一个字节的数据
            buffer[0]= Convert.ToByte(convertdata,16);  //把字符串转化为byte型变量(16进制)(相当于单片机中的unsigned int)
            try      //出错检测
            {
                serialPort1.Open();         //打开串口
                serialPort1.Write(buffer, 0, 1);//写入数据
                serialPort1.Close();        //关闭串口
            }
            catch
            {
                if (serialPort1.IsOpen)      //如果串口是打开状态
                    serialPort1.Close();     //就关闭串口
                MessageBox.Show("串口错误","错误");
            }
        }
    }
}
程序功能:下拉框选择要发送的1位数据,点击按键,就会按照指定的波特率往指定串口发送。



public partial class Form1 : Form { public Form1() { InitializeComponent(); } SerialPort port1 = new SerialPort(); string InputData = String.Empty; delegate void SetTextCallback(string text); private void Port_Select() {//获取机器中的串口地址 string[] ports = SerialPort.GetPortNames(); foreach (string port in ports) { comboBox1.Items.Add(port); } } private void Form1_Load_1(object sender, EventArgs e) { Port_Select(); this.comboBox1.SelectedIndex = 0; this.comboBox2.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "关闭串口") //当要关闭串口的时候 { port1.DiscardOutBuffer(); port1.DiscardInBuffer(); port1.Close(); button1.Text = "打开串口"; label3.Text = "串口当前状况:未打开"; comboBox1.Enabled = true; comboBox2.Enabled = true; } else if (button1.Text == "打开串口") //当要打开串口的时候 { try { port1.PortName = comboBox1.SelectedItem.ToString(); port1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem); port1.DataBits = 8; port1.RtsEnable = true; port1.Open(); port1.DiscardOutBuffer(); port1.DiscardInBuffer(); button1.Text = "关闭串口"; comboBox1.Enabled = false; comboBox2.Enabled = false; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + " 波特率:" + comboBox2.SelectedItem.ToString() + " 数据位:8 "; } catch { button1.Text = "打开串口"; label3.Text = "串口:" + comboBox1.SelectedItem.ToString() + "打开失败"; MessageBox.Show("该串口无法打开"); } } } 资源中部分代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值