C# 232通信--SerialPort类

       程序代码放在百度网盘,如果有小伙伴想要这个程序,可以去下面链接下载。

       下载链接:https://pan.baidu.com/s/1o2G9_Uac0OyoVZ-LEFnLqQ

       提取码:1234

         一直想着自己写个232通讯的程序,终于有时间实现,利用C#中SerialPort这个类,还是比较简单的,废话不多说,直接上图和代码。

        打开Com口前,这里端口是自动识别机器可用的com口,其余的选项是我自己手敲上去的,反正也不多:

打开com口后:

由于我只插了一根usb转串口线进行的测试,因此看不到返回值,很遗憾,不知道是否能有返回。

下面本程序的代码:

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;

namespace Test_232
{
    public partial class Test_232 : Form
    {
        SerialPort Conn = new SerialPort();
        public Test_232()
        {
            InitializeComponent();
            string[] ArryPort = SerialPort.GetPortNames();
            for (int i = 0; i < ArryPort.Length; i++)
            {
                CB_Com.Items.Add(ArryPort[i]);
                CB_Com.SelectedIndex = 0;
            }
        }

        private void BT_Open_Click(object sender, EventArgs e)
        {
            try
            {
                if (Conn.IsOpen) Conn.Close();
                else
                {
                    //获取端口信息
                    Conn.PortName = CB_Com.Text;
                    Conn.BaudRate = int.Parse(CB_Boud.Text);
                    Conn.DataBits = int.Parse(CB_Data.Text);
                    Conn.StopBits = (StopBits)Enum.Parse(typeof(StopBits), CB_Stop.Text);

                    if(!"No".Equals(CB_Patity.Text))
                        Conn.Parity = (Parity)Enum.Parse(typeof(Parity), CB_Patity.Text);

                    //打开端口
                    Conn.Open();
                }

                //设置空间状态
                Set_Enable(!Conn.IsOpen);

                if (Conn.IsOpen)
                {
                    BT_Open.Text = "关闭端口";
                    BX_Send.Focus();
                }
                else
                {
                    BT_Open.Text = "打开端口";
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("端口打开失败!!!\n" + er.Message, "提示");
            }
        }

        private void Set_Enable(bool Enable)
        {
            //设置com口等信息状态
            CB_Com.Enabled = Enable;
            CB_Boud.Enabled = Enable;
            CB_Data.Enabled = Enable;
            CB_Patity.Enabled = Enable;
            CB_Stop.Enabled = Enable;

            //设置发送框状态
            BX_Send.Enabled = (!Enable);

            //设置发送数据按钮状态
            BT_Send.Enabled = (!Enable);
        }

        private void BT_Send_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < BX_Send.Lines.Count(); i++)
            {
                Conn.WriteLine(BX_Send.Lines[i]);
                BX_Recieve.Text = BX_Recieve.Text + "\n Send Data:" + BX_Send.Lines[i]; 
            }
            //清空发送区内容
            BX_Send.Text = "";
            
            //获取光标焦点
            BX_Send.Focus();
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {             
            //使用委托进行跨线程读取数据          
            Invoke(
                new EventHandler(
                    delegate
                    {
                        BX_Recieve.Text = Conn.ReadExisting();
                    })
                );
        }
    }
}

 

评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值