zoukankan      html  css  js  c++  java
  • zz Alex's BLOG 串口连接

    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.Runtime.InteropServices;

    namespace ICS
    {
    public partial class FrmMain : Form
    {

    #region WndProc常量
    public const int WM_DEVICECHANGE = 0x219;
    public const int DBT_DEVICEARRIVAL = 0x8000;
    public const int DBT_CONFIGCHANGECANCELED = 0x0019;
    public const int DBT_CONFIGCHANGED = 0x0018;
    public const int DBT_CUSTOMEVENT = 0x8006;
    public const int DBT_DEVICEQUERYREMOVE = 0x8001;
    public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
    public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
    public const int DBT_DEVICEREMOVEPENDING = 0x8003;
    public const int DBT_DEVICETYPESPECIFIC = 0x8005;
    public const int DBT_DEVNODES_CHANGED = 0x0007;
    public const int DBT_QUERYCHANGECONFIG = 0x0017;
    public const int DBT_USERDEFINED = 0xFFFF;

    public const int DBT_DEVTYP_OEM = 0x00000000;//OEM- or IHV-defined device type
    public const int DBT_DEVTYP_PORT = 0x00000003;//Port device (serial or parallel).
    public const int DBT_DEVTYP_VOLUME = 0x00000002;//Logical volume.
    #endregion

    [StructLayout(LayoutKind.Sequential)]
    public struct DEV_BROADCASR_HDR
    {
    public int Size;
    public int DeviceType;
    public int Reserved;
    }
    public FrmMain()
    {
    InitializeComponent();
    }

    protected override void WndProc(ref Message m)
    {
    base.WndProc(ref m);//要保证这句话必须能构执行,否则会出现“创建窗口句柄时出错”,和写代码习惯有关。。
    try
    {
    if (m.Msg != WM_DEVICECHANGE) return;
    DEV_BROADCASR_HDR devHdr = (DEV_BROADCASR_HDR)Marshal.PtrToStructure(m.LParam, typeof(DEV_BROADCASR_HDR));
    if (devHdr.DeviceType == DBT_DEVTYP_PORT)
    {
    AddSerialPort();
    }
    }
    catch { }
    }

    public void AddSerialPort()
    {
    cmboxSerial.Items.Clear();
    cmboxSerial.Items.AddRange(SerialPort.GetPortNames());
    }

    }

    }

  • 相关阅读:
    Android相对布局RelativeLayout常用到的属性
    用LinkedList模拟队列(Java容器)
    JAVA数组(一)
    SQL分页查询(转)
    asp.net 子窗体和父窗体交互
    Silverlight加载外部XAP包和页面
    As.net 动态反射程序集里面DLL并创建对象
    Silverlight LIstBox 实现横向排列元素 并且自动换行
    java jdbc 连接SQL数据库
    Silverlight Command的运用
  • 原文地址:https://www.cnblogs.com/https/p/9234670.html
Copyright © 2011-2022 走看看