8-ESP8266 SDK开发基础入门篇--编写串口上位机软件

http://www.javashuo.com/article/p-tgmjlcsx-bv.htmlhtml

 

咱用这个编写 ,版本都无所谓哈,只要本身有就能够,不一样版本怎么打开api

http://www.javashuo.com/article/p-nprpwouf-r.htmlide

 

 

 

C#API     https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframework-4.0函数

 

 

 

 

 

 

 

 

 

 

 

 

 

放几个文本显示 测试

 

 

 

放个下拉框,用来选择串口号优化

 

 

 

 

各复制出来spa

 

 

 

 

把串口拖上来.net

 

 

 

 作一个功能,软件启动的时候把电脑上全部的串口号显示到3d

 

 

 

 

 

 

string[] ports = System.IO.Ports.SerialPort.GetPortNames();//获取电脑上可用串口号

 

 看一下控件的IDcode

 

 

comboBox1.Items.AddRange(ports);//给comboBox1添加数据
            comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//若是里面有数据,显示第0个

 

 

 若是电脑上有可用串口,会显示

 

 

 如今控制串口打开和关闭,,,,,,,改改按钮显示的哈,,,

让它默认显示打开

 

 

 

 

 

忘了...先设置下有可选择的波特率

 

写上经常使用的

 

 

1382400
921600
460800
256000
230400
128000
115200
76800
57600
43000
38400
19200
14400
9600
4800
1200

 

 

设置下默认显示的

 

 

 

 

if (button1.Text == "打开")//若是按钮显示的是打开
            {
                try//防止意外错误
                {
                    serialPort1.PortName = comboBox1.Text;//获得comboBox1显示的串口内容
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);//获得comboBox2显示的波特率内容
                    serialPort1.Open();//打开串口
                    button1.Text = "关闭";//按钮显示关闭
                }
                catch (Exception)
                {
                    MessageBox.Show("打开失败", "提示!");//对话框显示打开失败
                }
            }
            else//要关闭串口
            {
                try//预防串口有问题了,实际上已经关了
                {
                    serialPort1.Close();//关闭串口
                }
                catch (Exception)
                {
                }
                button1.Text = "打开";//按钮显示打开
            }

 

 

 

 

 

 

 

 

 

 

 如今优化一个地方

就是串口原本链接着电脑,而后拔下来了,或者从新来了个串口,咱检测下串口热插拔,而后从新更新下显示

 

 

https://blog.csdn.net/woshidaniu/article/details/44044093

 

开始上菜了哈,,

 

 

 

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0219)//设备改变
            {
                if (m.WParam.ToInt32() == 0x8004)//usb串口拔出
                {
                    string[] ports = System.IO.Ports.SerialPort.GetPortNames();//从新获取串口
                    comboBox1.Items.Clear();
                    comboBox1.Items.AddRange(ports);
                    if (button1.Text == "关闭")//咱打开过一个串口
                    {
                        if (!serialPort1.IsOpen)//咱打开的那个关闭了,说明拔插的是咱打开的
                        {
                            button1.Text = "打开";
                            serialPort1.Dispose();//释放掉原先的串口资源
                            comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//显示获取的第一个串口号
                        }
                        else//热插拔不是咱打开的那个
                        {
                            comboBox1.Text = PortNameCopy;//默认显示的是咱打开的那个串口号
                        }
                    }
                    else//没有打开过
                    {
                        comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//显示获取的第一个串口号
                    }
                }
                else if (m.WParam.ToInt32() == 0x8000)//usb串口链接上
                {
                    string[] ports = System.IO.Ports.SerialPort.GetPortNames();//从新获取串口
                    comboBox1.Items.Clear();
                    comboBox1.Items.AddRange(ports);
                    if (button1.Text == "关闭")//咱打开过一个串口
                    {
                        comboBox1.Text = PortNameCopy;//默认显示的是咱打开的那个串口号
                    }
                    else
                    {
                        comboBox1.SelectedIndex = comboBox1.Items.Count > 0 ? 0 : -1;//显示获取的第一个串口号
                    }                          
                }
            }
            base.WndProc(ref m);
        }

 

 

 这个是系统自带的函数

关于那些值,,,我是本身监控的....

 

如今你们本身测试热插拔哈

 

写的够多的了...放到下一节接着写

 

http://www.javashuo.com/article/p-abwyufaz-e.html

相关文章
相关标签/搜索