資源簡介
vs2005環境下利用frame.net 提供的serialport類

代碼片段和文件信息
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;
namespace?RS232
{
????public?partial?class?fclsRS232Tester?:?Form
????{
????????string?InputData?=?String.Empty;
????????
????????//?This?delegate?enables?asynchronous?calls?for?setting
????????//?the?text?property?on?a?TextBox?control:
????????delegate?void?SetTextCallback(string?text);
?
????????public?fclsRS232Tester()
????????{
????????????InitializeComponent();
????????????//?Nice?methods?to?browse?all?available?ports:
????????????string[]?ports?=?SerialPort.GetPortNames();
????????????//?Add?all?port?names?to?the?combo?box:
????????????foreach?(string?port?in?ports)
????????????{
????????????????cmbComSelect.Items.Add(port);
????????????}
????????}
????????private?void?cmbComSelect_SelectionchangeCommitted(object?sender?EventArgs?e)
????????{
????????????if?(port.IsOpen)?port.Close();
????????????port.PortName?=?cmbComSelect.SelectedItem.ToString();
????????????stsStatus.Text?=?port.PortName?+?“:?96008N1“;
????????????//?try?to?open?the?selected?port:
????????????try
????????????{
????????????????port.Open();
????????????}
????????????//?give?a?message?if?the?port?is?not?available:
????????????catch
????????????{
????????????????MessageBox.Show(“Serial?port?“?+?port.PortName?+?“?cannot?be?opened!“?“RS232?tester“?MessageBoxButtons.OK?MessageBoxIcon.Warning);
????????????????cmbComSelect.SelectedText?=?““;
????????????????stsStatus.Text?=?“Select?serial?port!“;
????????????}
????????}
????????private?void?btnSend_Click(object?sender?EventArgs?e)
????????{
????????????if?(port.IsOpen)?port.WriteLine(txtOut.Text);
????????????else?MessageBox.Show(“Serial?port?is?closed!“?“RS232?tester“?MessageBoxButtons.OK?MessageBoxIcon.Error);
????????????txtOut.Clear();
????????}
????????private?void?btnClear_Click(object?sender?EventArgs?e)
????????{
????????????txtIn.Clear();
????????}
????????private?void?port_DataReceived_1(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????InputData?=?port.ReadExisting();
????????????if?(InputData?!=?String.Empty)
????????????{
?//?????????????txtIn.Text?=?InputData;???//?because?of?different?threads?this?does?not?work?properly?!!
????????????????SetText(InputData);
????????????}
????????}
????????/*
????????To?make?a?thread-safe?call?a?Windows?Forms?control:
????????1.??Query?the?control‘s?InvokeRequired?property.
????????2.??If?InvokeRequired?returns?true??call?Invoke?with?a?delegate?that?makes?the?actual?call?to?the?control.
????????3.??If?InvokeRequired?returns?false?call?the?control?directly.
????????In?the?following?code?example?this?logic?is?implemented?in?a?utility?method?called?SetText.?
????????A?delegate?type?named?SetTextDelegate?encapsulates?the?SetText?method.?
????????When?the?TextBox?control‘s?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????24576??2009-11-07?23:11??RS232_C#\RS232\bin\Debug\RS232.exe
?????文件??????32256??2009-11-07?23:11??RS232_C#\RS232\bin\Debug\RS232.pdb
?????文件???????5632??2005-12-08?14:51??RS232_C#\RS232\bin\Debug\RS232.vshost.exe
?????文件??????24576??2007-01-22?14:33??RS232_C#\RS232\bin\Release\RS232.exe
?????文件???????5632??2005-09-23?06:56??RS232_C#\RS232\bin\Release\RS232.vshost.exe
?????文件???????4446??2009-11-07?23:11??RS232_C#\RS232\Form1.cs
?????文件???????8602??2009-10-18?18:49??RS232_C#\RS232\Form1.Designer.cs
?????文件???????6207??2009-10-18?18:49??RS232_C#\RS232\Form1.resx
?????文件????????842??2009-10-18?18:49??RS232_C#\RS232\obj\Debug\RS232.csproj.GenerateResource.Cache
?????文件??????24576??2009-11-07?23:11??RS232_C#\RS232\obj\Debug\RS232.exe
?????文件????????180??2009-10-18?18:49??RS232_C#\RS232\obj\Debug\RS232.fclsRS232Tester.resources
?????文件??????32256??2009-11-07?23:11??RS232_C#\RS232\obj\Debug\RS232.pdb
?????文件????????180??2009-10-18?16:35??RS232_C#\RS232\obj\Debug\RS232.Properties.Resources.resources
?????文件????????264??2009-10-10?10:07??RS232_C#\RS232\obj\RS232.csproj.FileList.txt
?????文件????????688??2009-11-11?17:24??RS232_C#\RS232\obj\RS232.csproj.FileListAbsolute.txt
?????文件????????482??2007-01-09?11:09??RS232_C#\RS232\Program.cs
?????文件???????1262??2007-01-09?11:08??RS232_C#\RS232\Properties\AssemblyInfo.cs
?????文件???????2837??2007-01-09?11:08??RS232_C#\RS232\Properties\Resources.Designer.cs
?????文件???????5612??2007-01-09?11:08??RS232_C#\RS232\Properties\Resources.resx
?????文件???????1088??2007-01-09?11:08??RS232_C#\RS232\Properties\Settings.Designer.cs
?????文件????????249??2007-01-09?11:08??RS232_C#\RS232\Properties\Settings.settings
?????文件???????3217??2007-01-09?11:19??RS232_C#\RS232\RS232.csproj
?????文件????????904??2007-01-09?11:08??RS232_C#\RS232.sln
????..A..H.?????18432??2009-11-11?17:31??RS232_C#\RS232.suo
?????目錄??????????0??2009-10-10?16:19??RS232_C#\RS232\obj\Debug\Refactor
?????目錄??????????0??2009-10-10?16:19??RS232_C#\RS232\obj\Debug\TempPE
?????目錄??????????0??2009-10-10?16:19??RS232_C#\RS232\obj\Release\TempPE
?????目錄??????????0??2009-10-10?16:19??RS232_C#\RS232\bin\Debug
?????目錄??????????0??2009-10-10?16:19??RS232_C#\RS232\bin\Release
?????目錄??????????0??2009-11-07?23:11??RS232_C#\RS232\obj\Debug
............此處省略9個文件信息
評論
共有 條評論