📄 be01仪表通讯.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 375
Left = 240
TabIndex = 3
Top = 1800
Width = 855
End
Begin VB.TextBox Text3
Height = 375
Left = 3120
TabIndex = 2
Text = "Text3"
Top = 1080
Width = 1215
End
Begin VB.TextBox Text2
Height = 375
Left = 1800
TabIndex = 1
Text = "Text2"
Top = 1080
Width = 1095
End
Begin VB.TextBox Text1
Height = 375
Left = 360
TabIndex = 0
Text = "Text1"
Top = 1080
Width = 1215
End
Begin MSCommLib.MSComm MSC
Left = 360
Top = 240
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim Out(17) As Byte '接收var中的值
Dim var As Variant '接收MSC.input中的数值
Dim nRece As Integer '计算MSC.inputbuffer的个数
Dim i As Integer, j As Integer '随即变量,计算循环
Private Sub Form_Load()
ClearText
With MSC
.CommPort = 1 '设置Com1为通信端口
.Settings = "1200,n,8,1" '设置通信端口参数 9600赫兹、偶校验、7个数据位、1个停止位.(这里需要进一步说明的是tting="BBBB,P,D,S"。
'含义是:B:Baud Rate(波特率);P:Parity(奇偶);D:Data Bit;S:Stop Bit)
.InBufferSize = 40 '设置缓冲区接收数据为40字节
.InputLen = 1 '设置Input一次从接收缓冲读取字节数为1
.RThreshold = 1 '设置接收一个字节就产生OnComm事件
End With
End Sub
Private Sub ClearText()
Text3.Text = ""
Text2.Text = "5"
Text1.Text = ""
End Sub
Private Sub Command1_Click()
ClearText
' nRece = 0 '计数器清零
With MSC
.InputMode = comInputModeBinary '设置数据接收模式为二进制形式
.InBufferCount = 0 '清除接收缓冲区
If Not .PortOpen Then
.PortOpen = True '打开通信端口
End If
End With
End Sub
Private Sub MSC_OnComm()
DelayTime '用来延续时间
ClearText
With MSC
Select Case .CommEvent '判断通信事件
Case comEvReceive: '收到Rthreshold个字节产生的接收事件
SwichVar 1
If Out(1) = 2 Then '判断是否为数据的开始标志
.RThreshold = 0 '关闭OnComm事件接收
End If
Do
DoEvents
Loop Until .InBufferCount >= 3 '循环等待接收缓冲区>=3个字节
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 '打开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可以计算从子夜到现在所经过的秒数,在Microsoft Windows中,Timer函数可以返回一秒的小数部分)
Do While bDT
If Timer - sPrevious >= 0.3 Then bDT = False
Loop
bDT = True
End Sub
Private Sub SwichVar(ByVal nNum As Integer)
DelayTime
var = Null
var = MSC.Input
Out(nNum) = var(0)
End Sub
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -