資源簡介
C#串口通訊例子。演示了C#對串口的讀寫操作。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
namespace?CommPort
{
????public?partial?class?Form1?:?Form
????{
????????private?int?txd;
????????private?int?rxd;
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????txd?=?0;
????????????rxd?=?0;
????????????string[]?ports=?System.IO.Ports.SerialPort.GetPortNames();
????????????mycomm.ReadTimeout?=?32;
????????????int?i?=?0;
????????????foreach?(string?port?in?ports)
????????????{
????????????????combo1.Items.Add(port);
????????????????i++;
????????????}
????????????if?(i?>?0)
????????????{
????????????????combo1.SelectedIndex?=?0;
????????????????if(mycomm.IsOpen)
????????????????{
????????????????????mycomm.Close();
????????????????}
????????????????mycomm.PortName?=?combo1.SelectedItem.ToString();
????????????????comminfo.Text?=?combo1.SelectedItem.ToString()?+?“960081“;
????????????????try
????????????????{
????????????????????mycomm.Open();
????????????????????openbtn.Text?=?“關閉串口“;
????????????????}
????????????????catch
????????????????{
????????????????????MessageBox.Show(“沒發現次串口或串口已經在使用“);
????????????????}
????????????}
????????}
????????private?void?Form1_Leave(object?sender?EventArgs?e)
????????{
????????????if(mycomm.IsOpen)
????????????{
????????????????mycomm.Close();
????????????}
????????}
????????private?void?openbtn_Click(object?sender?EventArgs?e)
????????{
????????????if(openbtn.Text?==“關閉串口“)
????????????{
????????????????openbtn.Text?=?“打開串口“;
????????????????if(mycomm.IsOpen)
????????????????{
????????????????????mycomm.Close();
????????????????}
????????????}
????????????else
????????????{
????????????????try
????????????????{
????????????????????if(mycomm.IsOpen)
????????????????????{
????????????????????????mycomm.Close();
????????????????????}
????????????????????mycomm.PortName?=combo1.SelectedItem.ToString();
????????????????????mycomm.Open();
????????????????????openbtn.Text?=?“關閉串口“;
????????????????????comminfo.Text?=?combo1.SelectedItem.ToString()?+?“960081“;
????????????????}
????????????????catch
????????????????{
????????????????????MessageBox.Show(“沒發現次串口或串口已經在使用“);
????????????????????openbtn.Text?=?“打開串口“;
????????????????}
????????????????
????????????}
????????}
????????private?void?send_Click(object?sender?EventArgs?e)
????????{
????????????string?str?=?sendtext.Text;
????????????if?(mycomm.IsOpen)
????????????{
????????????????mycomm.WriteLine(str);
????????????????txd?=?txd?+?str.Length;
????????????????tds.Text?=?txd.ToString();
????????????}
????????}
????????private?void?mycomm_DataReceived(object?sender?System.IO.Ports.SerialDataReceivedEventArgs?e)
????????{
????????????string?str?=?mycomm.ReadExisting();
?????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\
?????目錄???????????0??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\_UpgradeReport_Files\
?????文件????????3348??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\_UpgradeReport_Files\UpgradeReport.css
?????文件???????12505??2008-01-11?01:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\_UpgradeReport_Files\UpgradeReport.xslt
?????文件??????????69??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\_UpgradeReport_Files\UpgradeReport_Minus.gif
?????文件??????????71??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\_UpgradeReport_Files\UpgradeReport_Plus.gif
?????目錄???????????0??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\
?????文件?????????913??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort.sln
?????文件???????12288??2008-06-18?18:14??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort.suo
?????目錄???????????0??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\
?????文件????????3292??2008-06-18?17:30??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\CommPort.csproj
?????文件????????4155??2008-06-18?18:11??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Form1.cs
?????文件???????10515??2008-06-18?18:10??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Form1.Designer.cs
?????文件????????9806??2008-06-18?18:10??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Form1.resx
?????文件?????????467??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Program.cs
?????目錄???????????0??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Properties\
?????文件????????1178??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Properties\AssemblyInfo.cs
?????文件????????2870??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Properties\Resources.Designer.cs
?????文件????????5612??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Properties\Resources.resx
?????文件????????1091??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Properties\Settings.Designer.cs
?????文件?????????249??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\Properties\Settings.settings
?????文件??????????46??2008-06-18?17:30??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\Backup\CommPort\說明.txt
?????文件?????????914??2010-03-28?15:33??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort.sln
?????文件???????15360??2010-03-28?18:12??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort.suo
?????目錄???????????0??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\
?????目錄???????????0??2008-06-18?17:25??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\bin\
?????目錄???????????0??2010-03-28?15:34??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\bin\Debug\
?????文件???????15872??2010-03-28?15:45??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\bin\Debug\CommPort.exe
?????文件???????32256??2010-03-28?15:45??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\bin\Debug\CommPort.pdb
?????文件???????14328??2010-03-28?16:18??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\bin\Debug\CommPort.vshost.exe
?????文件?????????490??2009-06-11?05:14??C#寫的串口通訊程序(可找到并打開電腦上所有串口)\CommPort\CommPort\bin\Debug\CommPort.vshost.exe.manifest
............此處省略25個文件信息
評論
共有 條評論