資源簡介
簡單的串口通信工具,可以實現串口一些屬性的設置,文件是在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個文件信息
評論
共有 條評論