资源简介
基于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滤波器
评论
共有 条评论