• 大小: 84KB
    文件类型: .rar
    金币: 2
    下载: 2 次
    发布日期: 2021-09-22
  • 语言: C#
  • 标签: c#  串口通信  

资源简介

一款已经完成的常用串口通信软件,支持常见编码方式,附赠源码。

资源截图

代码片段和文件信息

using System;
using System.Windows.Forms;
using System.IO.Ports;
using System.Text;

namespace Traveller_SerialPortControl
{
    public partial class Form1 : Form
    {
        //定义端口类
        private SerialPort ComDevice = new SerialPort();
        public Form1()
        {
            InitializeComponent();
            InitralConfig();
        }
        /// 
        /// 配置初始化
        /// 

        private void InitralConfig()
        {
            //查询主机上存在的串口
            comboBox_Port.Items.AddRange(SerialPort.GetPortNames());

            if (comboBox_Port.Items.Count > 0)
            {
                comboBox_Port.SelectedIndex = 0;
            }
            else
            {
                comboBox_Port.Text = “未检测到串口“;
            }
            comboBox_BaudRate.SelectedIndex = 5;
            comboBox_DataBits.SelectedIndex = 0;
            comboBox_StopBits.SelectedIndex = 0;
            comboBox_CheckBits.SelectedIndex = 0;
            pictureBox_Status.BackgroundImage = Properties.Resources.red;

            //向ComDevice.DataReceived(是一个事件)注册一个方法Com_DataReceived,当端口类接收到信息时时会自动调用Com_DataReceived方法
            ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
        }

        /// 
        /// 一旦ComDevice.DataReceived事件发生,就将从串口接收到的数据显示到接收端对话框
        /// 

        /// 
        /// 
        private void Com_DataReceived(object sender SerialDataReceivedEventArgs e)
        {
            //开辟接收缓冲区
            byte[] ReDatas = new byte[ComDevice.BytesToRead];
            //从串口读取数据
            ComDevice.Read(ReDatas 0 ReDatas.Length);
            //实现数据的解码与显示
            AddData(ReDatas);
        }

        /// 
        /// 解码过程
        /// 

        /// 串口通信的数据编码方式因串口而异,需要查询串口相关信息以获取
        public void AddData(byte[] data)
        {
            if (radioButton_Hex.Checked)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sb.AppendFormat(“{0:x2}“ + “ “ data[i]);
                }
                AddContent(sb.ToString().ToUpper());
            }
            else if (radioButton_ASCII.Checked)
            {
                AddContent(new ASCIIEncoding().GetString(data));
            }
            else if (radioButton_UTF8.Checked)
            {
                AddContent(new UTF8Encoding().GetString(data));
            }
            else if (radioButton_Unicode.Checked)
            {
                AddContent(new UnicodeEncoding().GetString(data));
            }
            else
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.Length; i++)
                {
                    sb.AppendFormat(“{0:x2}“ + “ “ data[i]

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

    ..A..H.     47104  2017-06-04 13:19  Traveller_SerialPortControl\.vs\Traveller_SerialPortControl\v14\.suo

     文件        189  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\App.config

     文件      25088  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\bin\Debug\Traveller_SerialPortControl.exe

     文件        189  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\bin\Debug\Traveller_SerialPortControl.exe.config

     文件      30208  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\bin\Debug\Traveller_SerialPortControl.pdb

     文件      22696  2017-06-04 11:01  Traveller_SerialPortControl\Traveller_SerialPortControl\bin\Debug\Traveller_SerialPortControl.vshost.exe

     文件        189  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\bin\Debug\Traveller_SerialPortControl.vshost.exe.config

     文件        490  2016-07-16 19:44  Traveller_SerialPortControl\Traveller_SerialPortControl\bin\Debug\Traveller_SerialPortControl.vshost.exe.manifest

     文件       9267  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\Form1.cs

     文件      18683  2017-06-04 11:33  Traveller_SerialPortControl\Traveller_SerialPortControl\Form1.Designer.cs

     文件       5817  2017-06-04 11:33  Traveller_SerialPortControl\Traveller_SerialPortControl\Form1.resx

     文件       1464  2017-06-04 11:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7572  2017-06-04 11:15  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       4096  2017-06-03 14:38  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件       1140  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.csproj.FileListAbsolute.txt

     文件       1088  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.csproj.GenerateResource.Cache

     文件       2384  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.csprojResolveAssemblyReference.cache

     文件      25088  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.exe

     文件        180  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.Form1.resources

     文件      30208  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.pdb

     文件       6745  2017-06-04 13:01  Traveller_SerialPortControl\Traveller_SerialPortControl\obj\Debug\Traveller_SerialPortControl.Properties.Resources.resources

     文件        539  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\Program.cs

     文件       1362  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\Properties\AssemblyInfo.cs

     文件       3626  2017-06-03 14:38  Traveller_SerialPortControl\Traveller_SerialPortControl\Properties\Resources.Designer.cs

     文件       6433  2017-06-03 14:38  Traveller_SerialPortControl\Traveller_SerialPortControl\Properties\Resources.resx

     文件       1114  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\Properties\Settings.Designer.cs

     文件        249  2017-06-03 13:42  Traveller_SerialPortControl\Traveller_SerialPortControl\Properties\Settings.settings

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

评论

共有 条评论