📄 串口试验.frm
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 11070
ClientLeft = 60
ClientTop = 450
ClientWidth = 15840
LinkTopic = "Form1"
ScaleHeight = 11070
ScaleWidth = 15840
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox Text4
Height = 1575
Left = 240
MultiLine = -1 'True
TabIndex = 21
Text = "串口试验.frx":0000
Top = 8040
Width = 4815
End
Begin VB.CheckBox Check1
Caption = "定时发送"
Height = 375
Left = 240
TabIndex = 20
Top = 5640
Width = 1095
End
Begin VB.Timer Timer1
Left = 840
Top = 240
End
Begin VB.TextBox Text3
Height = 375
Left = 1440
TabIndex = 18
Text = "100"
Top = 5640
Width = 1095
End
Begin VB.CommandButton Command4
Caption = "清空"
Height = 495
Left = 3360
TabIndex = 16
Top = 7080
Width = 1095
End
Begin VB.ComboBox Combo5
Height = 300
ItemData = "串口试验.frx":006E
Left = 1800
List = "串口试验.frx":0081
TabIndex = 10
Text = "N"
Top = 3360
Width = 1095
End
Begin VB.ComboBox Combo4
Height = 300
ItemData = "串口试验.frx":00A3
Left = 1800
List = "串口试验.frx":00B0
TabIndex = 9
Text = "1"
Top = 2880
Width = 1095
End
Begin VB.ComboBox Combo3
Height = 300
ItemData = "串口试验.frx":00BF
Left = 1800
List = "串口试验.frx":00CF
TabIndex = 8
Text = "8"
Top = 2400
Width = 1095
End
Begin VB.ComboBox Combo2
Height = 300
ItemData = "串口试验.frx":00DF
Left = 1800
List = "串口试验.frx":00F8
TabIndex = 7
Text = "4800"
Top = 1920
Width = 1095
End
Begin VB.ComboBox Combo1
Height = 300
ItemData = "串口试验.frx":012B
Left = 1800
List = "串口试验.frx":0147
TabIndex = 6
Text = "COM1"
Top = 1440
Width = 1095
End
Begin VB.CommandButton Command3
Caption = "清空显示"
Height = 495
Left = 6480
TabIndex = 5
Top = 8760
Width = 975
End
Begin MSCommLib.MSComm MSComm1
Left = 120
Top = 120
_ExtentX = 1005
_ExtentY = 1005
_Version = 393216
DTREnable = -1 'True
End
Begin VB.CommandButton Command2
Caption = "发送数据"
Height = 495
Left = 240
TabIndex = 3
Top = 7080
Width = 975
End
Begin VB.TextBox Text2
Height = 8175
Left = 6480
MultiLine = -1 'True
TabIndex = 2
Top = 600
Width = 9375
End
Begin VB.TextBox Text1
Height = 855
Left = 240
MultiLine = -1 'True
TabIndex = 1
Top = 6120
Width = 4215
End
Begin VB.CommandButton Command1
Caption = "打开串口"
Height = 375
Left = 3240
TabIndex = 0
Top = 1440
Width = 1335
End
Begin VB.Label Label8
Caption = "ms"
Height = 255
Left = 2880
TabIndex = 19
Top = 5760
Width = 375
End
Begin VB.Label Label7
BackColor = &H00004080&
Caption = "采用16进制发送与接收显示"
BeginProperty Font
Name = "宋体"
Size = 24
Charset = 134
Weight = 400
Underline = -1 'True
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
ForeColor = &H00FFFF00&
Height = 495
Left = 240
TabIndex = 17
Top = 4920
Width = 5895
End
Begin VB.Label Label6
Caption = "校验位"
Height = 255
Left = 1080
TabIndex = 15
Top = 3360
Width = 615
End
Begin VB.Label Label5
Caption = "停止位"
Height = 255
Left = 1080
TabIndex = 14
Top = 2880
Width = 615
End
Begin VB.Label Label4
Caption = "数据位"
Height = 255
Left = 1080
TabIndex = 13
Top = 2400
Width = 615
End
Begin VB.Label Label3
Caption = "波特率"
Height = 255
Left = 1080
TabIndex = 12
Top = 1920
Width = 615
End
Begin VB.Label Label2
Caption = "端口"
Height = 255
Left = 1200
TabIndex = 11
Top = 1440
Width = 495
End
Begin VB.Label Label1
Caption = "接收窗口"
Height = 375
Left = 6480
TabIndex = 4
Top = 240
Width = 2055
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim str As String
Dim Buffer As Variant
'Dim Arr() As Byte
Dim i As Byte
Dim ccb As Byte
Dim textlast As String
Dim exx As Byte
'Dim bit As String
Private Sub Check1_Click()
If Check1.Value = 1 Then
Timer1.Interval = Text3.Text
Timer1.Enabled = True
Else
Timer1.Enabled = False
End If
End Sub
Private Sub Command1_Click()
If MSComm1.PortOpen = False Then
MSComm1.CommPort = Combo1.ListIndex + 1
'bit = Combo2.Text
MSComm1.Settings = Combo2.Text + "," + Left(Combo5.Text, 1) + "," + Combo3.Text + "," + Combo4.Text
'MSComm1.PortOpen = True
On Error GoTo Errdo
MSComm1.PortOpen = True
Command1.Caption = "关闭"
Combo1.Enabled = False
Combo2.Enabled = False
Combo3.Enabled = False
Combo4.Enabled = False
Combo5.Enabled = False
Command2.Enabled = True
Else
MSComm1.PortOpen = False
Command1.Caption = "打开"
Combo1.Enabled = True
Combo2.Enabled = True
Combo3.Enabled = True
Combo4.Enabled = True
Combo5.Enabled = True
Command2.Enabled = False
End If
Errdo:
If Err.Number = 8002 Then
MsgBox "串口不存在!"
ElseIf Err.Number = 8005 Then
MsgBox "串口已打开!"
End If
Text2.Text = MSComm1.Settings
End Sub
Private Sub Command2_Click()
Dim temp1 As String, temp2 As String, temp3 As Variant
Dim i As Byte
Dim ByteArray() As Byte '定义动态数组
ReDim ByteArray(0) '重定义数组大小
temp1 = Text1.Text
If Len(Text1.Text) Mod 3 = 0 And Len(Text1.Text) <> 0 Then
For i = 1 To Len(Text1.Text) Step 3
temp2 = Left(temp1, 1)
If Asc(temp2) <= 57 And Asc(temp2) >= 48 Then
temp3 = Asc(temp2) - 48
ElseIf Asc(temp2) <= 70 And Asc(temp2) >= 65 Then
temp3 = Asc(temp2) - 55
Else
temp3 = Asc(temp2) - 87
End If
temp2 = Mid(temp1, 2, 1)
If Asc(temp2) <= 57 And Asc(temp2) >= 48 Then
temp4 = Asc(temp2) - 48
ElseIf Asc(temp2) <= 70 And Asc(temp2) >= 65 Then
temp4 = Asc(temp2) - 55
Else
temp4 = Asc(temp2) - 87
End If
temp3 = temp3 * 16 + temp4
ByteArray(0) = temp3
'ByteArray(1) = temp3
MSComm1.Output = ByteArray 'Chr$(temp3)
temp1 = Right(temp1, Len(temp1) - 3)
Next i
Else
MsgBox ("请输入完整的16进制数")
End If
End Sub
Private Sub Command3_Click()
Text2.Text = ""
End Sub
Private Sub Command4_Click()
Text1.Text = ""
ccb = 0
End Sub
Private Sub Form_Load()
'MSComm1.CommPort = 5
'MSComm1.Settings = "9600,N,8,1"
Dim telist As String
Open "d:\testfile.txt" For Input As #1
Line Input #1, telist
Combo1.ListIndex = Val(Mid(telist, 1, 1)) 'itop
Combo2.ListIndex = Val(Mid(telist, 3, 1)) 'iTop
Combo3.ListIndex = Val(Mid(telist, 5, 1)) 'iTop
Combo4.ListIndex = Val(Mid(telist, 7, 1)) 'iTop
Combo5.ListIndex = Val(Mid(telist, 9, 1)) 'iTop
MSComm1.InBufferCount = 0
MSComm1.OutBufferCount = 0
MSComm1.OutBufferSize = 40
MSComm1.InputLen = 0
MSComm1.InputMode = comInputModeBinary
MSComm1.RThreshold = 1
textlast = Text1.Text
Command2.Enabled = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set fs = CreateObject("Scripting.FilesystemObject")
Set xxx = fs.createtextfile("d:\testfile.txt", True) 'curdir
xxx.writeline (Combo1.ListIndex & " " & Combo2.ListIndex & " " & Combo3.ListIndex & " " & Combo4.ListIndex & " " & Combo5.ListIndex)
xxx.Close
End Sub
Private Sub MSComm1_OnComm()
Dim intInputLen As Integer
Dim n As Integer
Dim Arr() As Byte
intInputLen = MSComm1.InBufferCount
Arr = MSComm1.Input
'Text1.Text = intInputLen
'Text1.Text = intInputLen
'Text1.Text = intInputLen
For n = 0 To intInputLen - 1
If Arr(n) <= 9 Then
Text2.Text = Text2.Text + "0" + Hex(Arr(n)) + " "
Else
Text2.Text = Text2.Text + Hex(Arr(n)) + " "
End If
Next n
End Sub
Private Sub Text1_Change()
If ccb = 2 Then
ccb = 0
Text1.Text = Text1.Text + " "
textlast = Text1.Text
End If
If exx = 1 Then
exx = 0
Text1.Text = textlast
End If
Text1.SelStart = Len(Text1.Text)
Text1.SetFocus
'Dim ccc As String
''Text1.SelStart = Len(Text1.Text)
'Text1.SetFocus
'ccc = Right(Text1.Text, 1)
'Text2.Text = Text2.Text + ccc
'If ((48 <= Asc(ccc)) & (Asc(ccc) <= 57)) Then '& (65 <= Asc(ccc) <= 70) & (97 <= Asc(ccc) <= 102)
'ccb = ccb + 1
'If ccb = 2 Then
'Text1.Text = Text1.Text + " "
'ccb = 0
'End If
'Else
'MsgBox ("请输入正确的字符")
'End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii <= 57 And KeyAscii >= 48) Or (KeyAscii >= 65 And KeyAscii <= 70) Or (97 <= KeyAscii And KeyAscii <= 102) Then
ccb = ccb + 1
'Text2.Text = ccb
'If ccb = 3 Then
'Text1.Text = Text1.Text + " "
'ccb = 0
'Text1.SelStart = Len(Text1.Text)
'Text1.SetFocus
'End If
Else
MsgBox ("请输入0-9的数字或A-F或a-f的字符")
exx = 1
End If
End Sub
Private Sub Timer1_Timer()
Dim temp1 As String, temp2 As String, temp3 As Variant
Dim i As Byte
Dim ByteArray() As Byte '定义动态数组
ReDim ByteArray(0) '重定义数组大小
temp1 = Text1.Text
If Len(Text1.Text) Mod 3 = 0 Then
For i = 1 To Len(Text1.Text) Step 3
temp2 = Left(temp1, 1)
If Asc(temp2) <= 57 And Asc(temp2) >= 48 Then
temp3 = Asc(temp2) - 48
ElseIf Asc(temp2) <= 70 And Asc(temp2) >= 65 Then
temp3 = Asc(temp2) - 55
Else
temp3 = Asc(temp2) - 87
End If
temp2 = Mid(temp1, 2, 1)
If Asc(temp2) <= 57 And Asc(temp2) >= 48 Then
temp4 = Asc(temp2) - 48
ElseIf Asc(temp2) <= 70 And Asc(temp2) >= 65 Then
temp4 = Asc(temp2) - 55
Else
temp4 = Asc(temp2) - 87
End If
temp3 = temp3 * 16 + temp4
ByteArray(0) = temp3
'ByteArray(1) = temp3
MSComm1.Output = ByteArray 'Chr$(temp3)
temp1 = Right(temp1, Len(temp1) - 3)
Next i
Else
MsgBox ("请输入完整的16进制数")
Timer1.Enabled = False
Option1.Value = False
End If
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -