Rss & SiteMap

曙海教育集團論壇 http://www.bjzhda.cn

曙海教育集團論壇
共1 條記錄, 每頁顯示 10 條, 頁簽: [1]
[瀏覽完整版]

標題:開發(fā)語言中的VB串口通訊實例

1樓
wangxinxin 發(fā)表于:2010-12-14 14:36:19
高精度電壓表(24bit) VB源程序
Dim PortValue As Integer `端口號選擇1-4
Dim value As Double `當前一次取值
Dim value2 As Double `要顯示的值
Dim valueSum As Double `和
Dim numCount As Double `算平均值是的計數(shù)個數(shù)
Dim func As Integer `功能號標志1-4
Dim valueFlag As Integer
Private Sub Check1_Click()
`自動刷新 被選中則 刷新按鈕無效
If Check1.value Then
Command1.Enabled = False
Else
Command1.Enabled = True

Command1.SetFocus
End If
End Sub
Private Sub Command1_Click()
`顯示
Call display
End Sub
Private Sub Command2_Click()
valueSum = 0 `清計數(shù)和
numCount = 1 `清計數(shù)個數(shù)
Label6.Caption = Str(numCount - 1) `顯示復位
value = 0
value2 = 0
valueFlag = 0
Call display
End Sub
Private Sub Form_Activate()
numCount = 1
value = 0
valueSum = 0
PortValue = 1
Text1.Visible = False
Label6.Caption = 0
Option1(0).value = True
Option2(0).value = True
Command1.SetFocus
Label1.Caption = Format(value2 0.000000)
For i = 0 To 3
If Option2(i).value = True Then
func = i + 1
End If
Next i
Check1.value = 1
`Call ComPortOpen
End Sub
Public Sub ComPortOpen() `開串口
With MSComm1
.CommPort = PortValue `使用COM1
.Settings = 9600N81 `設(shè)置通信口參數(shù)
.InBufferSize = 40
`設(shè)置MSComm1接收緩沖區(qū)為40字節(jié)
`.OutBufferSize = 2
`設(shè)置MSComm1發(fā)送緩沖區(qū)為2字節(jié)
.InputMode = comInputModeBinary
`設(shè)置接收數(shù)據(jù)模式為二進制形式
.InputLen = 1
`設(shè)置Input 一次從接收緩沖讀取字節(jié)數(shù)為1
`.SThreshold = 1
`設(shè)置Output 一次從發(fā)送緩沖讀取字節(jié)數(shù)為1
.InBufferCount = 0 `清除接收緩沖區(qū)
`.OutBufferCount = 0 `清除發(fā)送緩沖區(qū)
`MaxW = -99
`最大值賦初值
`MinW = 99 `最小值賦初值
`w = 0
`數(shù)據(jù)個數(shù)計數(shù)器清零
.RThreshold = 1
On Error Resume Next
`設(shè)置接收一個字節(jié)產(chǎn)生OnComm事件
If .PortOpen = False Then
`判斷通信口是否打開
.PortOpen = True `打開通信口
If Err Then `錯誤處理
msg = MsgBox( 串口 COM PortValue 無效! vbOKOnly 警告)
Exit Sub
End If
End If
End With
`MsgBox 端口已打開
End Sub
Public Sub ComPortClose() `關(guān)串口
MSComm1.PortOpen = False
` MsgBox 端口已關(guān)閉
End Sub
Private Sub MSComm1_OnComm()
Call recive
End Sub
Private Sub Option1_Click(Index As Integer)
If MSComm1.PortOpen = True Then
Call ComPortClose
End If
PortValue = Index + 1
Call ComPortOpen
End Sub
Private Sub recive() `檢測起始位并接收數(shù)據(jù)
Dim Buffer As Variant
Dim Arr() As Byte
Dim inData(5) As Byte
Dim count As Integer
Dim temp As Byte


` MsgBox OnComm
With MSComm1

Select Case .CommEvent
`判斷MSComm1通信事件
Case comEvReceive
`收到Rthreshold個字節(jié)產(chǎn)生的接收事件
Buffer = .Input
Arr = Buffer

`讀取一個接收字節(jié)
` Text1.Text = Arr(0)
If Arr(0) = H1B Then
.RThreshold = 0
Do
DoEvents
Loop Until .InBufferCount >= 4

For i = 1 To 4
`count = .InBufferCount
Buffer = .Input
Arr = Buffer
inData(i) = Arr(0)
Next i
If inData(4) = HA Then
If (inData(1) Mod 64) >= 32 Then
.RThreshold = 1
Exit Sub
End If
valueFlag = 1
`0.000003814697265625
temp = inData(1) Mod 16
If temp <= 7 Then
value = inData(1) Mod 8
value = value * 256 * 256
value = value + Val(inData(2)) * 256
value = value + Val(inData(3))
value = value * 3.814697265625E-06
`Text1.Text = Format(value 0.000000)
Else
value = inData(1) Mod 8
value = value * 256 * 256
value = value + Val(inData(2)) * 256
value = value + Val(inData(3))
value = value * 3.814697265625E-06
value = 0 - value
End If
temp = inData(1) Mod 128
` test OF
If temp >= 64 Then
If value < 0 Then
value = value - 0.000004
Else
value = value + 0.000004
End If
End If
`檢測自動刷新
If Check1.value Then
`valueFlag = 1
Call display
End If
共1 條記錄, 每頁顯示 10 條, 頁簽: [1]

Copyright © 2000 - 2009 曙海教育集團
Powered By 曙海教育集團 Version 2.2
Processed in .01563 s, 2 queries.
主站蜘蛛池模板: 国产ts最新人妖在线| 曰韩人妻无码一区二区三区综合部| 好吊妞欧美视频免费| 又大又硬又爽免费视频| 中文字幕精品无码亚洲字| 青青青免费网站在线观看| 日韩激情无码免费毛片| 国产天堂亚洲精品| 久久精品aⅴ无码中文字字幕重口| 成人午夜性视频欧美成人| 日韩精品人妻系列无码专区免费 | 在线观看人成视频免费| 亚洲黄色免费观看| 99久久99久久精品免费观看| 热re99久久精品国产99热| 在线日韩av永久免费观看| 亚洲欧美色鬼久久综合| 69av在线播放| 欧美人与动人物xxxx| 精品国产91久久久久久久a| 强行入侵粗暴h肉囚禁| 免费观看的毛片手机视频| a级毛片免费高清视频| 韩国男男腐啪GV肉视频| 日本免费精品视频| 四虎成人精品一区二区免费网站| 一级毛片在线完整免费观看| 相泽南亚洲一区二区在线播放| 国色天香精品一卡2卡3卡| 亚洲国产精品综合久久网络| my1136蜜芽入口永不失联版| 波多野结衣无内裤护士| 国产精品国产三级国产普通话 | 两个人日本免费完整版在线观看1| 992tv成人影院| 无遮挡边吃摸边吃奶边做| 国产精品一区二区久久不卡 | 999国产精品999久久久久久 | 国产日本一区二区三区| 久久久久亚洲av无码专区喷水 | 男人扒开女人的腿做爽爽视频|