資源簡介
完整的RS232串口通訊示例

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.IO.Ports;
using?System.Text;
using?System.Threading;
using?System.Windows.Forms;
namespace?RS232串口通訊CSharp
{
????public?partial?class?frmTest?:?Form
????{
????????public?frmTest()
????????{
????????????InitializeComponent();
????????}
????????///?
????????///?SerialPort串行端口
????????///?
????????private?SerialPort?Com_SerialPort?=?new?SerialPort();
????????///?
????????///?是否沒有執行完invoke相關操作
????????///?
????????private?bool?IsListening?=?false;
????????///?
????????///?是否正在關閉串口,執行Application.DoEvents,并阻止再次invoke
????????///?
????????private?bool?IsClosing?=?false;
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????//MCJS50.ReadValue(Com_SerialPort);
????????????//MessageBox.Show(MCJS50.intJs.ToString());
???????????
????????}
????????void?Com_SerialPort_DataReceived(object?sender?SerialDataReceivedEventArgs?e)
????????{
????????????if?(IsClosing)?{?return;?}
????????????try
????????????{
????????????????if?(Com_SerialPort.BytesToRead?<=?0)?return;
????????????????Thread.Sleep(50);
????????????????IsListening?=?true;
????????????????int?intBytesToRead?=?Com_SerialPort.BytesToRead;
????????????????byte[]?buffer?=?new?byte[intBytesToRead];
????????????????Com_SerialPort.Read(buffer?0?intBytesToRead);
????????????????if?(buffer.Length?>=?6)
????????????????{
????????????????????if?(buffer[0]?==?0xAC?&&?buffer[buffer.Length?-?1]?==?0x84)
????????????????????{
????????????????????????//讀計數值
????????????????????????string?strValue?=?buffer[5].ToString(“X2“)?+?“?“?+?buffer[6].ToString(“X2“)?+?“?“?+?buffer[7].ToString(“X2“)?+?“?“?+?buffer[8].ToString(“X2“)?+?“?“?+?buffer[9].ToString(“X2“)?+?“?“?+?buffer[10].ToString(“X2“);
????????????????????????Int64?intValue?=?Int64.Parse(buffer[5].ToString()?+?buffer[6].ToString()?+?buffer[7].ToString()?+?buffer[8].ToString()?+?buffer[9].ToString()?+?buffer[10].ToString());
????????????????????????this.Invoke((EventHandler)(delegate
????????????????????????{
????????????????????????????this.label1.Text?=?intValue.ToString();
????????????????????????}));
????????????????????}
????????????????????else
????????????????????{
????????????????????????buffer?=?null;
????????????????????}
????????????????}
????????????}
????????????finally
????????????{
????????????????IsListening?=?false;
????????????}
????????}
????????private?void?frmTest_Load(object?sender?EventArgs?e)
????????{
????????????Com_SerialPort?=?new?SerialPort();
????????????MCJS50.SetSerialPort(Com_SerialPort?IsClosing?IsListening);
????????????Com_SerialPort.DataReceived?+=?Com_SerialPort_DataReceived;
????????}
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
????????????MCJS50.ReadValue(Com_SerialPort);
????????}
????}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????58880??2016-09-21?16:18??RS232串口通訊CSharp\.vs\RS232串口通訊CSharp\v14\.suo
?????文件????????157??2016-09-07?16:06??RS232串口通訊CSharp\RS232串口通訊CSharp\App.config
?????文件??????35840??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\RS232串口通訊CSharp.dll
?????文件????????157??2016-09-07?16:06??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\RS232串口通訊CSharp.dll.config
?????文件??????54784??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\RS232串口通訊CSharp.pdb
?????文件??????21680??2016-09-19?08:30??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\RS232串口通訊CSharp.vshost.exe
?????文件????????157??2016-09-07?16:06??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\RS232串口通訊CSharp.vshost.exe.config
?????文件????????490??2015-06-04?12:48??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\RS232串口通訊CSharp.vshost.exe.manifest
?????文件?????180904??2013-07-09?11:04??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\System.Net.Http.dll
?????文件?????148528??2015-11-02?01:02??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\System.Net.Http.xm
?????文件??????17576??2013-07-09?11:34??RS232串口通訊CSharp\RS232串口通訊CSharp\bin\Debug\zh-Hans\System.Net.Http.resources.dll
?????文件???????3088??2016-09-07?17:28??RS232串口通訊CSharp\RS232串口通訊CSharp\frmTest.cs
?????文件???????3299??2016-09-07?17:27??RS232串口通訊CSharp\RS232串口通訊CSharp\frmTest.Designer.cs
?????文件???????5817??2016-09-07?17:27??RS232串口通訊CSharp\RS232串口通訊CSharp\frmTest.resx
?????文件???????1788??2016-09-07?16:39??RS232串口通訊CSharp\RS232串口通訊CSharp\frmUserVerification.cs
?????文件???????4930??2016-09-07?16:27??RS232串口通訊CSharp\RS232串口通訊CSharp\frmUserVerification.Designer.cs
?????文件???????5817??2016-09-07?16:27??RS232串口通訊CSharp\RS232串口通訊CSharp\frmUserVerification.resx
?????文件???????3203??2016-09-20?10:54??RS232串口通訊CSharp\RS232串口通訊CSharp\MCJS50.cs
?????文件??????18783??2016-09-08?10:40??RS232串口通訊CSharp\RS232串口通訊CSharp\MCJS50_Rs232.cs
?????文件??????46603??2016-09-07?18:23??RS232串口通訊CSharp\RS232串口通訊CSharp\MCJS50_Rs232.Designer.cs
?????文件???????6414??2016-09-07?18:23??RS232串口通訊CSharp\RS232串口通訊CSharp\MCJS50_Rs232.resx
?????文件???????2410??2016-09-13?17:07??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????6243??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????2423??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.csproj.FileListAbsolute.txt
?????文件????????986??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.csproj.GenerateResource.Cache
?????文件??????35840??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.dll
?????文件????????180??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.frmTest.resources
?????文件????????180??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.frmUserValidating.resources
?????文件????????180??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.MCJS50_Rs232.resources
?????文件??????54784??2016-09-20?10:55??RS232串口通訊CSharp\RS232串口通訊CSharp\obj\Debug\RS232串口通訊CSharp.pdb
............此處省略29個文件信息
評論
共有 條評論