在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 串口监控程序

C# 串口监控程序

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:5.62M
  • 下载次数:137
  • 浏览次数:1448
  • 发布时间:2020-04-07
  • 实例类别:C#语言基础
  • 发 布 人:deepfar
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 程序 监控 串口

实例介绍

【实例简介】C# 串口监控示例

【实例截图】

from clipboard


from clipboard


【核心代码】

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);
            
        }
    }
}

标签: 程序 监控 串口

网友评论

第 1 楼 Senses918 发表于: 2022-12-03 17:55 24
亲测,这是一个粘贴复制的程序包。

支持(0) 盖楼(回复)

第 2 楼 huangshulang 发表于: 2023-06-28 14:41 53
程序不完整,最关键的AxCommMonitorX11没有

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有2条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警