資源簡介
通過串口進行圖片的傳遞,圖片是以byte[]形式傳遞的,并能顯示出傳遞后的圖片

代碼片段和文件信息
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?WindowsFormsApplication21
{
????public?partial?class?Form1?:?Form
????{
????????private?StringBuilder?builder?=?new?StringBuilder();//避免在事件處理方法中反復的創建,定義到外面。???
????????private?long?received_count?=?0;//接收計數???
????????private?long?send_count?=?0;//發送計數
????????private?bool?Listening?=?false;//是否沒有執行完invoke相關操作???
????????private?bool?closing?=?false;//是否正在關閉串口,執行Application.DoEvents,并阻止再次invoke
????????string?curFileName;
????????static?int?width;
????????static?int?height;
????????Bitmap?bmp1?=?null;
????????byte[]?d?=null;//設置緩存區用來接收完整的傳送數據
????????public?static?int?x?=?0;//與d配套使用,用來記錄每次接收緩存區里數據的個數
????????
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????//初始化下拉串口名稱列表框???
????????????string[]?ports?=?SerialPort.GetPortNames();
????????????Array.Sort(ports);
????????????comboBox1.Items.AddRange(ports);
????????????comboBox1.SelectedIndex?=?comboBox1.Items.Count?>?0???0?:?-1;
????????????comboBox2.SelectedIndex?=?comboBox2.Items.IndexOf(“9600“);
????????????//初始化SerialPort對象??????
????????????serialPort1.NewLine?=?“/r/n“;
????????????serialPort1.RtsEnable?=?true;//設置是否啟用發送請求信號
????????}
????????//該事件在一次傳送過程中會觸發多次,每次都把系統分配的緩存區里數據讀至buf數組里
????????private?void?serialPort1_DataReceived(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????if?(closing)?return;//如果正在關閉,忽略操作,直接返回,盡快的完成串口監聽線程的一次循環???
????????????try
????????????{
????????????????Listening?=?true;//設置標記,說明我已經開始處理數據,一會兒要使用系統UI的。?
????????????????int?n?=?serialPort1.BytesToRead;//先記錄下來,避免某種原因,人為的原因,操作幾次之間時間長,緩存不一致???
????????????????byte[]?buf?=?new?byte[n];//聲明一個臨時數組存儲當前來的串口數據???
????????????????received_count?+=?n;//增加接收計數???
????????????????serialPort1.Read(buf?0?n);//讀取緩沖數據???
????????????????builder.Remove(0?builder.Length);//清除字符串構造器的內容???
????????????????//因為要訪問ui資源,所以需要使用invoke方式同步ui。???
????????????????this.Invoke((EventHandler)(delegate
????????????????{
????????????????????for?(int?i?=?0;?i?????????????????????{
????????????????????????//richTextBox2.Text?=?richTextBox2.Text?+?buf[i].ToString()?+?““;
????????????????????????d[x?+?i]?=?buf[i];
????????????????????}
????????????????????x?=?x?+?n;
????????????????}));
????????????}
????????????finally
????????????{
????????????????Listening?=?false;//我用完了,ui可以關閉串口了。???
????????????}
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)//打開串口
????????{
????????????button1.Enabled?=?false;
????????????button2.Enabled?=?true;
????????????button3.Enabled?=?true;
????????????
????????????serialPort1.PortName?=?comboBox1.Tex
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????965??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21.sln
????..A..H.?????34304??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21.suo
?????文件????????506??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21\Program.cs
?????文件???????3796??2011-09-02?15:18??通過串口傳送圖片實例\WindowsFormsApplication21\WindowsFormsApplication21.csproj
?????文件???????1406??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21\Properties\AssemblyInfo.cs
?????文件???????2900??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21\Properties\Resources.Designer.cs
?????文件???????5612??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21\Properties\Resources.resx
?????文件???????1110??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21\Properties\Settings.Designer.cs
?????文件????????249??2011-09-02?14:45??通過串口傳送圖片實例\WindowsFormsApplication21\Properties\Settings.settings
?????文件???????5690??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\WindowsFormsApplication21.csproj.FileListAbsolute.txt
?????文件??????15360??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\WindowsFormsApplication21.exe
?????文件????????180??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\WindowsFormsApplication21.Form1.resources
?????文件????????180??2011-09-02?15:24??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\WindowsFormsApplication21.Properties.Resources.resources
?????文件????????847??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\WindowsFormsApplication21.csproj.GenerateResource.Cache
?????文件??????28160??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\WindowsFormsApplication21.pdb
?????文件??????15360??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.exe
?????文件??????28160??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.pdb
?????文件??????14328??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.vshost.exe
?????文件????????490??2007-07-21?01:33??通過串口傳送圖片實例\WindowsFormsApplication21\bin\Debug\WindowsFormsApplication21.vshost.exe.manifest
?????文件???????6217??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\Form1.resx
?????文件??????11625??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\Form1.Designer.cs
?????文件???????7758??2012-02-23?08:49??通過串口傳送圖片實例\WindowsFormsApplication21\Form1.cs
?????文件????????189??2012-02-23?08:51??通過串口傳送圖片實例\使用說明.txt
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\TempPE
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug\Refactor
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\obj\Debug
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\bin\Debug
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\Properties
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\obj
?????目錄??????????0??2012-02-23?08:45??通過串口傳送圖片實例\WindowsFormsApplication21\bin
............此處省略5個文件信息
- 上一篇:C#連接Oracle含源碼和數據庫
- 下一篇:模糊均值聚類算法FCM(C#)
評論
共有 條評論