串口調(diào)試編程_第1頁
串口調(diào)試編程_第2頁
串口調(diào)試編程_第3頁
串口調(diào)試編程_第4頁
串口調(diào)試編程_第5頁
已閱讀5頁,還剩10頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Visual Basic與RS-232串口通信論文題目: Visual Basic與RS-232串口通信實作者:高麗君班級:自動化0803班學(xué)號: 0806050303時間: 2011年5月8日Visual Basic與RS-232串口通信實驗一、實驗?zāi)康模和ㄟ^軟件Visual Basic編寫程序和硬件計算機、RS-232實現(xiàn)比較完善的通信:數(shù)據(jù)的接收與發(fā)送。二、實驗器材:PC機一臺、RS-232一條、Visual Basic軟件三、實驗內(nèi)容:(1) 在線設(shè)置通信速率、奇偶校驗、通信口而無需重新啟動程序;(2) 設(shè)置定時發(fā)送的數(shù)據(jù)和時間間隔;(3) 自動顯示接收到的數(shù)據(jù),并能在在字符串、十進制

2、、十六進制之間進行自由切換;(4) 自動保存設(shè)置參數(shù)。四、實驗原理:4.1Visual Basic 簡介Visual Basic(VB)是一種由微軟公司開發(fā)的包含協(xié)助開發(fā)環(huán)境的事件驅(qū)動編程語言。VB擁有圖形用戶界面(GUI)和快速應(yīng)用程序開發(fā)(RAD)系統(tǒng),可以輕易的使用DAO、RDO、ADO連接數(shù)據(jù)庫,或者輕松的創(chuàng)建ActiveX控件。程序員可以輕松的使用VB提供的組件快速建立一個應(yīng)用程序。4.1.1窗體控件控件就是構(gòu)成或者說建造Visual Basic應(yīng)用程序的圖形化工具,包括窗體、按鈕、復(fù)選框、列表框、數(shù)據(jù)控件、表格控件和圖片控件等等.。每個控件都有自己的屬性和事件。默認的屬性值會在控件

3、創(chuàng)建的時候提供,但是程序員也可以進行更改。很多的屬性值可以在運行時候隨著用戶的動作和修改進行改動,這樣就形成了一個動態(tài)的程序。 VB的組件既可以擁有用戶界面,也可以沒有。這樣一來服務(wù)器端程序就可以處理增加的模塊。41.2VB的程序可以包含一個或多個窗體,或者是一個主窗體和多個子窗體。有很少功能的對話框窗口(比如沒有最大化和最小化按鈕的窗體)可以用來提供彈出功能事件由用戶或操作系統(tǒng)引發(fā)的動作。事件的示例有擊鍵、單擊鼠標(Click)、雙擊鼠標(DblClick)、一段時間的限制,或從端口接收數(shù)據(jù)。4.2 RS-232串口通信原理串口是計算機上一種非常通用設(shè)備通信的協(xié)議,大多數(shù)計算機包含兩個基于R

4、S-232的串口。串口同時也是儀器儀表設(shè)備通用的通信協(xié)議;很多GPIB兼容的設(shè)備也帶有RS-232口。同時,串口通信協(xié)議也可以用于獲取遠程采集設(shè)備的數(shù)據(jù)。電氣特性:RS-232標準的信號傳輸?shù)淖畲箅娎|長度為30M; RS-232C上傳送的數(shù)字量采用負邏輯,且與地對稱。邏輯1:-3-15V   (單片機一般為-10V)邏輯0:+3+15V   (單片機一般為+10V)4.3串口通信串口通信的概念非常簡單,串口按位(bit)發(fā)送和接收字節(jié)。盡管比按字節(jié)(byte)的并行通信慢,但是串口可以在使用一根線發(fā)送數(shù)據(jù)的同時用另一根線接收數(shù)據(jù)。它很簡單并且能夠?qū)崿F(xiàn)遠距

5、離通信。典型地,串口用于ASCII碼字符的傳輸。通信使用3根線完成:地線,發(fā)送,接收。由于串口通信是異步的,端口能夠在一根線上發(fā)送數(shù)據(jù)同時在另一根線上接收數(shù)據(jù)。其他線用于握手,但是不是必須的。串口通信最重要的參數(shù)是波特率、數(shù)據(jù)位、停止位和奇偶校驗。對于兩個進行通行的端口,這些參數(shù)必須匹配:波特率:這是一個衡量通信速度的參數(shù)。它表示每秒鐘傳送的bit的個數(shù)。例如300波特表示每秒鐘發(fā)送300個bit。當我們提到時鐘周期時,我們就是指波特率例如如果協(xié)議需要4800波特率,那么時鐘是4800Hz。這意味著串口通信在數(shù)據(jù)線上的采樣率為4800Hz。通常電話線的波特率為14400,28800和36600

6、。波特率可以遠遠大于這些值,但是波特率和距離成反比。高波特率常常用于放置的很近的儀器間的通信,典型的例子就是GPIB設(shè)備的通信。數(shù)據(jù)位:這是衡量通信中實際數(shù)據(jù)位的參數(shù)。當計算機發(fā)送一個信息包,實際的數(shù)據(jù)不會是8位的,標準的值是5、7和8位。如何設(shè)置取決于你想傳送的信息。比如,標準的ASCII碼是0127(7位)。擴展的ASCII碼是0255(8位)。如果數(shù)據(jù)使用簡單的文本(標準 ASCII碼),那么每個數(shù)據(jù)包使用7位數(shù)據(jù)。每個包是指一個字節(jié),包括開始/停止位,數(shù)據(jù)位和奇偶校驗位。由于實際數(shù)據(jù)位取決于通信協(xié)議的選取,術(shù)語“包”指任何通信的情況。停止位:用于表示單個包的最后一位。典型的值為1,1.

7、5和2位。由于數(shù)據(jù)是在傳輸線上定時的,并且每一個設(shè)備有其自己的時鐘,很可能在通信中兩臺設(shè)備間出現(xiàn)了小小的不同步。因此停止位不僅僅是表示傳輸?shù)慕Y(jié)束,并且提供計算機校正時鐘同步的機會。適用于停止位的位數(shù)越多,不同時鐘同步的容忍程度越大,但是數(shù)據(jù)傳輸率同時也越慢。奇偶校驗位:在串口通信中一種簡單的檢錯方式。有四種檢錯方式:偶、奇、高和低。當然沒有校驗位也是可以的。對于偶和奇校驗的情況,串口會設(shè)置校驗位(數(shù)據(jù)位后面的一位),用一個值確保傳輸?shù)臄?shù)據(jù)有偶個或者奇?zhèn)€邏輯高位。例如,如果數(shù)據(jù)是011,那么對于偶校驗,校驗位為0,保證邏輯高的位數(shù)是偶數(shù)個。如果是奇校驗,校驗位位1,這樣就有3個邏輯高位。高位和低

8、位不真正的檢查數(shù)據(jù),簡單置位邏輯高或者邏輯低校驗。這樣使得接收設(shè)備能夠知道一個位的狀態(tài),有機會判斷是否有噪聲干擾了通信或者是否傳輸和接收數(shù)據(jù)是否不同步.五、實驗步驟:5.1創(chuàng)建項目文件創(chuàng)建一個基于對話框程序,其中設(shè)置“自動發(fā)送”、“手動發(fā)送”、“開始接收”、“設(shè)置參數(shù)”和“清除”按鈕,分別用于自動向串口發(fā)送數(shù)據(jù)、手動向串口發(fā)送數(shù)據(jù)、從串口接收數(shù)據(jù)和設(shè)置串口參數(shù)(串口參數(shù),發(fā)送字符,自動發(fā)送間隔);一個對話框,響應(yīng)“設(shè)置參數(shù)”按鈕操作,用于設(shè)置串口參數(shù),發(fā)送字符、自動發(fā)送間隔。具體過程如下:(1)啟動,選擇“文件”“新建工程”命令。()在工程模板中選擇“標準EXE”,Visual Basic將自

9、動產(chǎn)生一個Form窗體,屬行都是默認設(shè)置。(3)選擇“文件”“保存工程”命令,(4)在控件工具欄上單擊鼠標右鍵,在彈出的對話框中選擇“部件”,選擇Microsoft CommControl6,0,單擊“確定”按鈕把它加入到控件工具欄中。5.2設(shè)計窗體主界面如圖所示:5.3設(shè)置參數(shù)界面從工程菜單中選擇“添加窗體”為主窗體添加對話框,如圖所示:54設(shè)計發(fā)送與接收的流程圖:開始開始初始化初始化選擇接收數(shù)據(jù)類型選擇發(fā)送數(shù)據(jù)類型設(shè)置參數(shù)設(shè)置參數(shù)接收發(fā)送結(jié)束結(jié)束發(fā)送流程圖接收流程圖5.5程序設(shè)計5.5.1建立公共模塊:(1)在菜單選擇“工程”“添加模塊”命令,則彈出“添加模塊”對話框。(2)選擇模塊圖標后

10、,單擊“打開”按鈕,則模塊已經(jīng)添加到項目中了。將此模塊命名為Main。在模塊中定義整個項目的公共變量。Public intPort As Integer '串行口號Public strSet As String '協(xié)議設(shè)置Public intTime As Integer '發(fā)送時間間隔Public blnAutoSendFlag As Boolean '發(fā)送標志Public blnReceiveFlag As Boolean '接收標志Public intOutMode As Integer '發(fā)送模式Public strSendText As

11、 String '發(fā)送文本數(shù)據(jù)Public bytSendByte() As Byte '發(fā)送二進制數(shù)據(jù)Public intHexChk As Integer '十六進制編碼標志Public intAsciiChk As Integer 'ASCII碼標志Public intAddressChk As Integer '地址標志Public intAdd48Chk As Integer '4/8位地址標志5.5.2 定義模塊變量'接收模塊Public bytReceiveByte() As Byte '接收到的字節(jié)Public i

12、ntReceiveLen As Integer '接收到的字節(jié)數(shù)'顯示模塊Public strAddress As String '地址信息Public strHex As String '十六進制編碼Public strAscii As String 'ASCII碼Public intHexWidth As Integer '顯示列數(shù)Public intOriginX As Long '橫向原點(像素)Public intOriginY As Integer '縱向原點(行)Public intLine As Integer &

13、#39;總行數(shù)'顯示常量Public Const ChrWidth = 105 '單位寬度Public Const ChrHeight = 2 * ChrWidth '單位高度Public Const BorderWidth = 210 '預(yù)留邊界Public Const LineMax = 16 '最大顯示行數(shù)'*'輸入處理'處理接收到的字節(jié)流,并保存在全局變量'bytReceiveRyte()'*Public Sub InputManage(bytInput() As Byte, intInputLenth A

14、s Integer)Dim n As Integer '定義變量及初始化ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)For n = 1 To intInputLenth Step 1bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)Next nintReceiveLen = intReceiveLen + intInputLenthEnd Sub'為輸出準備文本'保存在全局變量'strText'strHex'st

15、rAddress'總行數(shù)保存在'intLinePublic Sub GetDisplayText()Dim n As IntegerDim intValue As IntegerDim intHighHex As IntegerDim intLowHex As IntegerDim strSingleChr As String * 1Dim intAddress As IntegerDim intAddressArray(8) As IntegerDim intHighAddress As Integer strAscii = "" '設(shè)置初值str

16、Hex = ""strAddress = "" '* '獲得16進制碼和ASCII碼的字符串 '*For n = 1 To intReceiveLenintValue = bytReceiveByte(n - 1) If intValue < 32 Or intValue > 128 Then '處理非法字符strSingleChr = Chr(46) '對于不能顯示的ASCII碼,Else '用"."表示strSingleChr = Chr(intValue)End If

17、strAscii = strAscii + strSingleChr intHighHex = intValue 16 intLowHex = intValue - intHighHex * 16 If intHighHex < 10 Then intHighHex = intHighHex + 48 Else intHighHex = intHighHex + 55 End If If intLowHex < 10 Then intLowHex = intLowHex + 48 Else intLowHex = intLowHex + 55 End If strHex = str

18、Hex + " " + Chr$(intHighHex) + Chr$(intLowHex) + " "If (n Mod intHexWidth) = 0 Then '設(shè)置換行strAscii = strAscii + Chr$(13) + Chr$(10)strHex = strHex + Chr$(13) + Chr$(10)ElseEnd IfNext n'*'獲得地址字符串'*intLine = intReceiveLen intHexWidthIf (intReceiveLen - intHexWidth *

19、intLine) > 0 Then intLine = intLine + 1 End If For n = 1 To intLine intAddress = (n - 1) * intHexWidth If intAdd48Chk = 1 Then intHighAddress = 8ElseintHighAddress = 4End IfintAddressArray(0) = intAddress For m = 1 To intHighAddress intAddressArray(m) = intAddressArray(m - 1) 16 Next mFor m = 1 T

20、o intHighAddressintAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16Next m For m = 1 To intHighAddressIf intAddressArray(intHighAddress - m) < 10 Then intAddressArray(intHighAddress-m)=intAddressArray(intHighAddress - m) + Asc("0") ElseintAddressArray(intHighAddress

21、-m)=intAddressArray(intHighAddress - m) + Asc("A") - 10End IfstrAddress=strAddress+Chr$(intAddressArray(intHighAddress-m)Next mstrAddress = strAddress + Chr$(13) + Chr$(10) '設(shè)置換行 Next n '*End Sub'*'顯示輸出'*Public Sub display() Dim intViewWidth As Long '橫向?qū)挾?像素) Dim in

22、tViewLine As Integer '縱向?qū)挾?行) Dim strDisplayAddress As String Dim strDisplayHex As String Dim strDisplayAscii As String strDisplayAddress = "" strDisplayHex = "" strDisplayAscii = "" Dim intStart As Integer Dim intLenth As Integer intStart = intOriginY * (intHexWidt

23、h + 2) + 1 intLenth = intViewLine * (intHexWidth + 2) strDisplayAscii = Mid(strAscii, intStart, intLenth)End Sub'*'文本無變化的刷新'*Public Sub ScrollRedisplay() Call displayEnd Sub'*'文本發(fā)生變化的刷新'*Public Sub SlideRedisplay() Call GetDisplayText Call displayEnd Sub5.5.3 主窗體程序設(shè)置初始的發(fā)送和接收參

24、數(shù);設(shè)置各復(fù)選框的初始狀態(tài);初始串行口。'*'字符表示的十六進制數(shù)轉(zhuǎn)化為相應(yīng)的整數(shù)'錯誤則返回 -1'*Function ConvertHexChr(str As String) As IntegerDim test As Integertest = Asc(str)If test >= Asc("0") And test <= Asc("9") Thentest = test - Asc("0")ElseIf test >= Asc("a") And test &

25、lt;= Asc("f") Thentest = test - Asc("a") + 10ElseIf test >= Asc("A") And test <= Asc("F") Thentest = test - Asc("A") + 10Elsetest = -1 '出錯信息End IfConvertHexChr = testEnd Function'*'字符串表示的十六進制數(shù)據(jù)轉(zhuǎn)化為相應(yīng)的字節(jié)串'返回轉(zhuǎn)化后的字節(jié)數(shù)'*Function s

26、trHexToByteArray(strText As String, bytByte() As Byte) As IntegerDim HexData As Integer '十六進制(二進制)數(shù)據(jù)字節(jié)對應(yīng)值Dim hstr As String * 1 '高位字符Dim lstr As String * 1 '低位字符Dim HighHexData As Integer '高位數(shù)值Dim LowHexData As Integer '低位數(shù)值Dim HexDataLen As Integer '字節(jié)數(shù)Dim StringLen As Integ

27、er '字符串長度Dim Account As Integer '計數(shù)strTestn = "" '設(shè)初值HexDataLen = 0strHexToByteArray = 0StringLen = Len(strText)Account = StringLen 2ReDim bytByte(Account) For n = 1 To StringLenDo '清除空格hstr = Mid(strText, n, 1) n = n + 1If (n - 1) > StringLen ThenHexDataLen = HexDataLen

28、 - 1 Exit ForEnd IfLoop While hstr = " "Dolstr = Mid(strText, n, 1)n = n + 1If (n - 1) > StringLen ThenHexDataLen = HexDataLen - 1Exit ForEnd IfLoop While lstr = " "n = n - 1If n > StringLen ThenHexDataLen = HexDataLen - 1Exit ForEnd IfHighHexData = ConvertHexChr(hstr)LowHe

29、xData = ConvertHexChr(lstr)If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字符中斷轉(zhuǎn)化HexDataLen = HexDataLen - 1Exit ForElseHexData = HighHexData * 16 + LowHexDatabytByte(HexDataLen) = HexDataHexDataLen = HexDataLen + 1End IfNext n IfHexDataLen>0Then '修正最后一次循環(huán)改變的數(shù)值HexDataLen = HexDataLen - 1

30、ReDim Preserve bytByte(HexDataLen)ElseReDim Preserve bytByte(0)End IfIf StringLen = 0 Then '如果是空串,則不會進入循環(huán)體strHexToByteArray = 0ElsestrHexToByteArray = HexDataLen + 1End IfEnd FunctionPrivate Sub combo_Click() If Mbo.Text = "按ASCII碼" ThenintOutMode = 0 ElseintOutMode = 1 End IfEnd SubPr

31、ivate Sub chkAddress_Click() If chkAddress.Value = 0 Then intAddressChk = 0 Else intAddressChk = 1 End If Call ScrollRedisplayEnd SubPrivate Sub chkAddress48_Click() If chkAddress48.Value = 1 Then intAdd48Chk = 1 Else intAdd48Chk = 0 End If Call SlideRedisplayEnd SubPrivate Sub chkAscii_Click() If c

32、hkAscii.Value = 1 Then intAsciiChk = 1 Else intAsciiChk = 0 End If Call ScrollRedisplayEnd SubPrivate Sub chkHex_Click() If chkHex.Value = 0 Then intHexChk = 0 Else intHexChk = 1 End If Call ScrollRedisplayEnd SubPrivate Sub send_Click() If blnAutoSendFlag Then Main.ctrTimer.Enabled = False If Not b

33、lnReceiveFlag Then Main.ctrMSComm.PortOpen = False End If Main.send.Caption = "自動發(fā)送" Else If Not Main.ctrMSComm.PortOpen Then Main.ctrMSComm.CommPort = intPort Main.ctrMSComm.Settings = strSet Main.ctrMSComm.PortOpen = True End If Main.ctrTimer.Interval = intTime Main.ctrTimer.Enabled = Tr

34、ue Main.send.Caption = "停止發(fā)送" End If blnAutoSendFlag = Not blnAutoSendFlagEnd SubPrivate Sub clear_Click() Dim bytTemp(0) As Byte ReDim bytReceiveByte(0) intReceiveLen = 0 Call InputManage(bytTemp, 0) Call GetDisplayText Call displayEnd SubPrivate Sub send2_Click() If Not Main.ctrMSComm.Po

35、rtOpen Then Main.ctrMSComm.CommPort = intPort Main.ctrMSComm.Settings = strSet Main.ctrMSComm.PortOpen = True End If Call ctrTimer_Timer If Not blnAutoSendFlag Then Main.ctrMSComm.PortOpen = False End IfEnd SubPrivate Sub Receive_Click() If blnReceiveFlag Then If Not blnAutoSendFlag And Not blnRecei

36、veFlag Then Main.ctrMSComm.PortOpen = False End If Main.Receive.Caption = "開始接收" Else If Not Main.ctrMSComm.PortOpen Then Main.ctrMSComm.CommPort = intPort Main.ctrMSComm.Settings = strSet Main.ctrMSComm.PortOpen = True End If Main.ctrMSComm.InputLen = 0 Main.ctrMSComm.InputMode = 0 Main.c

37、trMSComm.InBufferCount = 0 Main.ctrMSComm.RThreshold = 1 Main.Receive.Caption = "停止接收" End If blnReceiveFlag = Not blnReceiveFlagEnd SubPrivate Sub setting_Click() config.Show config.Port.Text = str(intPort) config.Setting.Text = strSet config.Time.Text = str(intTime)End SubPrivate Sub ctr

38、MSComm_OnComm() Dim bytInput() As Byte Dim intInputLen As Integer Case comEvReceive If blnReceiveFlag ThenIf Not Main.ctrMSComm.PortOpen ThenMain.ctrMSComm.CommPort = intPortMain.ctrMSComm.Settings = strSetMain.ctrMSComm.PortOpen = TrueEnd If'此處添加處理接收的代碼Main.ctrMSComm.InputMode = comInputModeBin

39、aryReDimCall InputManage(bytInput, intInputLen)Call GetDisplayTextCall displayIf Not blnAutoSendFlag And Not blnReceiveFlag ThenMain.ctrMSComm.PortOpen = False End IfEnd SelectEnd SubPrivate Sub ctrTimer_Timer() Dim longth As Integer If intOutMode = 0 Then Main.txtReceive.Text = "ascii" Ma

40、in.ctrMSComm.Output = strSendText Else 'add code longth = strHexToByteArray(strSendText, bytSendByte() If longth > 0 Then Main.ctrMSComm.Output = bytSendByte End If End IfEnd Sub'*'初始化'*Private Sub Form_Load()If ctrMSComm.PortOpen = False ThenctrMSComm.PortOpen = TrueEnd If '設(shè)

41、置默認發(fā)送接收關(guān)閉狀態(tài) blnAutoSendFlag = False blnReceiveFlag = False '接收初始化 intReceiveLen = 0 '默認發(fā)送方式為ASCII intOutMode = 0 Mbo.Text = "按ASCII碼" '默認顯示寬度位數(shù)為8 intHexWidth = 8 '默認各復(fù)選框處于選定狀態(tài) intHexChk = 1 intAsciiChk = 1 intAddressChk = 1 intAdd48Chk = 1 Main.chkAddress.Value = intAddressChk Main.chkAscii.Valu

溫馨提示

  • 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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論