C#编写串口小程序(一)

功能展示:
在这里插入图片描述
代码:

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;
using System.IO.Ports;
using System.IO;
using System.Text.RegularExpressions;

namespace chuankou5
{
public partial class Form1 : Form
{

    string data;
    private string[] portNames = null;
    private object[] baudRates = { 4800, 9600, 19200 };
    private static string[] dataBuff=new string[1000];
    private bool Ready = false;
    private bool contrl = false;
    private static int getData_Num = 0;
           
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        portNames = SerialPort.GetPortNames();
        this.portNumberComboBox1.Items.AddRange(this.portNames);
        this.baudRatecomboBox.Items.AddRange(baudRates);
        
    }
    //发送数据
    private void button1_Click(object sender, EventArgs e)
    {
        //如果contrl未false,串口没有打开
        if(contrl==false)
        {
            int baurate = 9600;
            string port = portNumberComboBox1.Text;
            string baurate_str = baudRatecomboBox.Text.Trim();
            if (port != "")
            {
                serialPort1.PortName = port;
                if (baurate_str != "")
                {
                    int baurate_int = Convert.ToInt16(baurate_str) + 1;
                    baurate = baurate_int - 1;
                    serialPort1.BaudRate = baurate;
                    Ready = true;
                    openButton.Text = "关闭串口";
                    contrl = true;
                }
            }
            else
            {
                MessageBox.Show("请选择要打开的串口号!", "提示");
            } 
        }
        else
        {
            Ready = false;
            openButton.Text = "打开串口";
            contrl = false;
        }
        OpenPort();
    }
    private void OpenPort()
    {
        //如果准备好,将则将端口打开
        if (Ready==true)
        {
            try
            {
                serialPort1.Open();
            }
            catch(Exception ex)
            {
                MessageBox.Show("串行端口打开失败!具体原因:" + ex.Message, "提示信息");
            }
        }
        else
        {
            serialPort1.Close();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        serialPort1.Close();
        Close();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (serialPort1.IsOpen) serialPort1.Close();
    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        data = serialPort1.ReadExisting();
        int buffData_num = dataBuff.Length;
        dataBuff[getData_Num] = data;
        this.Invoke(new EventHandler(DisplayText));
        getData_Num = getData_Num + 1;
        
    }

 //显示数据 
    private void DisplayText(object sender,EventArgs e)
    {
        //char[] data1 = data.ToCharArray();//字符串转成字符数组
        //short data2 = (short)data1[0];

        textBox2.Text = textBox2.Text + data;
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void setDataButton_Click_1(object sender, EventArgs e)
    {
        string outdata = this.textBox1.Text; 
        if (outdata == "")
        {
            MessageBox.Show("发送的数据不能为空!");
        }
        else
        {
            serialPort1.Write(outdata);
        }
           
    }
}

}

C#串口介绍以及简单串口通信程序设计实现 源代码和串口程序介绍连接:https://www.cnblogs.com/JiYF/p/6618696.html 本站积分太贵,自己变得。。直接到连接地址下载代码 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍   串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。(至于再详细,自己百度) 串口应用:   工业领域使用较多,比如:数据采集,设备控制等等,好多都是用串口通信来实现!你要是细心的话,你会发现,目前家用国网智能电能表就具备RS485通信总线(串行总线的一种)与RS232可以相互转化(当然一般,非专业的谁也不会闲的蛋疼,趴电表上瞎看,最多也就看看走了多少度电) RS232 DB9介绍: 1.示意图 2.针脚介绍: 载波检测(DCD) 接受数据(RXD) 发出数据(TXD) 数据终端准备好(DTR) 信号地线(SG) 数据准备好(DSR) 请求发送(RTS) 清除发送(CTS) 振铃指示(RI) 3.实物图: 以下是我购买XX公司的一个usb转串口线:这个头就是一个公头,另一端是一个usb口 笨小孩串口工具运行图: 1.开启程序 2.发送一行字符串HelloBenXH,直接将针脚的发送和接收链接起来就可以测试了(针脚2 接受数据(RXD) 和3 发出数据(TXD))直接链接, C#代码实现:采用SerialPort 1.实例化一个SerialPort [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 private SerialPort ComDevice = new SerialPort(); 2.初始化参数绑定接收数据事件 [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 public void init() { btnSend.Enabled = false; cbbComList.Items.AddRange(SerialPort.GetPortNames()); if (cbbComList.Items.Count > 0) { cbbComList.SelectedIndex = 0; } cbbBaudRate.SelectedIndex = 5; cbbDataBits.SelectedIndex = 0; cbbParity.SelectedIndex = 0; cbbStopBits.SelectedIndex = 0; pictureBox1.BackgroundImage = Properties.Resources.red; ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值