資源簡(jiǎn)介
c# 串口通信源代碼c# 串口通信源代碼c# 串口通信源代碼

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
using?System.IO.Ports;
using?System.Windows.Forms;
namespace?SComm
{
????public??class?Com
????{
????????private?SerialPort?sport;
???????///?
????????///?設(shè)置發(fā)送緩沖區(qū)大小
???????///?
????????public?int?outBufferSize
????????{
????????????set
????????????{
????????????????sport.WriteBufferSize?=?value;
????????????}
????????}
????????///?
????????///?設(shè)置接收緩沖區(qū)大小
????????///?
????????public?int?inBufferSize
????????{
????????????set
????????????{
????????????????sport.ReadBufferSize?=?value;
????????????}
????????}
????????public?Com()
????????{
????????????sport?=?new?SerialPort();
????????}
????????///?
????????///?初始化
????????///?
????????///?端口名稱(chēng)
????????///?波特率
????????///?校驗(yàn)方式
????????public?Com(string?portNameint?rateParity?parity)
????????{
????????????sport?=?new?SerialPort(portNamerateparity);???
????????}
????????public?bool?InitCom()
????????{
????????????if?(sport.IsOpen)
????????????????return?true;
????????????else
????????????{
????????????????try
????????????????{
????????????????????sport.Open();?
????????????????????return?true;
????????????????}
????????????????catch?(Exception?e)
????????????????{
????????????????????return?false;
????????????????}
????????????}
????????}
????????public?void?Close()
????????{
????????????if?(sport.IsOpen)
????????????????sport.Close();
????????}
????????///?
????????///?串口設(shè)置并打開(kāi)
????????///?
????????///?
????????///?
????????///?
????????///?
????????public?bool?InitCom(string?portName?int?rate?Parity?parity)
????????{
????????????if?(sport.IsOpen)
????????????????sport.Close();
????????????sport.BaudRate?=?rate;
????????????sport.PortName?=?portName;
????????????sport.Parity?=?parity;
????????????try
????????????{
????????????????sport.Open();
????????????????return?true;
????????????}
????????????catch?(Exception?e)
????????????{
????????????????return?false;
????????????}
????????}
??????///?
??????///?發(fā)送字節(jié)
??????///?
??????///?要發(fā)送的字節(jié)
??????///?發(fā)送字節(jié)的數(shù)量
??????///?
????????public?bool?write(byte[]?writeBytesint?count)
????????{
????????????if?(InitCom())
????????????{
????????????????try
????????????????{
????????????????????sport.Write(writeBytes?0?count);
????????????????????return?true;
????????????????}
????????????????catch?(Exception?e)
????????????????{
????????????????????return?false;
????????????????}
????????????}
????????????return?false;
????????}
????????///?
????????///?發(fā)送字符串
????????///?
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????1048576??2010-01-22?18:00??SCommnet\Databa
?????文件????1048576??2010-01-22?18:00??SCommnet\Databa
?????文件????????270??2010-01-22?10:42??SCommnet\SComm\App.config
?????文件??????10678??2010-01-18?14:54??SCommnet\SComm\bin\Debug\app.publish\Application?Files\SComm_1_0_0_1\Resource?Files\Toolbar.bmp.deploy
?????文件??????52654??2010-01-18?14:47??SCommnet\SComm\bin\Debug\app.publish\Application?Files\SComm_1_0_0_1\Resource?Files\未標(biāo)題-10.bmp.deploy
?????文件?????921654??2010-01-18?14:21??SCommnet\SComm\bin\Debug\app.publish\Application?Files\SComm_1_0_0_1\Resource?Files\順泰圖標(biāo).bmp.deploy
?????文件????1005056??2010-01-20?12:26??SCommnet\SComm\bin\Debug\app.publish\Application?Files\SComm_1_0_0_1\SComm.exe.deploy
?????文件???????8435??2010-01-20?12:26??SCommnet\SComm\bin\Debug\app.publish\Application?Files\SComm_1_0_0_1\SComm.exe.manifest
?????文件???????5364??2010-01-20?12:26??SCommnet\SComm\bin\Debug\app.publish\SComm.application
?????文件?????461824??2010-01-20?12:26??SCommnet\SComm\bin\Debug\app.publish\setup.exe
?????文件???????1555??2010-01-22?17:42??SCommnet\SComm\bin\Debug\SComm.application
?????文件????1011712??2010-01-22?17:41??SCommnet\SComm\bin\Debug\SComm.exe
?????文件????????270??2010-01-22?10:42??SCommnet\SComm\bin\Debug\SComm.exe.config
?????文件???????6487??2010-01-22?17:42??SCommnet\SComm\bin\Debug\SComm.exe.manifest
?????文件??????62976??2010-01-22?17:41??SCommnet\SComm\bin\Debug\SComm.pdb
?????文件??????14328??2010-01-20?08:42??SCommnet\SComm\bin\Debug\SComm.vshost.exe
?????文件????????490??2007-07-21?01:33??SCommnet\SComm\bin\Debug\SComm.vshost.exe.manifest
?????文件??????14848??2010-01-22?11:00??SCommnet\SComm\bin\Debug\Shuntai.dll
?????文件??????46592??2010-01-22?11:00??SCommnet\SComm\bin\Debug\Shuntai.pdb
?????文件???????6390??2010-01-22?16:42??SCommnet\SComm\Com.cs
?????文件???????1056??2010-01-21?14:59??SCommnet\SComm\ComFile.cs
?????文件???????5457??2010-01-20?11:19??SCommnet\SComm\CommWithARM.cs
?????文件???????7708??2010-01-22?17:41??SCommnet\SComm\DataForm.cs
?????文件??????18228??2010-01-22?17:41??SCommnet\SComm\DataForm.Designer.cs
?????文件???????6224??2010-01-22?17:41??SCommnet\SComm\DataForm.resx
?????文件????????939??2010-01-22?14:38??SCommnet\SComm\MainFrm.cs
?????文件???????6370??2010-01-22?14:38??SCommnet\SComm\MainFrm.Designer.cs
?????文件???????6010??2010-01-22?14:38??SCommnet\SComm\MainFrm.resx
?????文件???????3764??2010-01-22?11:00??SCommnet\SComm\obj\Debug\ResolveAssemblyReference.cache
?????文件???????1555??2010-01-22?17:42??SCommnet\SComm\obj\Debug\SComm.application
............此處省略76個(gè)文件信息
評(píng)論
共有 條評(píng)論