![VB編寫的簡單的上位機_第1頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/8/1aee41d4-b710-4a59-8bf9-1383f0886f59/1aee41d4-b710-4a59-8bf9-1383f0886f591.gif)
![VB編寫的簡單的上位機_第2頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/8/1aee41d4-b710-4a59-8bf9-1383f0886f59/1aee41d4-b710-4a59-8bf9-1383f0886f592.gif)
![VB編寫的簡單的上位機_第3頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/8/1aee41d4-b710-4a59-8bf9-1383f0886f59/1aee41d4-b710-4a59-8bf9-1383f0886f593.gif)
![VB編寫的簡單的上位機_第4頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/8/1aee41d4-b710-4a59-8bf9-1383f0886f59/1aee41d4-b710-4a59-8bf9-1383f0886f594.gif)
![VB編寫的簡單的上位機_第5頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/8/1aee41d4-b710-4a59-8bf9-1383f0886f59/1aee41d4-b710-4a59-8bf9-1383f0886f595.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、一個簡單的用 vb 編寫的上位機程序自己想做一個簡單上位機, 卻發(fā)現(xiàn)相關(guān)的資料很少,于是在做出來之后把自己的這樣一個簡單程序上傳,給新入門的朋友一個借鑒學(xué)習(xí)的資料。窗口布局用到的 mscomm 控件的基本屬性(必用)commport :設(shè)置或返回串口通信的串口號(com 口)設(shè)置格式是: object.commport= value,其中 object 為控件名,如 mscomm1;value的合法值是整形的數(shù)據(jù),例如1,2比如 mscomm1.commport=1(默認值),表示我們將用 com1 口進行通信com 口可以通過鼠標右擊“我的電腦(計算機) ”圖標,在彈出的快捷菜單中選擇“管理
2、”,彈出如下圖左邊的對話框左鍵點擊“設(shè)備管理器”可以看到上圖右邊對話框左鍵點擊設(shè)備管理器可以看到如上圖右邊視圖其中的就是當(dāng)前連接到計算機的可用串口了settings :以字符串的形式設(shè)置或返回串口通信參數(shù)。設(shè)置格式:object.commport= value其中 object為控件名,如 mscomm1;value需為一字符串,由四個設(shè)置值組成其格式為:“波特率,奇偶校驗,數(shù)據(jù)位,停止位”portopen : 設(shè)置或返回串口狀態(tài), 若其已經(jīng)打開, 則返回 true, 否則返回 false設(shè)置格式: object.portopen= value其中 object 為控件名,如mscomm1;v
3、alue為一邏輯值,若為true,則打開,否則關(guān)閉注意:如果串口不是打開(關(guān)閉) ,而對它進行關(guān)閉(打開)將會出錯rthreshold :該屬性為一閥值。當(dāng)接收緩沖區(qū)中字符數(shù)達到該值時,mscomm控件設(shè)置 commevent屬性為 comevreceive, 并產(chǎn)生 oncomm事件。用戶可在 oncomm事件處理程序中進行相應(yīng)處理。若rthreshold屬性設(shè)置為 0,則不產(chǎn)生 oncomm 事件。例如用戶希望接收緩沖區(qū)中達到一個字符就接收一個字符,可將 rthreshold設(shè)置為 1。這樣接收緩沖區(qū)中接收到一個字符,就產(chǎn)生一次oncomm事件。inputmode:設(shè)置或返回接收數(shù)據(jù)的類型
4、。設(shè)置格式: object.inputmode= value,其中 value的值有兩個;cominputmodebinary 表示以二進制方式接收數(shù)據(jù), input 屬性返回值為一字符串cominputmodetext表示以字符方式接收數(shù)據(jù),input 屬性返回值為一字數(shù)組inputlen:設(shè)置或返回一次從接收緩沖區(qū)中讀取字節(jié)數(shù)。設(shè)置格式: object. inputlen= value,value 為一個整形數(shù)據(jù);當(dāng)其設(shè)置為 0 時,表示一次讀取接收緩沖區(qū)中的全部內(nèi)容。這個屬性在定字節(jié)讀取中非常有用input:從接收緩沖區(qū)中讀取數(shù)據(jù)并清空該緩沖區(qū),該屬性設(shè)計時無效,運行時只讀。使用格式:
5、str=object.input,str為一字符串型變量或?qū)ο髈utput:向發(fā)送緩沖區(qū)發(fā)送數(shù)據(jù),該屬性設(shè)計時無效,運行時只讀。使用格式: object.output=value,value 為一字符串型變量或任意類型的數(shù)組commevent :這是一個非常重要的屬性。該屬性設(shè)計時無效,運行時只讀。一旦串口發(fā)生通信事件或產(chǎn)生錯誤, 依據(jù)產(chǎn)生的事件和錯誤, mscomm控件為 commevent 屬性賦不同的代碼,同時產(chǎn)生oncomm 事件。用戶程序就可在oncomm 事件處理程序中針對不同的代碼, 進行相應(yīng)的處理。comevreceive 接受到 rthreshold 個字符。該事件將持續(xù)產(chǎn)生
6、,直到用 input屬性從接受緩沖區(qū)中讀取并刪除字符。其它的屬性,需要時可以在網(wǎng)上查找相關(guān)資料(下面附幾個網(wǎng)址)http:/ sub combo2_click()mscomm1.settings = combo2.text + ,n,8,1end subprivate sub command6_click()on error goto uerror:if command6.caption = 打開串口 thenmscomm1.portopen = truecommand6.caption = 關(guān)閉串口 shape1.fillcolor = rgb(0, 255, 0)elsemscomm1.p
7、ortopen = falsecommand6.caption = 打開串口 shape1.fillcolor = rgb(128, 128, 128)end ifexit subuerror:msg$ = 無效端口號 title$ = 串口調(diào)試 x = msgbox(msg$, 48, title$)end subprivate sub form_load()text1 = text3 = combo2.listindex = 0combo1.listindex = 0timer1.interval = val(text2)mscomm1.commport = combo1.listinde
8、x + 1mscomm1.rthreshold = 1mscomm1.inputlen = 0if option3 then mscomm1.inputmode = cominputmodebinaryend subprivate sub form_unload(cancel as integer)if mscomm1.portopen then mscomm1.portopen = falseend subprivate sub mscomm1_oncomm()dim bytreceived() as bytedim strbuff as stringselect casemscomm1.c
9、ommeventcasecomevreceiveif option4 thentext3 = text3 & mscomm1.inputelsestrbuff = mscomm1.inputbytreceived() = strbuffstrbuff = dim i as integerfor i = 0 to ubound(bytreceived)if len(hex(bytreceived(i)= 1 thenstrbuff = strbuff & 0 & hex(bytreceived(i)& elsestrbuff = strbuff & hex
10、(bytreceived(i)& end ifnexttext3 = text3 & strbuffend ifend selectend subprivate sub option3_click()mscomm1.inputmode = cominputmodebinaryend subprivate sub option4_click()mscomm1.inputmode = cominputmodetextend subprivate sub timer1_timer()dim sendbuff() as bytedim bytflag as stringdim strb
11、uff as stringdim i as integerdim flag as booleanif option2 thenmscomm1.output = text1.textelsefor i = 1 to len(ltrim(text1)bytflag = mid(text1, i, 1)if bytflag= 0 and bytflag = a and bytflag= a and bytflag = f thenflag = trueelseflag = falseexit forend ifnextif flag thenif len(ltrim(text1)mod 2 0 th
12、enstrbuff = 0 & ltrim(text1)elsestrbuff = text1end ifredim sendbuff(len(strbuff)/ 2 - 1)for i = 0 to ubound(sendbuff)sendbuff(i) = val(&h& mid(strbuff,2 * i + 1, 2)nextmscomm1.output = sendbuffelsemsg$ = 請輸入 hex 格式數(shù)據(jù): a-f、a-f、0-9title$ = 串口調(diào)試助手 x = msgbox(msg$, 48, title$)timer1.enabled
13、= falsecommand3.caption = 自動發(fā)送 end ifend ifend subprivate sub combo1_click()mscomm1.commport = combo1.listindex + 1end subprivate sub command1_click()text1 = end subprivate sub command2_click()dim sendbuff() as bytedim bytflag as stringdim strbuff as stringdim i as integerdim flag as booleantimer1.e
14、nabled = falseif mscomm1.portopen thenif option2 thenmscomm1.output = text1.textelsefor i = 1 to len(ltrim(text1)bytflag = mid(text1, i, 1)if bytflag = 0 and bytflag = a and bytflag = a and bytflag = f thenflag = trueelseflag = falseexit forend ifnextif flag thenif len(ltrim(text1)mod 2 0 thenstrbuf
15、f = 0 & ltrim(text1)elsestrbuff = text1end ifredim sendbuff(len(strbuff)/ 2 - 1)for i = 0 to ubound(sendbuff)sendbuff(i) = val(&h& mid(strbuff,2 * i + 1, 2)nextmscomm1.output = sendbuffelsemsg$= 請輸入 hex 格式數(shù)據(jù): a-f、a-f 、0-9title$ = 串口調(diào)試助手x = msgbox(msg$, 48, title$)end ifend ifelsemsg$ = 請打開串口 title$ = 串口調(diào)試助手x = msgbox(msg$, 48, title$)end ifend subprivate sub command3_click()if mscomm1.portopen thenif command3.caption = 自動發(fā)送 thentimer1.enabled = truetimer1.interval = val(
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年全球及中國乘用車用輕型柴油發(fā)動機行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025年全球及中國800G 數(shù)據(jù)中心交換機行業(yè)頭部企業(yè)市場占有率及排名調(diào)研報告
- 2025-2030全球電動汽車電子軸行業(yè)調(diào)研及趨勢分析報告
- 2025-2030全球高架軌道秤行業(yè)調(diào)研及趨勢分析報告
- 2025打工人發(fā)財游園年會(打工人發(fā)財年會主題)活動策劃方案
- 建筑節(jié)能的規(guī)劃與實施策略
- 健身休閑行業(yè)服務(wù)交易合同范文
- 會計勞動合同模板
- 掌握數(shù)據(jù)分析的關(guān)鍵技能
- 石材幕墻施工合同范本
- 醫(yī)院醫(yī)共體2025年度工作計劃
- 《酶聯(lián)免疫分析技術(shù)》課件
- 鮮棗貯藏技術(shù)規(guī)程
- DB23T 3838-2024商貿(mào)行業(yè)有限空間個體防護裝備配備規(guī)范
- 2024年循環(huán)水操作工(中級)職業(yè)鑒定理論考試題庫((含答案))
- 《電子技術(shù)基礎(chǔ)(第二版)》中職技工全套教學(xué)課件
- 人教版五年級上冊小數(shù)乘除法豎式計算題200道及答案
- 五年級上冊美術(shù)《傳統(tǒng)門飾》課件
- DL∕T 1309-2013 大型發(fā)電機組涉網(wǎng)保護技術(shù)規(guī)范
- (2020版)煤礦安全生產(chǎn)標準化管理體系評分表
- 城鄉(xiāng)低保待遇協(xié)議書
評論
0/150
提交評論