• 大小: 191KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-17
  • 语言: C#
  • 标签: 代码  

资源简介

简单的串口通信工具,可以实现串口一些属性的设置,文件是在VS2017下的整个工程文件,方便修改、学习。C#语言实现。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _20180601
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender EventArgs e)//单击清除窗口按钮事件
        {
            richTextBox1.Clear();
        }

        private void button1_Click(object sender EventArgs e)//单击打开串口按钮事件
        {
            serialPort1.PortName = “COM5“;
            serialPort1.BaudRate = 115200;
            serialPort1.StopBits = System.IO.Ports.StopBits.One;
            serialPort1.DataBits = 8;
            serialPort1.Parity = System.IO.Ports.Parity.None;

            try
            {
                serialPort1.Open();//打开串口后,锁定下拉框。
                comboBox1.Enabled = false;
                comboBox2.Enabled = false;
                comboBox3.Enabled = false;
                comboBox4.Enabled = false;
                comboBox5.Enabled = false;
            }
            catch
            {
                MessageBox.Show(“串口打开错误,请检查!““消息提示“);
            }
        }

        private void button2_Click(object sender EventArgs e)//点击关闭串口事件
        {
            serialPort1.Close();
            comboBox1.Enabled = true;//关闭串口后,下拉框可选。
            comboBox2.Enabled = true;
            comboBox3.Enabled = true;
            comboBox4.Enabled = true;
            comboBox5.Enabled = true;
        }

        private void button4_Click(object sender EventArgs e)//点击保存按钮事件
        {
            textBox3.Text = “D:\\“;
            string curTime = DateTime.Now.ToString(“yyyyMMddhhmmss“);//获取系统当前时间
            textBox3.Text += curTime+“.txt“;//将当前时间文本显示在文本框中
            FileStream fs = new FileStream(“D:\\“+ curTime+“.txt“ FileMode.Create FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(richTextBox1.Text);
            sw.Flush();
            sw.Close();
        }

        private void button6_Click(object sender EventArgs e)//点击发送文件按钮
        {
            OpenFileDialog file = new OpenFileDialog();
            file.title = “请选择要发送的文件“;
            file.Filter = “文本文件|*.txt“;
            if (file.ShowDialog() == DialogResult.OK)
            {
                // richTextBox2.LoadFile(file.FileName); 报错,提示无效的文件格式。
                //如下,将文本文件中的内容读取到流中,再写入richTextBox2中
                try
                {
                    using (StreamReader sr = new StreamReader(file.FileName))
                    {
                        string line;
                        //循环发送将下列代码for(1000次)
                        while ((line = sr.ReadLine()) != null)
                        {
                            richTextBox2.Text += line;

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

    ..A..H.     49664  2018-06-02 13:14  20180601\.vs\20180601\v15\.suo

     文件     647168  2018-06-02 13:13  20180601\.vs\20180601\v15\sqlite3\storage.ide

     文件       3564  2018-06-01 18:40  20180601\20180601\20180601.csproj

     文件        189  2018-06-01 18:23  20180601\20180601\App.config

     文件      22528  2018-06-02 13:13  20180601\20180601\bin\Debug\20180601.exe

     文件        189  2018-06-01 18:23  20180601\20180601\bin\Debug\20180601.exe.config

     文件      26112  2018-06-02 13:13  20180601\20180601\bin\Debug\20180601.pdb

     文件       5643  2018-06-02 13:13  20180601\20180601\Form1.cs

     文件      21594  2018-06-02 13:09  20180601\20180601\Form1.Designer.cs

     文件      12748  2018-06-02 13:09  20180601\20180601\Form1.resx

     文件         42  2018-06-01 18:23  20180601\20180601\obj\Debug\20180601.csproj.CoreCompileInputs.cache

     文件        684  2018-06-01 18:40  20180601\20180601\obj\Debug\20180601.csproj.FileListAbsolute.txt

     文件       1012  2018-06-02 13:13  20180601\20180601\obj\Debug\20180601.csproj.GenerateResource.Cache

     文件       2384  2018-06-01 18:40  20180601\20180601\obj\Debug\20180601.csprojResolveAssemblyReference.cache

     文件      22528  2018-06-02 13:13  20180601\20180601\obj\Debug\20180601.exe

     文件      26112  2018-06-02 13:13  20180601\20180601\obj\Debug\20180601.pdb

     文件       1464  2018-06-02 09:49  20180601\20180601\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7120  2018-06-01 19:41  20180601\20180601\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2018-06-01 18:23  20180601\20180601\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-06-01 18:23  20180601\20180601\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-06-01 18:23  20180601\20180601\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       4852  2018-06-02 13:13  20180601\20180601\obj\Debug\_20180601.Form1.resources

     文件        180  2018-06-01 18:40  20180601\20180601\obj\Debug\_20180601.Properties.Resources.resources

     文件        521  2018-06-01 18:23  20180601\20180601\Program.cs

     文件       1314  2018-06-01 18:23  20180601\20180601\Properties\AssemblyInfo.cs

     文件       2831  2018-06-01 18:23  20180601\20180601\Properties\Resources.Designer.cs

     文件       5612  2018-06-01 18:23  20180601\20180601\Properties\Resources.resx

     文件       1096  2018-06-01 18:23  20180601\20180601\Properties\Settings.Designer.cs

     文件        249  2018-06-01 18:23  20180601\20180601\Properties\Settings.settings

     文件       1123  2018-06-01 18:23  20180601\20180601.sln

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

评论

共有 条评论