-
大小: 51KB文件類型: .zip金幣: 2下載: 1 次發(fā)布日期: 2021-05-09
- 語言: C#
- 標(biāo)簽: SerialPort??
資源簡介
支持串口大量數(shù)據(jù)讀寫操作,能一次性讀取數(shù)據(jù),通過設(shè)置超時(shí)大小控制一次讀取串口所有數(shù)據(jù)。支持事件觸發(fā),異步發(fā)送接收數(shù)據(jù)。附件有demo,可下載Virtual Serial Port Driver模擬串口測試。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO.Ports;
using?System.Linq;
using?System.Text;
using?System.Threading.Tasks;
using?System.Windows.Forms;
using?YS.IO.Ports;
namespace?SerialPortTest
{
????public?partial?class?Form1?:?Form
????{
????????SerialPortListener?m_SerialPort?=?null;
????????SerialPort?m_s?=?null;
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????string[]?portlist?=?SerialPort.GetPortNames();
????????}
????????private?void?btnListen_Click(object?sender?EventArgs?e)
????????{
????????????if?(m_SerialPort?==?null)
????????????{
????????????????m_SerialPort?=?new?SerialPortListener(tbPort.Text?Convert.ToInt32(tbFrequency.Text));
????????????????m_SerialPort.Parity?=?Parity.None;
????????????????m_SerialPort.StopBits?=?StopBits.One;
????????????????m_SerialPort.Handshake?=?Handshake.None;
????????????????m_SerialPort.DataBits?=?8;
????????????????m_SerialPort.ReadBufferSize?=?100;
????????????????m_SerialPort.ReceivedBytesThreshold?=?1;
????????????????m_SerialPort.BufferSize?=?4;
????????????????m_SerialPort.ReceiveTimeout?=?Convert.ToInt32(tbTimeout.Text);
????????????????m_SerialPort.WriteBufferSize?=?100;
????????????????m_SerialPort.SendInterval?=?100;
????????????????m_SerialPort.SerialPortResult?+=?new?HandResult(SerialPort_Result);
????????????????m_SerialPort.OnSerialPortReceived?+=?new?OnReceivedData(SerialPort_Received);
????????????????m_SerialPort.OnSeriaPortSend?+=?new?OnSendData(SerialPort_Send);
????????????}
????????????if?(m_SerialPort.IsOpen)
????????????{
????????????????m_SerialPort.Stop();
????????????????btnListen.Text?=?“啟動(dòng)監(jiān)聽“;
????????????}
????????????else
????????????{
????????????????m_SerialPort.Start();
????????????????btnListen.Text?=?“關(guān)閉監(jiān)聽“;
????????????}
????????}
????????void?SerialPort_Result(object?senderSerialPortEvents?e)
????????{
????????????this.Invoke(new?MethodInvoker(()?=>?{
????????????????//處理結(jié)果
????????????????rtbResult.Text?+=?Encoding.GetEncoding(“GB2312“).GetString(e.BufferData);
????????????}));
????????}
????????void?SerialPort_Received(object?sender?SerialPortEvents?e)
????????{
????????????this.Invoke(new?MethodInvoker(()?=>
????????????{
????????????????long?receivedCount?=?Convert.ToInt64(lbReceivedCount.Text);
????????????????if?(e.BufferData?!=?null)
????????????????{
????????????????????receivedCount?+=?e.BufferData.Length;
????????????????}
????????????????lbReceivedCount.Text?=?receivedCount.ToString();
????????????}));
????????}
????????void?SerialPort_Send(object?sender?SerialPortEvents?e)
????????{
????????????this.Invoke(new?MethodInvoker(()?=>
????????????{
????????????????long?sendCount?=?Convert.ToInt64(lbSendCount.Text);
????????????????if?(e.BufferData?!=?null)
????????
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-10-16?12:06??SerialPortTest\
?????目錄???????????0??2016-10-16?21:58??SerialPortTest\SerialPortTest\
?????文件????????1480??2016-10-16?12:06??SerialPortTest\SerialPortTest.sln
?????文件???????73216??2016-10-16?21:59??SerialPortTest\SerialPortTest.v12.suo
?????文件?????????187??2016-10-16?00:00??SerialPortTest\SerialPortTest\App.config
?????目錄???????????0??2016-10-16?21:58??SerialPortTest\SerialPortTest\bin\
?????目錄???????????0??2016-10-16?21:58??SerialPortTest\SerialPortTest\bin\Debug\
?????文件?????????187??2016-10-16?00:00??SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.exe.config
?????文件???????24224??2016-10-16?21:58??SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.vshost.exe
?????文件?????????187??2016-10-16?00:00??SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.vshost.exe.config
?????文件?????????490??2010-03-17?22:39??SerialPortTest\SerialPortTest\bin\Debug\SerialPortTest.vshost.exe.manifest
?????文件????????3633??2016-10-16?21:48??SerialPortTest\SerialPortTest\Form1.cs
?????文件????????9733??2016-10-16?21:26??SerialPortTest\SerialPortTest\Form1.Designer.cs
?????文件????????5817??2016-10-16?21:26??SerialPortTest\SerialPortTest\Form1.resx
?????目錄???????????0??2016-10-16?00:00??SerialPortTest\SerialPortTest\obj\
?????目錄???????????0??2016-10-16?21:53??SerialPortTest\SerialPortTest\obj\Debug\
?????文件?????????865??2016-10-16?00:00??SerialPortTest\SerialPortTest\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????7064??2016-10-16?12:07??SerialPortTest\SerialPortTest\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????934??2016-10-16?21:58??SerialPortTest\SerialPortTest\obj\Debug\SerialPortTest.csproj.FileListAbsolute.txt
?????文件???????????0??2016-10-16?00:00??SerialPortTest\SerialPortTest\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件???????????0??2016-10-16?00:00??SerialPortTest\SerialPortTest\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件???????????0??2016-10-16?00:00??SerialPortTest\SerialPortTest\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????目錄???????????0??2016-10-16?00:00??SerialPortTest\SerialPortTest\obj\Debug\TempPE\
?????文件?????????526??2016-10-16?00:00??SerialPortTest\SerialPortTest\Program.cs
?????目錄???????????0??2016-10-16?00:00??SerialPortTest\SerialPortTest\Properties\
?????文件????????1370??2016-10-16?00:00??SerialPortTest\SerialPortTest\Properties\AssemblyInfo.cs
?????文件????????2880??2016-10-16?00:00??SerialPortTest\SerialPortTest\Properties\Resources.Designer.cs
?????文件????????5612??2016-10-16?00:00??SerialPortTest\SerialPortTest\Properties\Resources.resx
?????文件????????1101??2016-10-16?00:00??SerialPortTest\SerialPortTest\Properties\Settings.Designer.cs
?????文件?????????249??2016-10-16?00:00??SerialPortTest\SerialPortTest\Properties\Settings.settings
?????文件????????4011??2016-10-16?12:07??SerialPortTest\SerialPortTest\SerialPortTest.csproj
............此處省略18個(gè)文件信息
評(píng)論
共有 條評(píng)論