資源簡介
獲取電腦連接在串口和數據
代碼片段和文件信息
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個文件信息
評論
共有 條評論