-
大小: 290KB文件類型: .rar金幣: 2下載: 1 次發布日期: 2021-05-17
- 語言: C#
- 標簽: C#??serialPort??
資源簡介
在C#中使用serialPort控件、消息處理和DataReceived事件實時顯示數據。

代碼片段和文件信息
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.Runtime.InteropServices;
using?Nuctech.Raman.base.ComClasses;
namespace?Com1Example
{
????public?partial?class?Form1?:?Form
????{
????????public?const?int?WM_USER?=?0x400;
????????public?const?int?WM_USER_SERIALRCVMSG?=?WM_USER?+?101;???????//串口消息號
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????private?static?byte[]?byDataRcv?=?new?byte[128];
????????private?static?IntPtr?m_hwnd?=?IntPtr.Zero;
????????//消息發送API
????????[DllImport(“User32.dll“?EntryPoint?=?“PostMessage“)]
????????public?static?extern?int?PostMessage(
????????????IntPtr?hWnd????????//?信息發往的窗口的句柄
????????????int?Msg????????????//?消息ID
????????????int?wParam?????????//?參數1
????????????int?lParam????????????//?參數2
????????);
????????
????????private?void?Form1_Load(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????cmb_COM.SelectedIndex?=?0;
????????????????serialPort1.DataReceived?+=?new?SerialDataReceivedEventHandler(DataReceivedHandler);
????????????????m_hwnd?=?this.Handle;
????????????}
????????????catch?(System.Exception?ex)
????????????{
????????????????string?sError?=?“載入窗體錯誤:?“?+?ex.ToString();
????????????????MessageBox.Show(sError);
????????????}
????????}
????????
????????///重寫窗體的消息處理函數DefWndProc,從中加入自己定義消息的檢測的處理入口
????????protected?override?void?DefWndProc(ref?Message?m)
????????{
????????????switch?(m.Msg)
????????????{
????????????????//接收自定義消息MYMESSAGE,并顯示其參數
????????????????case?WM_USER_SERIALRCVMSG:
????????????????????{
????????????????????????//轉換數據顯示
????????????????????????string?sRcv?=?“收到數據:“?+?Utililty.ByteArrayToStringChinese(byDataRcv)?+?“\r\n長度=“?+?m.WParam.ToString();
????????????????????????
????????????????????????txtRcv.Text?=?sRcv;
????????????????????}
????????????????????break;
????????????????default:
????????????????????base.DefWndProc(ref?m);
????????????????????break;
????????????}
????????}
????????
????????private?static?int?CopyRcvData(string?sRcv?int?iLen)
????????{
????????????int?iRe?=?0;
????????????try
????????????{
????????????????//
????????????????//清空
????????????????Array.Clear(byDataRcv?0?byDataRcv.Length);
????????????????//拷貝數據
????????????????Utililty.StringToArrayBytes(ref?byDataRcv?sRcv);
????????????????//發送消息
????????????????//MessageBox.Show(“sRcv=“?+?sRcv?+?“\r\n?iLen?=“?+?iLen.ToString());
????????????????PostMessage((IntPtr)m_hwnd?Form1.WM_USER_SERIALRCVMSG?iLen?0);
????????????}
????????????catch?(System.Exception?ex)
????????????{
????????????????iRe?=?-1;
????????????}
????????????return?iRe;
????????}
????????private?static?int?CopyRcvData(byte[]?byRcv?int?iLen)
????????{
????????????int?iRe?=?0;
?????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????11776??2014-08-26?13:18??Com1Example\Com1Example\bin\Debug\Com1Example.exe
?????文件??????26112??2014-08-26?13:18??Com1Example\Com1Example\bin\Debug\Com1Example.pdb
?????文件??????14328??2014-08-26?11:45??Com1Example\Com1Example\bin\Debug\Com1Example.vshost.exe
?????文件????????490??2007-07-21?01:33??Com1Example\Com1Example\bin\Debug\Com1Example.vshost.exe.manifest
?????文件?????434176??2014-08-06?10:04??Com1Example\Com1Example\bin\Debug\CoreDll.dll
?????文件?????218624??2014-08-25?16:29??Com1Example\Com1Example\bin\Debug\Nuctech.Raman.ba
?????文件???????3982??2014-08-26?10:53??Com1Example\Com1Example\Com1Example.csproj
?????文件???????6766??2014-08-26?13:17??Com1Example\Com1Example\Form1.cs
?????文件???????5489??2014-08-26?10:40??Com1Example\Com1Example\Form1.Designer.cs
?????文件???????6013??2014-08-26?10:40??Com1Example\Com1Example\Form1.resx
?????文件?????218624??2014-08-25?16:29??Com1Example\Com1Example\Nuctech.Raman.ba
?????文件????????890??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug\Com1Example.csproj.FileListAbsolute.txt
?????文件????????847??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug\Com1Example.csproj.GenerateResource.Cache
?????文件??????11776??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug\Com1Example.exe
?????文件????????180??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug\Com1Example.Form1.resources
?????文件??????26112??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug\Com1Example.pdb
?????文件????????180??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug\Com1Example.Properties.Resources.resources
?????文件????????503??2014-08-26?10:11??Com1Example\Com1Example\Program.cs
?????文件???????1434??2014-08-26?10:11??Com1Example\Com1Example\Properties\AssemblyInfo.cs
?????文件???????2851??2014-08-26?10:11??Com1Example\Com1Example\Properties\Resources.Designer.cs
?????文件???????5612??2014-08-26?10:11??Com1Example\Com1Example\Properties\Resources.resx
?????文件???????1096??2014-08-26?10:11??Com1Example\Com1Example\Properties\Settings.Designer.cs
?????文件????????249??2014-08-26?10:11??Com1Example\Com1Example\Properties\Settings.settings
?????文件????????923??2014-08-26?10:11??Com1Example\Com1Example.sln
????..A..H.?????15360??2014-08-26?13:17??Com1Example\Com1Example.suo
?????目錄??????????0??2014-08-26?10:17??Com1Example\Com1Example\obj\Debug\Refactor
?????目錄??????????0??2014-08-26?10:11??Com1Example\Com1Example\obj\Debug\TempPE
?????目錄??????????0??2014-08-26?13:18??Com1Example\Com1Example\bin\Debug
?????目錄??????????0??2014-08-26?13:18??Com1Example\Com1Example\obj\Debug
?????目錄??????????0??2014-08-26?10:11??Com1Example\Com1Example\bin
............此處省略7個文件信息
- 上一篇:C#,AJAX讀寫JSON
- 下一篇:30個 c# 小程序
評論
共有 條評論