資源簡介
C# 簡單的串口通訊 親測可用
代碼片段和文件信息
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;
namespace?Csharp串口
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?delegate?void?MyDelegate(string?s);
????????private?void?SetText(string?s)
????????{
????????????TX_Rev.Text?=?TX_Rev.Text?+?s;
????????}
????????private?void?groupBox2_Enter(object?sender?EventArgs?e)
????????{
????????}
????????private?void?statusStrip1_ItemClicked(object?sender?ToolStripItemClickedEventArgs?e)
????????{
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????serialPort1?=?new?System.IO.Ports.SerialPort(“COM3“?9600?System.IO.Ports.Parity.None?8?System.IO.Ports.StopBits.One);
????????????????serialPort1.DataReceived?+=?new?System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
????????????????serialPort1.Open();
????????????}
????????????catch?(Exception?err)
????????????{
????????????????MessageBox.Show(err.Message);
????????????}
????????}
????????public?static?string?byteToHexStr(byte[]?bytes)
????????{
????????????string?returnStr?=?““;
????????????if?(bytes?!=?null)
????????????{
????????????????for?(int?i?=?0;?i?????????????????{
????????????????????returnStr?+=?bytes[i].ToString(“X2“);
????????????????}
????????????}
????????????return?returnStr;
????????}
????????private?void?serialPort1_DataReceived(object?sender?System.IO.Ports.SerialDataReceivedEventArgs?e)
????????{
????????????MyDelegate?md?=?new?MyDelegate(SetText);
????????????try
????????????{
????????????????if?(serialPort1.IsOpen?==?true)
????????????????{?????
????????????????????int?buffersize?=?5;???//十六進制數的大小(假設為6Byte)
????????????????????byte[]?ReadBuffer?=?new?Byte[buffersize];???//創建緩沖區
????????????????????serialPort1.Read(ReadBuffer?0?ReadBuffer.Length);
????????????????????string?ReadStr?=?byteToHexStr(ReadBuffer);??????????????????
????????????????????Invoke(md?ReadStr);
????????????????}
????????????}
????????????catch?(Exception?err)
????????????{
????????????????throw?err;
????????????}
????????}
????????private?void?BTN_Send_Click(object?sender?EventArgs?e)
????????{
????????????serialPort1.WriteLine(TX_Send.Text);
????????}
????????private?void?BTN_Clear_Click(object?sender?EventArgs?e)
????????{
????????????TX_Rev.Text?=?““;
????????}
????????private?void?groupBox1_Enter(object?sender?EventArgs?e)
????????{
????????}
????????private?void?timer1_Tick(object?sender?EventArgs?e)
????????{
????????????float?tmp1?tmp2?tmp3tmp4;
????????????if?(TX_Rev.Text?!=?““)
????????????{
????????????????string?aaa?=?TX_Rev.Text;
??????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????187??2015-11-11?16:12??Csharp串口000\Csharp串口\App.config
?????文件??????15872??2015-11-14?00:03??Csharp串口000\Csharp串口\bin\Debug\Csharp串口.exe
?????文件????????187??2015-11-11?16:12??Csharp串口000\Csharp串口\bin\Debug\Csharp串口.exe.config
?????文件??????32256??2015-11-14?00:03??Csharp串口000\Csharp串口\bin\Debug\Csharp串口.pdb
?????文件??????24224??2015-11-14?00:04??Csharp串口000\Csharp串口\bin\Debug\Csharp串口.vshost.exe
?????文件????????187??2015-11-11?16:12??Csharp串口000\Csharp串口\bin\Debug\Csharp串口.vshost.exe.config
?????文件????????490??2015-07-10?19:01??Csharp串口000\Csharp串口\bin\Debug\Csharp串口.vshost.exe.manifest
?????目錄??????????0??2015-11-13?09:13??Csharp串口000\Csharp串口\bin\Debug
?????目錄??????????0??2015-11-11?22:39??Csharp串口000\Csharp串口\bin
?????文件???????3804??2015-11-11?16:27??Csharp串口000\Csharp串口\Csharp串口.csproj
?????文件???????4669??2015-11-14?00:03??Csharp串口000\Csharp串口\Form1.cs
?????文件??????18154??2015-11-13?23:15??Csharp串口000\Csharp串口\Form1.Designer.cs
?????文件???????6384??2015-11-13?23:15??Csharp串口000\Csharp串口\Form1.resx
?????文件???????2716??2015-11-14?00:04??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.csproj.FileListAbsolute.txt
?????文件????????977??2015-11-13?23:15??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.csproj.GenerateResource.Cache
?????文件???????2211??2015-11-12?07:35??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.csprojResolveAssemblyReference.cache
?????文件??????15872??2015-11-14?00:03??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.exe
?????文件????????180??2015-11-13?23:15??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.Form1.resources
?????文件??????32256??2015-11-14?00:03??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.pdb
?????文件????????180??2015-11-12?07:35??Csharp串口000\Csharp串口\obj\Debug\Csharp串口.Properties.Resources.resources
?????文件????????865??2015-11-11?22:40??Csharp串口000\Csharp串口\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7039??2015-11-13?22:36??Csharp串口000\Csharp串口\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????????0??2015-11-11?16:12??Csharp串口000\Csharp串口\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件??????????0??2015-11-11?16:12??Csharp串口000\Csharp串口\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件??????????0??2015-11-11?16:12??Csharp串口000\Csharp串口\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????目錄??????????0??2015-11-11?16:12??Csharp串口000\Csharp串口\obj\Debug\TempPE
?????目錄??????????0??2015-11-14?00:03??Csharp串口000\Csharp串口\obj\Debug
?????目錄??????????0??2015-11-11?22:39??Csharp串口000\Csharp串口\obj
?????文件????????524??2015-11-11?16:12??Csharp串口000\Csharp串口\Program.cs
?????文件???????1348??2015-11-11?16:12??Csharp串口000\Csharp串口\Properties\AssemblyInfo.cs
............此處省略12個文件信息
- 上一篇:支票管理系統源碼(附數據庫)
- 下一篇:C# 紙飛機大作戰 游戲源碼
評論
共有 條評論