91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 53KB
    文件類型: .zip
    金幣: 2
    下載: 0 次
    發(fā)布日期: 2021-05-29
  • 語(yǔ)言: C#
  • 標(biāo)簽: c#??serialport??sample??

資源簡(jiǎn)介

http://blog.csdn.net/wuyazhe/archive/2010/05/17/5598945.aspx 博客文章的配套代碼。希望能幫助到你。

資源截圖

代碼片段和文件信息

using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.IO.Ports;
using?System.Text.Regularexpressions;

namespace?SerialportSample
{
????public?partial?class?SerialportSampleForm?:?Form
????{
????????private?SerialPort?comm?=?new?SerialPort();
????????private?StringBuilder?builder?=?new?StringBuilder();//避免在事件處理方法中反復(fù)的創(chuàng)建,定義到外面。
????????private?long?received_count?=?0;//接收計(jì)數(shù)
????????private?long?send_count?=?0;//發(fā)送計(jì)數(shù)

????????public?SerialportSampleForm()
????????{
????????????InitializeComponent();
????????}

????????//窗體初始化
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????//初始化下拉串口名稱列表框
????????????string[]?ports?=?SerialPort.GetPortNames();
????????????Array.Sort(ports);
????????????comboPortName.Items.AddRange(ports);
????????????comboPortName.SelectedIndex?=?comboPortName.Items.Count?>?0???0?:?-1;
????????????comboBaudrate.SelectedIndex?=?comboBaudrate.Items.IndexOf(“9600“);

????????????//初始化SerialPort對(duì)象
????????????comm.NewLine?=?“\r\n“;
????????????comm.RtsEnable?=?true;//根據(jù)實(shí)際情況吧。

????????????//添加事件注冊(cè)
????????????comm.DataReceived?+=?comm_DataReceived;
????????}

????????void?comm_DataReceived(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????int?n?=?comm.BytesToRead;//先記錄下來(lái),避免某種原因,人為的原因,操作幾次之間時(shí)間長(zhǎng),緩存不一致
????????????byte[]?buf?=?new?byte[n];//聲明一個(gè)臨時(shí)數(shù)組存儲(chǔ)當(dāng)前來(lái)的串口數(shù)據(jù)
????????????received_count?+=?n;//增加接收計(jì)數(shù)
????????????comm.Read(buf?0?n);//讀取緩沖數(shù)據(jù)
????????????builder.Clear();//清除字符串構(gòu)造器的內(nèi)容
????????????//因?yàn)橐L問(wèn)ui資源,所以需要使用invoke方式同步ui。
????????????this.Invoke((EventHandler)(delegate
????????????{
????????????????//判斷是否是顯示為16禁止
????????????????if?(checkBoxHexView.Checked)
????????????????{
????????????????????//依次的拼接出16進(jìn)制字符串
????????????????????foreach?(byte?b?in?buf)
????????????????????{
????????????????????????builder.Append(b.ToString(“X2“)?+?“?“);
????????????????????}
????????????????}
????????????????else
????????????????{
????????????????????//直接按ASCII規(guī)則轉(zhuǎn)換成字符串
????????????????????builder.Append(Encoding.ASCII.GetString(buf));
????????????????}
????????????????//追加的形式添加到文本框末端,并滾動(dòng)到最后。
????????????????this.txGet.AppendText(builder.ToString());
????????????????//修改接收計(jì)數(shù)
????????????????labelGetCount.Text?=?“Get:“?+?received_count.ToString();
????????????}));
????????}

????????private?void?buttonOpenClose_Click(object?sender?EventArgs?e)
????????{
????????????//根據(jù)當(dāng)前串口對(duì)象,來(lái)判斷操作
????????????if?(comm.IsOpen)
????????????{
????????????????//打開(kāi)時(shí)點(diǎn)擊,則關(guān)閉串口
????????????????comm.Close();
????????????}
????????????else
????????????{
????????????????//關(guān)閉時(shí)點(diǎn)擊,則設(shè)置好端口,波特率后打開(kāi)
????????????????comm.PortName?=?comboPortName.Text;
????????????????comm.BaudRate?=?int.Parse(comboBaudrate.Text);
????????????????try
????????????????{
????????????????????comm.Open();
????????????????}
?????????

?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2010-05-16?02:19??SerialportSample\
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\
?????文件?????????890??2010-05-16?02:19??SerialportSample\SerialportSample.sln
?????文件???????19456??2010-05-17?01:20??SerialportSample\SerialportSample.suo
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\bin\
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\bin\Debug\
?????文件???????15872??2010-05-17?01:19??SerialportSample\SerialportSample\bin\Debug\SerialportSample.exe
?????文件???????26112??2010-05-17?01:19??SerialportSample\SerialportSample\bin\Debug\SerialportSample.pdb
?????文件???????11600??2010-05-17?01:00??SerialportSample\SerialportSample\bin\Debug\SerialportSample.vshost.exe
?????文件?????????490??2010-03-17?22:39??SerialportSample\SerialportSample\bin\Debug\SerialportSample.vshost.exe.manifest
?????文件????????6110??2010-05-17?01:19??SerialportSample\SerialportSample\Form1.cs
?????文件???????15770??2010-05-17?01:06??SerialportSample\SerialportSample\Form1.Designer.cs
?????文件????????5817??2010-05-17?01:06??SerialportSample\SerialportSample\Form1.resx
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\obj\
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\obj\x86\
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\obj\x86\Debug\
?????文件????????3408??2010-05-17?00:44??SerialportSample\SerialportSample\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6277??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????324??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\GenerateResource.read.1.tlog
?????文件?????????812??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\GenerateResource.write.1.tlog
?????文件?????????937??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.csproj.FileListAbsolute.txt
?????文件???????15872??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.exe
?????文件???????26112??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.pdb
?????文件?????????180??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.Properties.Resources.resources
?????文件?????????180??2010-05-17?01:19??SerialportSample\SerialportSample\obj\x86\Debug\SerialportSample.SerialportSampleForm.resources
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\obj\x86\Debug\TempPE\
?????文件?????????523??2010-05-16?03:24??SerialportSample\SerialportSample\Program.cs
?????目錄???????????0??2010-05-16?02:19??SerialportSample\SerialportSample\Properties\
?????文件????????1468??2010-05-16?02:19??SerialportSample\SerialportSample\Properties\AssemblyInfo.cs
?????文件????????2858??2010-05-16?02:19??SerialportSample\SerialportSample\Properties\Resources.Designer.cs
?????文件????????5612??2010-05-16?02:19??SerialportSample\SerialportSample\Properties\Resources.resx
............此處省略3個(gè)文件信息

評(píng)論

共有 條評(píng)論