• 大小: 0.06M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-04-28
  • 语言: C#
  • 标签: 获取  串口  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;

namespace _01_SerialPort
{
    public partial class Form1 : Form
    {
        public delegate void showReceiveDelegate (string text);//当采用响应模式,应申明一个委托,实现不同线程的控件实验
        SerialPort com = new SerialPort (“COM4“9600Parity.None8StopBits.One);
        public Form1 ()
        {
            InitializeComponent ();
        }
        //窗体加载
        private void Form1_Load (object sender EventArgs e)
        {
            cmbPort.SelectedIndex = 0;
            cmbBaudRate.SelectedIndex = 0;
            cmbDataBits.SelectedIndex = 0;
            cmbStopBits.SelectedIndex = 0;
            cmbParity.SelectedIndex = 0;
        }
        //串口打开与关闭
        private void btnOpen_Click (object sender EventArgs e)
        {
            if (btnOpen.Text == “打开串口“)
            {
                try
                {
                    if (!com.IsOpen)
                    {
                        com.PortName = cmbPort.Text;
                        com.BaudRate = int.Parse (cmbBaudRate.Text);
                        com.DataBits = int.Parse (cmbDataBits.Text);
                        switch (cmbStopBits.SelectedIndex)
                        {
                            case 0:
                                com.StopBits = StopBits.One; break;
                            case 1:
                                com.StopBits = StopBits.Two; break;
                            case 2:
                                com.StopBits = StopBits.OnePointFive; break;
                            case 3:
                                com.StopBits = StopBits.None; break;
                        }
                        switch (cmbParity.SelectedIndex)
                        {
                            case 0: com.Parity = Parity.None; break;
                            case 1: com.Parity = Parity.Odd; break;
                            case 2: com.Parity = Parity.Even; break;
                        }
                        com.Open ();//打开串口
                    }
                    btnOpen.Text = “关闭串口“;
                    txtStatus.Text = “串口已打开!“;
                    btnSend.Enabled = true;
                    if (rbAck.Checked)
                        btnReceive.Enabled = true;//应答模式,接收按钮有效
                }
                catch
                {
                    txtStatus.Text = “串口打开错误或串口不存在!“;
                }
            }
            else
                try
                {
                    if (com.IsOpen)
                        com.Close ();//关闭串口
                    btnOpen.Text = “打开串口“;
                    txtStatus.Text = “串口已关闭!“;
                    btnSend.Enabled = false;
                    if (rbAck.Checked)
         

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

    ..A..H.     20480  2020-10-28 12:58  01_SerialPort\.vs\01_SerialPort\v14\.suo

     文件       3925  2020-10-28 13:05  01_SerialPort\01_SerialPort\01_SerialPort.csproj

     文件        189  2020-10-28 12:58  01_SerialPort\01_SerialPort\App.config

     文件      16896  2020-10-28 15:06  01_SerialPort\01_SerialPort\bin\Debug\01_SerialPort.exe

     文件        189  2020-10-28 12:58  01_SerialPort\01_SerialPort\bin\Debug\01_SerialPort.exe.config

     文件      28160  2020-10-28 15:06  01_SerialPort\01_SerialPort\bin\Debug\01_SerialPort.pdb

     文件      22696  2020-10-28 15:06  01_SerialPort\01_SerialPort\bin\Debug\01_SerialPort.vshost.exe

     文件        189  2020-10-28 12:58  01_SerialPort\01_SerialPort\bin\Debug\01_SerialPort.vshost.exe.config

     文件       9069  2020-10-28 15:06  01_SerialPort\01_SerialPort\Form1.cs

     文件      17033  2020-10-28 15:06  01_SerialPort\01_SerialPort\Form1.Designer.cs

     文件       5817  2020-10-28 15:06  01_SerialPort\01_SerialPort\Form1.resx

     文件        737  2020-10-28 15:06  01_SerialPort\01_SerialPort\obj\Debug\01_SerialPort.csproj.FileListAbsolute.txt

     文件       1012  2020-10-28 15:06  01_SerialPort\01_SerialPort\obj\Debug\01_SerialPort.csproj.GenerateResource.Cache

     文件       2384  2020-10-28 13:05  01_SerialPort\01_SerialPort\obj\Debug\01_SerialPort.csprojResolveAssemblyReference.cache

     文件      16896  2020-10-28 15:06  01_SerialPort\01_SerialPort\obj\Debug\01_SerialPort.exe

     文件      28160  2020-10-28 15:06  01_SerialPort\01_SerialPort\obj\Debug\01_SerialPort.pdb

     文件        868  2020-10-28 12:58  01_SerialPort\01_SerialPort\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7160  2020-10-28 12:58  01_SerialPort\01_SerialPort\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2020-10-28 12:58  01_SerialPort\01_SerialPort\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2020-10-28 12:58  01_SerialPort\01_SerialPort\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2020-10-28 12:58  01_SerialPort\01_SerialPort\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        180  2020-10-28 15:06  01_SerialPort\01_SerialPort\obj\Debug\_01_SerialPort.Form1.resources

     文件        180  2020-10-28 13:05  01_SerialPort\01_SerialPort\obj\Debug\_01_SerialPort.Properties.Resources.resources

     文件        531  2020-10-28 12:58  01_SerialPort\01_SerialPort\Program.cs

     文件       1346  2020-10-28 12:58  01_SerialPort\01_SerialPort\Properties\AssemblyInfo.cs

     文件       2850  2020-10-28 12:58  01_SerialPort\01_SerialPort\Properties\Resources.Designer.cs

     文件       5612  2020-10-28 12:58  01_SerialPort\01_SerialPort\Properties\Resources.resx

     文件       1105  2020-10-28 12:58  01_SerialPort\01_SerialPort\Properties\Settings.Designer.cs

     文件        249  2020-10-28 12:58  01_SerialPort\01_SerialPort\Properties\Settings.settings

     文件       1006  2020-10-28 12:58  01_SerialPort\01_SerialPort.sln

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

评论

共有 条评论