產(chǎn)品列表PRODUCTS LIST
Visual Basic串口通訊調(diào)試方法
現(xiàn)有電子秤一臺(tái),使用串口與計(jì)算機(jī)進(jìn)行通訊。編寫VB程序來(lái)訪問(wèn)串口,達(dá)到讀取電子秤上顯示的數(shù)據(jù)。該電子秤為BE01型儀表,輸出為RS-232C標(biāo)準(zhǔn)接口,波特率為300-9600、偶校驗(yàn)、7個(gè)數(shù)據(jù)位、2個(gè)停止位。所有字符均發(fā)送11位ASCII碼,一個(gè)起始位。在VB中與串口通訊需要引入控件MSComm串口通訊控件(在Microsoft Comm Control 6.0中)。具體程序如下:控件簡(jiǎn)稱:MSC
Dim Out(12) As Byte '接收var中的值
Dim var As Variant '接收MSC.input中的數(shù)值
Dim nRece As Integer '計(jì)算MSC.inputbuffer的個(gè)數(shù)
Dim i As Integer, j As Integer '隨即變量,計(jì)算循環(huán)
****************************************************************************
Private Sub Form_Load()
ClearText
With MSC
.CommPort = 1 '設(shè)置Com1為通信端口
.Settings = "9600,E,7,2" '設(shè)置通信端口參數(shù) 9600赫茲、偶校驗(yàn)、7個(gè)數(shù)據(jù)位、1個(gè)停止位.(這里需要進(jìn)一步說(shuō)明的是:.Setting=”BBBB,P,D,S”。
含義是:B:Baud Rate(波特率);P:Parity(奇偶);D:Data Bit;S:Stop Bit)
.InBufferSize = 40 '設(shè)置緩沖區(qū)接收數(shù)據(jù)為40字節(jié)
.InputLen = 1 '設(shè)置Input一次從接收緩沖讀取字節(jié)數(shù)為1
.RThreshold = 1 '設(shè)置接收一個(gè)字節(jié)就產(chǎn)生OnComm事件
End With
End Sub
****************************************************************************
Private Sub ClearText()
Text3.Text = ""
Text2.Text = "5"
Text1.Text = ""
End Sub
Private Sub Command1_Click()
ClearText
' nRece = 0 '計(jì)數(shù)器清零
With MSC
.InputMode = comInputModeBinary '設(shè)置數(shù)據(jù)接收模式為二進(jìn)制形式
.InBufferCount = 0 '清除接收緩沖區(qū)
If Not .PortOpen Then
.PortOpen = True '打開(kāi)通信端口
End If
End With
End Sub
Private Sub MSC_OnComm()
DelayTime ‘用來(lái)延續(xù)時(shí)間
ClearText
With MSC
Select Case .CommEvent '判斷通信事件
Case comEvReceive: '收到Rthreshold個(gè)字節(jié)產(chǎn)生的接收事件
SwichVar 1
If Out(1) = 2 Then '判斷是否為數(shù)據(jù)的開(kāi)始標(biāo)志
.RThreshold = 0 '關(guān)閉OnComm事件接收
End If
Do
DoEvents
Loop Until .InBufferCount >= 3 '循環(huán)等待接收緩沖區(qū)>=3個(gè)字節(jié)
' nRece = nRece + 1
For i = 2 To 12
SwichVar i
Text1.Text = Text1.Text & Chr(Out(i))
Next
Text1.Text = LTrim(Text1.Text)
Text2.Text = Text2.Text & CStr(nRece)
.RThreshold = 1 '打開(kāi)MSComm事件接收
Case Else
' .PortOpen = False
End Select
End With
End Sub
****************************************************************************
Private Sub DelayTime()
Dim bDT As Boolean
Dim sPrevious As Single, sLast As Single
bDT = True
sPrevious = Timer (Timer可以計(jì)算從子夜到現(xiàn)在所經(jīng)過(guò)的秒數(shù),在Microsoft Windows中,Timer函數(shù)可以返回一秒的小數(shù)部分)
Do While bDT
If Timer - sPrevious >= 0.3 Then bDT = False
Loop
bDT = True
End Sub
(通信傳輸速率為9600bps,則zui快速度1.04ms發(fā)送一個(gè)字節(jié),儀表每秒發(fā)送50幀數(shù)據(jù),每幀數(shù)據(jù)有4個(gè)字節(jié),即每秒發(fā)送200個(gè)字節(jié),平均5.0ms 發(fā)送一個(gè)字節(jié),連續(xù)讀取串口數(shù)據(jù)時(shí)要在程序中添加循環(huán)等待程序)
Private Sub SwichVar(ByVal nNum As Integer)
DelayTime
var = Null
var = MSC.Input
Out(nNum) = var(0)
End Sub
(設(shè)置接收數(shù)據(jù)模式采用二進(jìn)制形式,即 InputMode=comInputModeBinary,但用Input屬性讀取數(shù)據(jù)時(shí),不能直接賦值給 Byte 類型變量,只能通過(guò)先賦值給一個(gè) Variant 類型變量,返回一個(gè)二進(jìn)制數(shù)據(jù)的數(shù)組,再轉(zhuǎn)換保存到Byte類型數(shù)變量中。)
Private Sub Text1_Change()
Text3.Text = CText(Text1.Text) - CText(Text2.Text)
End Sub
****************************************************************************
Private Function CText(ByVal str As String) As Currency
If str <> "" Then
CText = CCur(Val(str))
Else
CText = 0
End If
End Function