新手入门上位机开发 C#语言:PC串口发送数据

该博客介绍了如何在VS2017环境下进行串口通信,通过C#编程实现从0x00到0xFF的十六进制数发送,并在界面上设置下拉框选择数值。当点击按钮时,程序将转换选定的十六进制数为字节并尝试通过串口发送。由于没有实际连接端口,因此在运行时会报错。博客重点在于串口通信的编程实现和异常处理。
摘要由CSDN通过智能技术生成

题目概述:
VS2017开发环境
PC串口发送数据
编程:
namespace _004_7_28
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)//窗口创建初始化
    {
        string str;//用来临时存储i的大写的十六进制格式字符串
        for(int i=0;i<256;i++)//0x00~0xff
        {
            str = i.ToString("x").ToUpper();//ToString("x")是将数字转换为16进制字符串,ToUpper是将字符串所有字符大写
            //comboBox1.Items.Add("0x" + (str.Length == 1 ? "0" + str : str));
            if (str.Length == 1)
            {
                str = "0" + str;//如果是一位的(0xA),此时为了对齐,在数据前加一个字符“0”(0x0A)
            }
            comboBox1.Items.Add("0x" + str);//统一添加“0x”
        }
        comboBox1.Text = "0x00";//初始值
        
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)//按键单击事件
    {
        string data = comboBox1.Text;//存储当前下拉框内容
        string convertdata = data.Substring(2, 2);//把字符分开  将“0x"忽略
        byte[] buffer = new byte[1];//数据一个字节就够用了
        buffer[0] = Convert.ToByte(convertdata, 16);//将字符串转化为byte型变量(byte相当于unsigned char
        try//防止出错
        {
            serialPort1.Open();//如果串口打开失败
            serialPort1.Write(buffer, 0, 1);//如果是写数据出错  发送buffer数组 从0开始 发送1个字节
            serialPort1.Close();//如果串口关闭失败
        }
        catch//如果出错就执行此程序
        {
            if(serialPort1.IsOpen)//判断串口是否打开成功
            {
                serialPort1.Close();//如果是写数据出错,此时窗口状态为开,就应该关闭串口,防止下次不能使用,串口是不能重复打开和关闭的
            }
            MessageBox.Show("端口错误", "错误");
        }
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

    }
}

}
上机实践:
请添加图片描述
由于没有连接端口,所以发送数据报错。
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值