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.Reflection;
using System.Runtime.InteropServices;
namespace CSharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool fActive = false;
long dwReadTotal = 0;
long dwWriteTotal = 0;
const int IRP_MJ_CREATE = 0x00;
const int IRP_MJ_CLOSE = 0x02;
const int IRP_MJ_READ = 0x03;
const int IRP_MJ_WRITE = 0x04;
const int IRP_MJ_DEVICE_CONTROL = 0x0E;
const int IRP_MJ_CLEANUP = 0x12;
const int IRP_MJ_MAXIMUM_FUNCTION = 0x1B;
const int IRP_MJ_FLUSH_BUFFERS = 0x09;
public enum ShowCommands : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}
[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);
private void Form1_Load(object sender, EventArgs e)
{
this.axCommMonitor = new AxCommMonitorX11.AxCCommMonitorX();
((System.ComponentModel.ISupportInitialize)(this.axCommMonitor)).BeginInit();
this.axCommMonitor.Location = new System.Drawing.Point(10, 5);
this.axCommMonitor.Name = "axCommMonitor";
resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.axCommMonitor.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axCCommMonitorX1.OcxState")));
this.axCommMonitor.Size = new System.Drawing.Size(160, 28);
this.axCommMonitor.TabIndex = 3;
this.axCommMonitor.OnMonitor = new AxCommMonitorX11.ICommMonitorXEvents_OnMonitorEventHandler(this.axCommMonitor_OnMonitor);
this.panel1.Controls.Add(this.axCommMonitor);
this.Text = axCommMonitor.Caption "/for C# demo| QQ/Email: 348677065@qq.com";
this.axCommMonitor.Caption = "CommMonitor11.0 SDK/OCX";
this.panel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.axCommMonitor)).EndInit();
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
this.axCommMonitor.About();
}
private void StartMonitor_Click(object sender, EventArgs e)
{
if (!fActive)
{
fActive = axCommMonitor.StartMonitor("", this.comboBox1.Text, false);
if (!fActive) {
switch (axCommMonitor.LastError)
{
case -252: System.Windows.Forms.MessageBox.Show("Monitoring service must be run with administrator privileges."); break;
case -253: System.Windows.Forms.MessageBox.Show("Already open monitor."); break;
case -254: System.Windows.Forms.MessageBox.Show("Invalid port."); break;
case -255: System.Windows.Forms.MessageBox.Show("Binding monitoring failed."); break;
// else FormatMessage lastError windows error msg
}
}
}
else
{
axCommMonitor.StopMonitor();
fActive = false;
}
if (fActive)
{
StartMonitor.Text = "StopMonitor";
StartMonitor.ForeColor = Color.Blue;
}
else
{
StartMonitor.Text = "StartMonitor";
StartMonitor.ForeColor = Color.Black;
}
}
private void button2_Click(object sender, EventArgs e)
{
redtData.Clear();
}
private void axCommMonitor_OnMonitor(object sender, AxCommMonitorX11.ICommMonitorXEvents_OnMonitorEvent e)
{
string ss = "";
string data = "";
Color color = Color.Black;
if ((e.dwSize > 0) && ((e.dwCtrlCode == IRP_MJ_READ) || (e.dwCtrlCode == IRP_MJ_WRITE)))
{
byte[] numbers = (byte[])e.vtData;
ss = Encoding.GetEncoding("GB2312").GetString(numbers);
}
switch (e.dwCtrlCode)
{
case IRP_MJ_CLOSE:
data = string.Format("{0}: Close", e.sPortName);
break;
case IRP_MJ_CREATE:
data = string.Format("{0}: Open", e.sPortName);
break;
case IRP_MJ_READ:
{
dwReadTotal = e.dwSize;
color = Color.Blue;
data = string.Format("{0}, Read({1:D}): {2}", e.sPortName, e.dwSize, ss);
break;
}
case IRP_MJ_WRITE:
{
dwWriteTotal = e.dwSize;
color = Color.Green;
data = string.Format("{0}, Write({1:D}): {2}", e.sPortName, e.dwSize, ss);
break;
}
default:
{
if (e.dwCtrlCode > 0x400)
{
color = Color.Black;
data = string.Format("{0}, CTLCode({1:D}): {2}",
e.sPortName,
axCommMonitor.SerialCtrlCode(e.dwCtrlCode),
e.dwCtrlCode,
ss);
}
break;
}
}
if (data != "")
{
this.redtData.SelectionColor = color;
redtData.AppendText(data);
redtData.AppendText(Environment.NewLine);
}
if ((e.dwCtrlCode == IRP_MJ_READ) || (e.dwCtrlCode == IRP_MJ_WRITE))
{
lblTip.Text = string.Format("Write: {0:D}(Bytes), Read: {1:D}(Bytes)", dwWriteTotal, dwReadTotal);
}
}
private void btnWriteData_Click(object sender, EventArgs e)
{
byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(textBox1.Text);
axCommMonitor.WriteData(this.comboBox1.Text, (object)bytes, bytes.Length);
}
private void btnOpenSDKDir_Click(object sender, EventArgs e)
{
string sDir = System.IO.Directory.GetCurrentDirectory();
string path3 = String.Format("/select,\"{0}\\demo\"", sDir);
ShellExecute(IntPtr.Zero, "open", "explorer", path3, "", ShowCommands.SW_SHOWNORMAL);
///ShellExecute(IntPtr.Zero, "open", "explorer", sDir, "", ShowCommands.SW_SHOWNORMAL);
}
private void btnOpenHelp_Click(object sender, EventArgs e)
{
string path3 = System.IO.Directory.GetCurrentDirectory() "\\help.pdf";
ShellExecute(IntPtr.Zero, "open", path3, "", "", ShowCommands.SW_SHOWNORMAL);
}
}
}
资源文件列表
VS2008/.vs/VS2008/v15/.suo , 62464
VS2008/.vs/VS2008/v15/Browse.VC.db , 25092096
VS2008/.vs/VS2008/v15/Server/sqlite3/db.lock , 0
VS2008/.vs/VS2008/v15/Server/sqlite3/storage.ide , 4096
VS2008/.vs/VS2008/v15/Server/sqlite3/storage.ide-shm , 32768
VS2008/.vs/VS2008/v15/Server/sqlite3/storage.ide-wal , 799312
VS2008/.vs/VS2008/v15/Solution.VC.db , 688128
VS2008/Backup/CSharp/CSharp.csproj , 5032
VS2008/Backup/CSharp/Form1.cs , 8118
VS2008/Backup/CSharp/Form1.Designer.cs , 12703
VS2008/Backup/CSharp/Form1.resx , 6014
VS2008/Backup/CSharp/Program.cs , 487
VS2008/Backup/CSharp/Properties/AssemblyInfo.cs , 1362
VS2008/Backup/CSharp/Properties/Resources.Designer.cs , 2862
VS2008/Backup/CSharp/Properties/Resources.resx , 5612
VS2008/Backup/CSharp/Properties/Settings.Designer.cs , 1091
VS2008/Backup/CSharp/Properties/Settings.settings , 249
VS2008/Backup/VB.net/Form1.Designer.vb , 10324
VS2008/Backup/VB.net/Form1.resx , 6601
VS2008/Backup/VB.net/Form1.vb , 5253
VS2008/Backup/VB.net/My Project/Application.Designer.vb , 1509
VS2008/Backup/VB.net/My Project/Application.myapp , 510
VS2008/Backup/VB.net/My Project/AssemblyInfo.vb , 1091
VS2008/Backup/VB.net/My Project/Resources.Designer.vb , 2794
VS2008/Backup/VB.net/My Project/Resources.resx , 5612
VS2008/Backup/VB.net/My Project/Settings.Designer.vb , 3032
VS2008/Backup/VB.net/My Project/Settings.settings , 279
VS2008/Backup/VB.net/VB.net.vbproj , 7503
VS2008/Backup/VS2008.sln , 4211
VS2008/CSharp/CSharp.csproj , 5133
VS2008/CSharp/Form1.cs , 8118
VS2008/CSharp/Form1.Designer.cs , 12703
VS2008/CSharp/Form1.resx , 6014
VS2008/CSharp/obj/x86/Debug/AxInterop.CommMonitorX11.dll , 8192
VS2008/CSharp/obj/x86/Debug/CSharp.csproj.CopyComplete , 0
VS2008/CSharp/obj/x86/Debug/CSharp.csproj.CoreCompileInputs.cache , 42
VS2008/CSharp/obj/x86/Debug/CSharp.csproj.FileListAbsolute.txt , 2184
VS2008/CSharp/obj/x86/Debug/CSharp.csproj.GenerateResource.cache , 847
VS2008/CSharp/obj/x86/Debug/CSharp.csprojAssemblyReference.cache , 39631
VS2008/CSharp/obj/x86/Debug/CSharp.exe , 16384
VS2008/CSharp/obj/x86/Debug/CSharp.Form1.resources , 180
VS2008/CSharp/obj/x86/Debug/CSharp.pdb , 26112
VS2008/CSharp/obj/x86/Debug/CSharp.Properties.Resources.resources , 180
VS2008/CSharp/obj/x86/Debug/DesignTimeResolveAssemblyReferences.cache , 1099
VS2008/CSharp/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 7108
VS2008/CSharp/obj/x86/Debug/Interop.CommMonitorX11.dll , 9216
VS2008/CSharp/obj/x86/Debug/TempPE/Properties.Resources.Designer.cs.dll , 3584
VS2008/CSharp/Program.cs , 487
VS2008/CSharp/Properties/AssemblyInfo.cs , 1362
VS2008/CSharp/Properties/Resources.Designer.cs , 2847
VS2008/CSharp/Properties/Resources.resx , 5612
VS2008/CSharp/Properties/Settings.Designer.cs , 1106
VS2008/CSharp/Properties/Settings.settings , 249
VS2008/MFC-DLL/Debug/DLL.log , 344
VS2008/MFC-DLL/DLL.APS , 59520
VS2008/MFC-DLL/DLL.cpp , 1762
VS2008/MFC-DLL/DLL.h , 431
VS2008/MFC-DLL/DLL.rc , 6005
VS2008/MFC-DLL/DLL.vcproj , 5391
VS2008/MFC-DLL/DLL.vcproj.DESKTOP-IN6MP6L.king.user , 1427
VS2008/MFC-DLL/DLL.vcproj.Jfyes-PC.Jfyes.user , 1413
VS2008/MFC-DLL/DLL.vcproj.king-pc.king.user , 1411
VS2008/MFC-DLL/DLL.vcxproj , 6733
VS2008/MFC-DLL/DLL.vcxproj.filters , 2011
VS2008/MFC-DLL/DLLDlg.cpp , 9949
VS2008/MFC-DLL/DLLDlg.h , 916
VS2008/MFC-DLL/ReadMe.txt , 2819
VS2008/MFC-DLL/res/DLL.ico , 21630
VS2008/MFC-DLL/res/DLL.rc2 , 359
VS2008/MFC-DLL/resource.h , 951
VS2008/MFC-DLL/stdafx.cpp , 134
VS2008/MFC-DLL/stdafx.h , 1796
VS2008/MFC-DLL/targetver.h , 1030
VS2008/UpgradeLog.htm , 55328
VS2008/VB.net/Form1.Designer.vb , 10324
VS2008/VB.net/Form1.resx , 6601
VS2008/VB.net/Form1.vb , 5253
VS2008/VB.net/My Project/Application.Designer.vb , 1554
VS2008/VB.net/My Project/Application.myapp , 510
VS2008/VB.net/My Project/AssemblyInfo.vb , 1091
VS2008/VB.net/My Project/Resources.Designer.vb , 2782
VS2008/VB.net/My Project/Resources.resx , 5612
VS2008/VB.net/My Project/Settings.Designer.vb , 2975
VS2008/VB.net/My Project/Settings.settings , 279
VS2008/VB.net/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache , 6972
VS2008/VB.net/obj/Debug/TempPE/My Project.Resources.Designer.vb.dll , 6144
VS2008/VB.net/obj/Debug/VB.net.vbproj.CoreCompileInputs.cache , 42
VS2008/VB.net/obj/Debug/VB.net.vbprojAssemblyReference.cache , 8684
VS2008/VB.net/obj/x86/Debug/AxInterop.CommMonitorX11.dll , 8192
VS2008/VB.net/obj/x86/Debug/Interop.CommMonitorX11.dll , 9216
VS2008/VB.net/obj/x86/Debug/TempPE/My Project.Resources.Designer.vb.dll , 7680
VS2008/VB.net/obj/x86/Debug/VB.net.vbproj.ResolveComReference.cache , 590
VS2008/VB.net/VB.net.vbproj , 7764
VS2008/VS2008.sln , 4418
