-
大小: 48.55 KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2024-07-21
- 語言: C#
- 標(biāo)簽: C#??串口通信??serialPort??VS2010??
資源簡(jiǎn)介
開發(fā)環(huán)境VS2010,C#編寫的串口通信程序,利用了serialPort組件,測(cè)試過可以使用。
代碼片段和文件信息
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.Threading;
namespace?SerialPortTry
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????SerialPort?sp?=?new?SerialPort();
????????bool?cxstate?=?true;
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????
????????????if?(sp.IsOpen)
????????????{
????????????????sp.Close();
????????????}
????????????label7.BackColor?=?Color.Black;
????????????comboBox1.SelectedIndex?=?0;
????????????comboBox2.SelectedIndex?=?0;
????????????comboBox3.SelectedIndex?=?0;
????????????comboBox4.SelectedIndex?=?0;
????????????comboBox5.SelectedIndex?=?0;
????????????CheckForIllegalCrossThreadCalls?=?false;
????????????
????????????
????????}
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????sp.PortName?=?comboBox1.SelectedItem.ToString();
????????????sp.BaudRate?=?Convert.ToInt32(comboBox2.SelectedItem);
????????????sp.DataBits?=?Convert.ToInt32(comboBox3.SelectedItem);
????????????//serialPort1.StopBits?=?Convert.ToSinglecomboBox4.SelectedItem);
????????????label6.Text?=?“您選用的串口是:“+sp.PortName;
????????????if?(sp.IsOpen)
????????????{
????????????????btn_send.Enabled?=?true;
????????????????button3.Enabled?=?true;
????????????}
????????????else
????????????{
????????????????btn_send.Enabled?=?false;
????????????????button3.Enabled?=?false;
????????????}
????????}
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
????????????if?(sp.IsOpen)
????????????{
????????????????sp.Close();
????????????????btn_send.Enabled?=?false;
????????????????button3.Enabled?=?false;
????????????????label7.BackColor?=?Color.Black;
????????????????label6.Text?=?“串口已關(guān)閉...“;
????????????????button2.Text?=?“打開串口“;
????????????}
????????????else
????????????{
????????????????
????????????????sp.Open();
????????????????btn_send.Enabled?=?true;
????????????????button3.Enabled?=?true;
????????????????//sp.DtrEnable?=?true;
????????????????//sp.RtsEnable?=?true;
????????????????label7.BackColor?=?Color.Red;
????????????????label6.Text?=?“串口已打開...“;
????????????????button2.Text?=?“關(guān)閉串口“;
????????????}
????????????
????????}
????????
????????
????????private?void?btn_send_Click(object?sender?EventArgs?e)
????????{
?????????????sp.WriteLine(textBox1.Text);
?????????????label6.Text?=?“發(fā)送成功“;
????????}
????????
????????private?void?button3_Click(object?sender?EventArgs?e)
????????{
????????????/*//===================================================查詢方式1
????????????if?(button3.Text?==?“開始接收“)
?????????????{
?????????????????button3.Text?=?“停止接收“;
?????????????????textBox2.Text?=?““;
?????????????????wh
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件??????14336??2012-07-04?15:14??SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.exe
?????文件??????28160??2012-07-04?15:14??SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.pdb
?????文件??????11600??2012-07-04?22:20??SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.vshost.exe
?????文件????????490??2010-03-17?22:39??SerialPortTry\SerialPortTry\bin\Debug\SerialPortTry.vshost.exe.manifest
?????文件???????5358??2012-07-01?11:11??SerialPortTry\SerialPortTry\Form1.cs
?????文件??????13588??2012-07-01?10:19??SerialPortTry\SerialPortTry\Form1.Designer.cs
?????文件???????6190??2012-07-01?10:19??SerialPortTry\SerialPortTry\Form1.resx
?????文件???????4440??2012-06-26?22:55??SerialPortTry\SerialPortTry\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6235??2012-07-04?22:20??SerialPortTry\SerialPortTry\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件????????268??2012-07-01?10:19??SerialPortTry\SerialPortTry\obj\x86\Debug\GenerateResource.read.1.tlog
?????文件????????658??2012-07-01?10:19??SerialPortTry\SerialPortTry\obj\x86\Debug\GenerateResource.write.1.tlog
?????文件???????7897??2012-06-26?23:01??SerialPortTry\SerialPortTry\obj\x86\Debug\ResolveAssemblyReference.cache
?????文件????????778??2012-07-04?22:20??SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.csproj.FileListAbsolute.txt
?????文件??????14336??2012-07-04?15:14??SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.exe
?????文件????????180??2012-07-01?10:19??SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.Form1.resources
?????文件??????28160??2012-07-04?15:14??SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.pdb
?????文件????????180??2012-06-26?11:43??SerialPortTry\SerialPortTry\obj\x86\Debug\SerialPortTry.Properties.Resources.resources
?????文件????????494??2012-06-26?11:31??SerialPortTry\SerialPortTry\Program.cs
?????文件???????1406??2012-06-26?11:31??SerialPortTry\SerialPortTry\Properties\AssemblyInfo.cs
?????文件???????2876??2012-06-26?11:31??SerialPortTry\SerialPortTry\Properties\Resources.Designer.cs
?????文件???????5612??2012-06-26?11:31??SerialPortTry\SerialPortTry\Properties\Resources.resx
?????文件???????1098??2012-06-26?11:31??SerialPortTry\SerialPortTry\Properties\Settings.Designer.cs
?????文件????????249??2012-06-26?11:31??SerialPortTry\SerialPortTry\Properties\Settings.settings
?????文件???????3685??2012-06-26?11:43??SerialPortTry\SerialPortTry\SerialPortTry.csproj
?????文件????????881??2012-06-26?11:31??SerialPortTry\SerialPortTry.sln
????..A..H.?????19968??2012-07-04?22:20??SerialPortTry\SerialPortTry.suo
?????目錄??????????0??2012-06-26?11:31??SerialPortTry\SerialPortTry\obj\x86\Debug\TempPE
?????目錄??????????0??2012-07-04?15:14??SerialPortTry\SerialPortTry\obj\x86\Debug
?????目錄??????????0??2012-06-26?11:43??SerialPortTry\SerialPortTry\bin\Debug
?????目錄??????????0??2012-06-26?11:31??SerialPortTry\SerialPortTry\obj\x86
............此處省略8個(gè)文件信息
評(píng)論
共有 條評(píng)論