资源简介

自己写的串口通讯帮助类,实现应答机制,通讯中发送一条指令会等待应答,同步异步都有

资源截图

代码片段和文件信息

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 WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private delegate byte[] Del(string data);

        SerialPortHelper sph = new SerialPortHelper(“COM2“);
        SerialPortHelper sph1 = new SerialPortHelper(“COM3“);
        Del d;
        public Form1()
        {
           
            InitializeComponent();
            sph.RecDataEvent += Sph_RecDataEvent;
            sph1.RecDataEvent += Sph1_RecDataEvent;
        }

        private void Sph1_RecDataEvent(object sender RecDataArgs e)
        {
            if (richTextBox2.InvokeRequired)
            {
                Action actionDelegate = () => richTextBox2.Text = Encoding.Default.GetString(e.Data);
                richTextBox1.Invoke(actionDelegate);
            }
        }

        private void Sph_RecDataEvent(object sender RecDataArgs e)
        {
            if (richTextBox1.InvokeRequired)
            {
                Action actionDelegate = () => { richTextBox1.Text = Encoding.Default.GetString(e.Data); };
                richTextBox1.Invoke(actionDelegate);
            }
        }

        private void button1_Click(object sender EventArgs e)
        {
            sph.Open();
            sph1.Open();
        }

        private async void button2_Click(object sender EventArgs e)
        {
           
            try
            {
                byte[] r = await sph.SendWithAckAsyc(“QWEQWEQWEWQE“);
                MessageBox.Show(Encoding.Default.GetString(r));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button3_Click(object sender EventArgs e)
        {
            sph1.Send(“ACK“);
        }

        private void label1_Click(object sender EventArgs e)
        {

        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\.vs\
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\.vs\WindowsFormsApplication3\
     目录           0  2020-03-31 13:50  串口帮助类实现超时机制\.vs\WindowsFormsApplication3\v14\
     文件       48640  2020-03-31 14:34  串口帮助类实现超时机制\.vs\WindowsFormsApplication3\v14\.suo
     目录           0  2020-03-31 14:34  串口帮助类实现超时机制\WindowsFormsApplication3\
     文件         189  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\App.config
     文件        5808  2020-03-31 14:27  串口帮助类实现超时机制\WindowsFormsApplication3\Form1.Designer.cs
     文件        2145  2020-03-31 14:27  串口帮助类实现超时机制\WindowsFormsApplication3\Form1.cs
     文件        6016  2020-03-31 14:27  串口帮助类实现超时机制\WindowsFormsApplication3\Form1.resx
     文件         536  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\Program.cs
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\WindowsFormsApplication3\Properties\
     文件        1356  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\Properties\AssemblyInfo.cs
     文件        2861  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\Properties\Resources.Designer.cs
     文件        5612  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\Properties\Resources.resx
     文件        1111  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\Properties\Settings.Designer.cs
     文件         249  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\Properties\Settings.settings
     文件       12847  2020-03-31 14:34  串口帮助类实现超时机制\WindowsFormsApplication3\SerialPortHelper.cs
     文件        3993  2020-03-31 10:18  串口帮助类实现超时机制\WindowsFormsApplication3\WindowsFormsApplication3.csproj
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\WindowsFormsApplication3\bin\
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\
     文件       20480  2020-03-31 14:27  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.exe
     文件         189  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.exe.config
     文件       42496  2020-03-31 14:27  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.pdb
     文件       22696  2020-03-31 14:30  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.vshost.exe
     文件         189  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.vshost.exe.config
     文件         490  2016-07-16 19:44  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.vshost.exe.manifest
     目录           0  2020-03-31 13:44  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Release\
     文件       13312  2020-03-31 13:02  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Release\WindowsFormsApplication3.exe
     文件         189  2020-03-31 09:42  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Release\WindowsFormsApplication3.exe.config
     文件       28160  2020-03-31 13:02  串口帮助类实现超时机制\WindowsFormsApplication3\bin\Release\WindowsFormsApplication3.pdb
............此处省略31个文件信息

评论

共有 条评论