📄 form2.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form2
Caption = "通讯窗口"
ClientHeight = 2325
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form2"
ScaleHeight = 2325
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 495
Left = 1740
TabIndex = 14
Top = 900
Width = 1215
End
Begin VB.Frame Frame2
Caption = "回放方式"
ForeColor = &H00FF0000&
Height = 975
Left = 105
TabIndex = 8
Top = 0
Width = 2130
Begin VB.OptionButton Option2
Caption = "液面|套压|回归数据"
ForeColor = &H00FF0000&
Height = 255
Index = 0
Left = 120
TabIndex = 10
Top = 270
Value = -1 'True
Width = 1935
End
Begin VB.OptionButton Option2
Caption = "回归数据"
ForeColor = &H00FF0000&
Height = 255
Index = 1
Left = 120
TabIndex = 9
Top = 570
Width = 1935
End
End
Begin VB.CommandButton Command1
Caption = "通讯"
Height = 375
Index = 0
Left = 840
TabIndex = 7
Top = 1845
Width = 960
End
Begin VB.CommandButton Command1
Caption = "返回"
Height = 375
Index = 1
Left = 3120
TabIndex = 6
Top = 1845
Width = 960
End
Begin VB.Frame Frame1
Caption = "通讯端口"
ForeColor = &H00FF0000&
Height = 975
Left = 2280
TabIndex = 1
Top = 0
Width = 2175
Begin VB.OptionButton Option1
Caption = "COM1"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 240
Index = 0
Left = 120
TabIndex = 5
Top = 270
Value = -1 'True
Width = 765
End
Begin VB.OptionButton Option1
Caption = "COM2"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 240
Index = 1
Left = 960
TabIndex = 4
Top = 270
Width = 765
End
Begin VB.OptionButton Option1
Caption = "COM3"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 240
Index = 2
Left = 120
TabIndex = 3
Top = 600
Width = 765
End
Begin VB.OptionButton Option1
Caption = "COM4"
BeginProperty Font
Name = "宋体"
Size = 10.5
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FF0000&
Height = 240
Index = 3
Left = 960
TabIndex = 2
Top = 600
Width = 765
End
End
Begin MSComctlLib.ProgressBar ProgressBar1
Height = 210
Left = 135
TabIndex = 0
Top = 1515
Width = 4125
_ExtentX = 7276
_ExtentY = 370
_Version = 393216
Appearance = 1
End
Begin MSCommLib.MSComm MSComm1
Left = 0
Top = 1755
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
BaudRate = 19200
End
Begin VB.Label Label1
Caption = "状态"
Height = 285
Index = 0
Left = 135
TabIndex = 13
Top = 1170
Width = 780
End
Begin VB.Label Label1
Caption = "..."
Height = 285
Index = 1
Left = 990
TabIndex = 12
Top = 1170
Width = 1635
End
Begin VB.Label Label1
Height = 255
Index = 2
Left = 2940
TabIndex = 11
Top = 1170
Width = 1275
End
End
Attribute VB_Name = "Form2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private getAll As Integer
Private commPort As Integer
Private SendComm(2) As Byte
Const byteAll = 32766
Const byteHG = 1330
Const commAll = &H55
Const commHG = &H44
Private Sub Command1_Click(Index As Integer)
Select Case Index
Case 0
If openComm Then
saveFile ByteToData(getComm, getAll), "c:\aa.dat", getAll
closeComm
Else
MsgBox "无法通讯,请检查设置!!!"
End If
Case 1
closeComm
Unload Me
End Select
End Sub
Private Sub Command2_Click()
Dim aa() As Long
aa = getTimeArr(10, 320, False)
End Sub
Private Sub Form_Load()
Option1_Click (0)
Option2_Click (0)
End Sub
Function openComm() As Boolean
On Error GoTo errlab
MSComm1.Settings = "19200,n,8,1" '19200波特率,无校验,8位数据位,1位停止位
MSComm1.commPort = commPort
MSComm1.InputMode = comInputModeBinary
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
End If
openComm = True
Command1(0).Enabled = False
Exit Function
errlab:
openComm = False
End Function
Function getComm() As Byte()
Dim tempData() As Byte
Dim startTime As Long
MSComm1.OutBufferCount = 0 '清空发送缓冲区
MSComm1.InBufferCount = 0 '清空接收缓冲区
MSComm1.Output = SendComm
startTime = Timer
Do
DoEvents
If Timer - startTime > 60 Then
MsgBox "等待时间过长,本次通讯被取消!"
closeComm
Exit Function
End If
ProgressBar1.Value = MSComm1.InBufferCount
Loop Until MSComm1.InBufferCount >= ProgressBar1.Max
tempData = MSComm1.Input
getComm = tempData
End Function
Function closeComm()
If MSComm1.PortOpen Then MSComm1.PortOpen = False
Command1(0).Enabled = True
End Function
Private Sub Option1_Click(Index As Integer)
commPort = Index + 1
End Sub
Private Sub Option2_Click(Index As Integer)
Dim tempComm As Byte
Dim i As Integer
getAll = Index
Select Case Index
Case 0
MSComm1.InBufferSize = byteAll
ProgressBar1.Max = byteAll
tempComm = commAll
Case 1
MSComm1.InBufferSize = byteHG
ProgressBar1.Max = byteHG
tempComm = commHG
End Select
For i = 1 To 2
SendComm(i) = tempComm
Next
End Sub
'转化字节到数据
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -