• 大小: 60KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: 其他
  • 标签: 串口  C#  

资源简介

简单的串口通讯例子

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Communication
{
    public partial class Form1 : Form
    {
        /// 
        /// 创建串口对象
        /// 

        private SerialPort port = new SerialPort();
        /// 
        /// 接受消息
        /// 

        private StringBuilder MsgStr = new StringBuilder();
        /// 
        /// 构造函数
        /// 

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;   //防止跨线程访问出错,好多地方会用到 
        }
        /// 
        /// 窗口加载函数
        /// 

        /// 
        /// 
        private void Form1_Load(object sender EventArgs e)
        {
            ///获取窗口列表
            string[] ports = SerialPort.GetPortNames();
            if (ports.Length > 0)
            {
                ///添加可选通讯口
                com.Items.AddRange(ports);
                ///设置默认选择
                com.SelectedIndex = 0;
                bot.SelectedIndex = 0;
                databit.SelectedIndex = 0;
                stopbit.SelectedIndex = 0;
                jy.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show(“没有可用的串口!“);
            }
        }
        /// 
        /// 确认按钮
        /// 

        /// 
        /// 
        private void button1_Click(object sender EventArgs e)
        {
            try
            {
                ///检测串口是否开着
                if (!port.IsOpen)
                {
                    ///设置串口名称
                    port.PortName = com.SelectedItem.ToString();
                    ///设置波特率
                    port.BaudRate = Convert.ToInt32(bot.Text);
                    ///数据位
                    port.DataBits = Convert.ToInt32(databit.Text);
                    ///设置无协议
                    port.Handshake = Handshake.None;
                    ///设置读取不超时
                    port.ReadTimeout = -1;
                    //port.WriteTimeout = -1;
                    ///设置停止位
                    port.StopBits = GetStopBits(stopbit.SelectedIndex + 1);

                    port.DtrEnable = true;
                    ///无校验
                    port.Parity = GetParity(jy.SelectedIndex);
                    ///设置数据接收事件
                    port.DataReceived += ReciveData;
                    ///打开串口
                    port.Open();
                    open.Text = “关闭串口“;
                    UpdateStutas();
                }
                else
                {
                    ///关闭串口
                    port.Close();
                    port.DataReceived -= ReciveData;
                    open.Text = “打开串口“;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     53248  2017-03-07 12:49  Communication\.vs\Communication\v14\.suo

     文件        189  2017-03-03 14:53  Communication\Communication\App.config

     文件      15360  2017-03-07 12:44  Communication\Communication\bin\Debug\Communication.exe

     文件        189  2017-03-03 14:53  Communication\Communication\bin\Debug\Communication.exe.config

     文件      24064  2017-03-07 12:44  Communication\Communication\bin\Debug\Communication.pdb

     文件      22696  2017-03-07 12:44  Communication\Communication\bin\Debug\Communication.vshost.exe

     文件        189  2017-03-03 14:53  Communication\Communication\bin\Debug\Communication.vshost.exe.config

     文件        490  2015-11-13 10:24  Communication\Communication\bin\Debug\Communication.vshost.exe.manifest

     文件       3924  2017-03-03 17:22  Communication\Communication\Communication.csproj

     文件       7962  2017-03-07 12:49  Communication\Communication\Form1.cs

     文件      11645  2017-03-07 12:44  Communication\Communication\Form1.Designer.cs

     文件       6017  2017-03-07 12:44  Communication\Communication\Form1.resx

     文件        870  2017-03-07 12:44  Communication\Communication\obj\Debug\Communication.csproj.FileListAbsolute.txt

     文件       1012  2017-03-07 12:44  Communication\Communication\obj\Debug\Communication.csproj.GenerateResource.Cache

     文件       2384  2017-03-03 17:22  Communication\Communication\obj\Debug\Communication.csprojResolveAssemblyReference.cache

     文件      15360  2017-03-07 12:44  Communication\Communication\obj\Debug\Communication.exe

     文件        180  2017-03-07 12:44  Communication\Communication\obj\Debug\Communication.Form1.resources

     文件      24064  2017-03-07 12:44  Communication\Communication\obj\Debug\Communication.pdb

     文件        180  2017-03-03 17:22  Communication\Communication\obj\Debug\Communication.Properties.Resources.resources

     文件       1464  2017-03-06 09:29  Communication\Communication\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7220  2017-03-03 14:53  Communication\Communication\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2017-03-03 14:53  Communication\Communication\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-03-03 14:53  Communication\Communication\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-03-03 14:53  Communication\Communication\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        525  2017-03-03 14:53  Communication\Communication\Program.cs

     文件       1334  2017-03-03 14:53  Communication\Communication\Properties\AssemblyInfo.cs

     文件       2839  2017-03-03 14:53  Communication\Communication\Properties\Resources.Designer.cs

     文件       5612  2017-03-03 14:53  Communication\Communication\Properties\Resources.resx

     文件       1100  2017-03-03 14:53  Communication\Communication\Properties\Settings.Designer.cs

     文件        249  2017-03-03 14:53  Communication\Communication\Properties\Settings.settings

............此处省略15个文件信息

评论

共有 条评论