資源簡介
基于c#的串口通信獲取溫濕度傳感器數據,使用單片機芯片為CC2530

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.IO.Ports;
using?System.Threading;
namespace?CommAssist
{
????public?partial?class?Form1?:?Form
????{
????????Byte[]?OutputBuf?=?new?Byte[128];
????????SerialPort?Comm?=?new?SerialPort(“com3“?1200?Parity.None?8?StopBits.One);??????//初始化串口設置??默認打開串口三^_^因為其他的串口被我溺死了。。。
????????public?Form1()
????????{
????????????InitializeComponent();
????????????comboBox1.SelectedItem?=?“com3“;
????????????Comm.DataReceived?+=?new?SerialDataReceivedEventHandler(Comm_DataReceived);
????????????try
????????????{
????????????????Comm.Open();
????????????}
????????????catch?(InvalidOperationException?ex)
????????????{
????????????????MessageBox.Show(ex.ToString());
????????????}
?????}
????????
????????private?void?BtnTest_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????OutputBuf[0]?=?0x7E;????????????//發送一串數據給下位機
????????????????OutputBuf[1]?=?0x01;
????????????????OutputBuf[2]?=?0x60;
????????????????OutputBuf[3]?=?0x61;
????????????????OutputBuf[4]?=?0x7D;
????????????????Comm.Write(OutputBuf05);
????????????}
????????????catch?(TimeoutException?ex)?????????//異常處理
????????????{
????????????????MessageBox.Show(ex.ToString());
????????????}
????????}
????????void?Comm_DataReceived(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????
????????????Byte[]?InputBuf?=?new?Byte[128];
????????????try
????????????{???????????????
????????????????string?strRD?=?Comm.ReadTo(“}“);????????????????????????????????//讀取緩沖區的數據直到“}”即0x7D為結束符
????????????????//InputBuf?=?UnicodeEncoding.Default.GetBytes(strRD);?????????????//將得到的數據轉換成byte的格式
???????????????
????????????????textBox1.Text?+=?strRD;
????????????????
???????????}
????????????catch?(TimeoutException?ex)?????????//超時處理
????????????{
????????????????MessageBox.Show(ex.ToString());
????????????}??????
????????}
????????private?void?comboBox1_SelectedIndexChanged(object?sender?EventArgs?e)?????//選擇串口的處理過程
????????{
????????????bool?b_OpenFlag?=?Comm.IsOpen;
????????????if?(b_OpenFlag?==?true)
????????????{
????????????????Comm.Close();
????????????????Comm.PortName?=?comboBox1.SelectedItem.ToString();
????????????????Comm.Open();
????????????}
????????????else
????????????{
????????????????Comm.PortName?=?comboBox1.SelectedItem.ToString();
????????????}
?????????}
????????private?void?OpenSerialPort_Click(object?sender?EventArgs?e)???????????????//點擊“打開串口”按鈕的操作
????????{
????????????if?(OpenSerialPort.Text?==?“關閉串口“)
????????????{
????????????????Comm.Close();
????????????????OpenSerialPort.Text?=?“打開串口“;
????????????}
????????????else
????????????{
????????????????Comm.Open();
????????????????OpenSerialPort.Text?=?“關閉串口“;
????????????}
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????3227??2009-02-05?10:27??commassist\Backup\CommAssist\CommAssist.csproj
?????文件???????3197??2009-02-09?10:44??commassist\Backup\CommAssist\Form1.cs
?????文件???????5037??2009-02-09?10:44??commassist\Backup\CommAssist\Form1.Designer.cs
?????文件???????6213??2009-02-09?10:44??commassist\Backup\CommAssist\Form1.resx
?????文件????????469??2009-02-05?10:16??commassist\Backup\CommAssist\Program.cs
?????文件???????1192??2009-02-05?10:16??commassist\Backup\CommAssist\Properties\AssemblyInfo.cs
?????文件???????2875??2009-02-05?10:16??commassist\Backup\CommAssist\Properties\Resources.Designer.cs
?????文件???????5612??2009-02-05?10:16??commassist\Backup\CommAssist\Properties\Resources.resx
?????文件???????1094??2009-02-05?10:16??commassist\Backup\CommAssist\Properties\Settings.Designer.cs
?????文件????????249??2009-02-05?10:16??commassist\Backup\CommAssist\Properties\Settings.settings
?????文件????????919??2009-02-05?10:16??commassist\Backup\CommAssist.sln
?????文件??????23040??2009-03-31?16:32??commassist\Backup\CommAssist.suo
?????文件???????3435??2009-06-18?11:04??commassist\Backup1\CommAssist\CommAssist.csproj
?????文件???????3696??2009-06-23?17:47??commassist\Backup1\CommAssist\Form1.cs
?????文件???????5037??2009-02-09?10:44??commassist\Backup1\CommAssist\Form1.Designer.cs
?????文件???????6213??2009-02-09?10:44??commassist\Backup1\CommAssist\Form1.resx
?????文件????????469??2009-02-05?10:16??commassist\Backup1\CommAssist\Program.cs
?????文件???????1192??2009-02-05?10:16??commassist\Backup1\CommAssist\Properties\AssemblyInfo.cs
?????文件???????2844??2009-06-18?11:04??commassist\Backup1\CommAssist\Properties\Resources.Designer.cs
?????文件???????5612??2009-02-05?10:16??commassist\Backup1\CommAssist\Properties\Resources.resx
?????文件???????1108??2009-06-18?11:04??commassist\Backup1\CommAssist\Properties\Settings.Designer.cs
?????文件????????249??2009-02-05?10:16??commassist\Backup1\CommAssist\Properties\Settings.settings
?????文件????????920??2009-06-18?11:04??commassist\Backup1\CommAssist.sln
?????文件??????23040??2009-06-23?17:49??commassist\Backup1\CommAssist.suo
?????文件??????12800??2015-11-22?09:48??commassist\CommAssist\bin\Debug\app.publish\Application?Files\CommAssist_1_0_0_0\CommAssist.exe.deploy
?????文件???????6716??2015-11-22?09:48??commassist\CommAssist\bin\Debug\app.publish\Application?Files\CommAssist_1_0_0_0\CommAssist.exe.manifest
?????文件???????5394??2015-11-22?09:48??commassist\CommAssist\bin\Debug\app.publish\CommAssist.application
?????文件?????495056??2015-11-22?09:48??commassist\CommAssist\bin\Debug\app.publish\setup.exe
?????文件???????1633??2015-11-22?10:40??commassist\CommAssist\bin\Debug\CommAssist.application
?????文件??????12800??2015-11-22?10:40??commassist\CommAssist\bin\Debug\CommAssist.exe
............此處省略80個文件信息
- 上一篇:C#&Csgl進行OpenGL編程
- 下一篇:Wallis濾波器
評論
共有 條評論