立即注册
查看: 1608|回复: 2

C#写的串口助手源码

发表于 2016-6-13 11:09:58 | 显示全部楼层 |阅读模式 来自 广东省深圳市
运行界面图.jpg
for (int i = 1; i < 256; i++)
{
    comport.Items.Add("COM"+i.ToString());

}

在serialport中本来就有这个种自动获取端口号的方法。连看打开设备管理器看COM号都省了
//
// 摘要:
//     获取当前计算机的串行端口名的数组。
//
// 返回结果:
//     当前计算机的串行端口名的数组。
//
// 异常:
//   T:System.ComponentModel.Win32Exception:
//     无法查询的串行端口名称。
public static string[] GetPortNames();

这样即可。
string[] comname = SerialPort.GetPortNames();
Array.Sort(comname);
comboBox_com.Items.AddRange(comname);
comboBox_com.SelectedIndex = comboBox_com.Items.Count > 0 ? 0 : -1;


USB插拔检测代码
// USB消息定义
public const int WM_DEVICE_CHANGE = 0x219;
public const int DBT_DEVICEARRIVAL = 0x8000;
public const int DBT_DEVICE_REMOVE_COMPLETE = 0x8004;
public const UInt32 DBT_DEVTYP_PORT = 0x00000003;
[Structlayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_HDR
{
    public UInt32 dbch_size;
    public UInt32 dbch_devicetype;
    public UInt32 dbch_reserved;
}

[StructLayout(LayoutKind.Sequential)]
protected struct DEV_BROADCAST_PORT_Fixed
{
    public uint dbcp_size;
    public uint dbcp_devicetype;
    public uint dbcp_reserved;
    // Variable?length field dbcp_name is declared here in the C header file.
}


/// <summary>
/// 检测USB串口的拔插
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref message m)
{
    if (m.Msg == WM_DEVICE_CHANGE)        // 捕获USB设备的拔出消息WM_DEVICECHANGE
    {
        switch (m.WParam.ToInt32())
        {
            case DBT_DEVICE_REMOVE_COMPLETE:    // USB拔出  
            {
                MessageBox.Show("USB设备拔出!");
                break;
            }

            case DBT_DEVICEARRIVAL:             // USB插入获取对应串口名称
            {
                MessageBox.Show("USB设备插入!");
                /*
                DEV_BROADCAST_HDR dbhdr = (DEV_BROADCAST_HDR)Marshal.PtRTOStructure(m.LParam, typeof(DEV_BROADCAST_HDR));
                if (dbhdr.dbch_devicetype == DBT_DEVTYP_PORT)
                {
                    string portName = Marshal.PtrToStringUni((IntPtr)(m.LParam.ToInt32() + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT_Fixed))));
                    Console.WriteLine("Port '" + portName + "' arrived.");
                }*/

                //自动搜索串口,并将其加入到[串口选择框]中
                string[] SCIPorts;//字符串数组存放搜索到的COM口

                SCIPorts = System.IO.Ports.SerialPort.GetPortNames();//获取所有COM口
                this.UART_Num_ComboBox.Items.Clear();//首先将现有的项清除掉

                int num = SCIPorts.Length;

                //向[串口选择框]中添加搜索到的串口号
                for (int i = 1; i <= SCIPorts.Length; i++)
                {
                    this.UART_Num_ComboBox.Items.Add(SCIPorts[num - i]);
                }

                //UART_Num_ComboBox.Text

                //设置各组合框的初始显示值
                if (SCIPorts.Length != 0)
                {
                    //初始值选择第一个Index的内容
                    this.UART_Num_ComboBox.SelectedIndex = 0;//串口号
                    this.UART_Speed_ComboBox.SelectedIndex = 0;//波特率

                }
                else
                {
                    MessageBox.Show("没有可用的串口,请检查!");
                }

                break;                       
            }
        }
    }
    base.WndProc(ref m);
}

2016-6-13 11:06 上传
文件大小:
701.06 KB
下载次数:
26
本地下载

16RD supports Paypal , Payment is calculated at the exchange rate of the day. Unable to download please contact 18902843661 (WhatsApp OR wechat number)

关于一牛网在微软浏览器(Microsoft Edge、IE浏览器)警报通告&解决方案!(无法下载直接更换浏览器即可)

*附件为作者发布,与本站无关,如有侵权,请联系客服删除

发表于 2017-3-21 12:22:17 | 显示全部楼层 来自 广东省深圳市
这么好的东西怎么没有人回复?
发表于 2017-4-1 09:45:25 | 显示全部楼层 来自 吉林省长春市
C#的实际应用这么多啊,太好了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

合作/建议

TEL: 19168984579

工作时间:
周一到周五 9:00-11:30 13:30-19:30
  • 扫一扫关注公众号
  • 扫一扫打开小程序
Copyright © 2013-2024 一牛网 版权所有 All Rights Reserved. 帮助中心|隐私声明|联系我们|手机版|粤ICP备13053961号|营业执照|EDI证
在本版发帖搜索
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表