8-ESP8266 SDK開發(fā)基礎(chǔ)入門篇--編寫串口上位機(jī)軟件

https://www.cnblogs.com/yangfengwu/p/11087558.html


咱用這個編寫 ,版本都無所謂哈,只要自己有就可以,不同版本怎么打開

https://www.cnblogs.com/aliflycoris/p/8005955.html




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











放幾個文本顯示?



放個下拉框,用來選擇串口號



各復(fù)制出來




把串口拖上來



做一個功能,軟件啟動的時候把電腦上所有的串口號顯示到




string[] ports = System.IO.Ports.SerialPort.GetPortNames();//獲取電腦上可用串口號


?看一下控件的ID


comboBox1.Items.AddRange(ports);//給comboBox1添加數(shù)據(jù)comboBox1.SelectedIndex = comboBox1.Items.Count >0?0: -1;//如果里面有數(shù)據(jù),顯示第0個



如果電腦上有可用串口,會顯示


現(xiàn)在控制串口打開和關(guān)閉,,,,,,,改改按鈕顯示的哈,,,

讓它默認(rèn)顯示打開





忘了...先設(shè)置下有可選擇的波特率


寫上常用的



13824009216004608002560002304001280001152007680057600430003840019200144009600

4800

1200



設(shè)置下默認(rèn)顯示的




if(button1.Text =="打開")//如果按鈕顯示的是打開? ? ? ? ? ? {

? ? ? ? ? ? ? ? try//防止意外錯誤? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? serialPort1.PortName = comboBox1.Text;//得到comboBox1顯示的串口內(nèi)容serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);//得到comboBox2顯示的波特率內(nèi)容serialPort1.Open();//打開串口button1.Text ="關(guān)閉";//按鈕顯示關(guān)閉? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? MessageBox.Show("打開失敗","提示!");//對話框顯示打開失敗? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? else//要關(guān)閉串口? ? ? ? ? ? {

? ? ? ? ? ? ? ? try//預(yù)防串口有問題了,實際上已經(jīng)關(guān)了? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? serialPort1.Close();//關(guān)閉串口? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? button1.Text ="打開";//按鈕顯示打開}







現(xiàn)在優(yōu)化一個地方

就是串口本來連接著電腦,然后拔下來了,或者重新來了個串口,咱檢測下串口熱插拔,然后重新更新下顯示



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


開始上菜了哈,,




protectedoverridevoidWndProc(ref Message m)

? ? ? ? {

? ? ? ? ? ? if(m.Msg ==0x0219)//設(shè)備改變? ? ? ? ? ? {

? ? ? ? ? ? ? ? if(m.WParam.ToInt32() ==0x8004)//usb串口拔出? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? string[] ports = System.IO.Ports.SerialPort.GetPortNames();//重新獲取串口? ? ? ? ? ? ? ? ? ? comboBox1.Items.Clear();

? ? ? ? ? ? ? ? ? ? comboBox1.Items.AddRange(ports);

? ? ? ? ? ? ? ? ? ? if(button1.Text =="關(guān)閉")//咱打開過一個串口? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? if(!serialPort1.IsOpen)//咱打開的那個關(guān)閉了,說明拔插的是咱打開的? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? button1.Text ="打開";

? ? ? ? ? ? ? ? ? ? ? ? ? ? serialPort1.Dispose();//釋放掉原先的串口資源comboBox1.SelectedIndex = comboBox1.Items.Count >0?0: -1;//顯示獲取的第一個串口號? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? else//熱插拔不是咱打開的那個? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? comboBox1.Text = PortNameCopy;//默認(rèn)顯示的是咱打開的那個串口號? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? else//沒有打開過? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? comboBox1.SelectedIndex = comboBox1.Items.Count >0?0: -1;//顯示獲取的第一個串口號? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? elseif(m.WParam.ToInt32() ==0x8000)//usb串口連接上? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? string[] ports = System.IO.Ports.SerialPort.GetPortNames();//重新獲取串口? ? ? ? ? ? ? ? ? ? comboBox1.Items.Clear();

? ? ? ? ? ? ? ? ? ? comboBox1.Items.AddRange(ports);

? ? ? ? ? ? ? ? ? ? if(button1.Text =="關(guān)閉")//咱打開過一個串口? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? comboBox1.Text = PortNameCopy;//默認(rèn)顯示的是咱打開的那個串口號? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? comboBox1.SelectedIndex = comboBox1.Items.Count >0?0: -1;//顯示獲取的第一個串口號? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? base.WndProc(ref m);

? ? ? ? }



這個是系統(tǒng)自帶的函數(shù)

關(guān)于那些值,,,我是自己監(jiān)控的....


現(xiàn)在大家自己測試熱插拔哈


寫的夠多的了...放到下一節(jié)接著寫


https://www.cnblogs.com/yangfengwu/p/11087618.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容