基本信息
源码名称:C# 串口监控程序
源码大小:5.62M
文件格式:.rar
开发语言:C#
更新时间:2020-04-07
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍
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);
            
        }
    }
}