⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 通信测试_二进制.frm

📁 VB窗口界面 支持自发自收 二进制传输 毕业设计有用哦
💻 FRM
📖 第 1 页 / 共 2 页
字号:
      Left            =   6840
      TabIndex        =   24
      Top             =   4800
      Width           =   540
   End
   Begin VB.Label Label8 
      AutoSize        =   -1  'True
      Caption         =   "0"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   15.75
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H000000FF&
      Height          =   315
      Left            =   9240
      TabIndex        =   23
      Top             =   4680
      Width           =   165
   End
   Begin VB.Label Label7 
      AutoSize        =   -1  'True
      Caption         =   "发送数据个数:"
      ForeColor       =   &H000000FF&
      Height          =   180
      Left            =   7800
      TabIndex        =   22
      Top             =   4800
      Width           =   1170
   End
   Begin VB.Label Label5 
      BorderStyle     =   1  'Fixed Single
      Caption         =   "0h"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   14.25
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00404040&
      Height          =   495
      Left            =   4560
      TabIndex        =   21
      Top             =   1920
      Width           =   615
   End
   Begin VB.Label Label6 
      AutoSize        =   -1  'True
      Caption         =   "二进制格式进行通信"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   18
         Charset         =   134
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FF0000&
      Height          =   360
      Left            =   4680
      TabIndex        =   9
      Top             =   360
      Width           =   3375
   End
   Begin VB.Label Label4 
      AutoSize        =   -1  'True
      Caption         =   "接收数据个数:"
      ForeColor       =   &H00C000C0&
      Height          =   180
      Left            =   7920
      TabIndex        =   7
      Top             =   1560
      Width           =   1170
   End
   Begin VB.Label Label3 
      AutoSize        =   -1  'True
      Caption         =   "0"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   15.75
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00C000C0&
      Height          =   315
      Left            =   9240
      TabIndex        =   6
      Top             =   1440
      Width           =   165
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      Caption         =   "接收区"
      ForeColor       =   &H00C000C0&
      Height          =   180
      Left            =   6840
      TabIndex        =   5
      Top             =   1560
      Width           =   540
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      Caption         =   "要发送的数"
      Height          =   180
      Index           =   0
      Left            =   3480
      TabIndex        =   4
      Top             =   1560
      Width           =   900
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim Sj(1000) As Integer
Dim Nn As Integer
Dim Mn As Integer
Sub Port_init()
Dim Port_num As Integer
Dim Baud_rate As String
Dim Verify As String
Dim A As String
On Error GoTo Port_err

Nn = 0
If Comm1.PortOpen = True Then Comm1.PortOpen = False

Open "G:\vb\comset.txt" For Input As #1
Input #1, Port_num
Port_Text.Text = Port_num

Input #1, Baud_rate
Baud_Text.Text = Baud_rate

Line Input #1, Verify
Verify_Text.Text = Verify

Comm1.CommPort = Port_num
A = Baud_rate & "," & Left(Verify, 1) + ",8,1"
Comm1.Settings = A$
Close #1
If Comm1.PortOpen = False Then Comm1.PortOpen = True

Exit Sub
Port_err:
If Comm1.PortOpen = True Then Comm1.PortOpen = False
MsgBox ("选择的串中不存在!")
Command4.Caption = "确认"
   Combo1.Enabled = True
   Combo2.Enabled = True
   Combo3.Enabled = True
End Sub
Sub Set_change()
Open "G:\vb\comset.txt" For Output As #1
Port_Text.Text = Combo1.Text
Baud_Text.Text = Combo2.Text
Verify_Text.Text = Combo3.Text
Print #1, Port_Text.Text
Print #1, Baud_Text.Text
Print #1, Verify_Text.Text
Close #1
Call Port_init

End Sub

Sub Delay(Dt)
Dim T1, Xx
T1 = Timer
Do While Timer - T1 < Dt
Xx = DoEvents()
Loop
End Sub
Private Sub CmdExit_Click()
End
End Sub

Private Sub CmdSend_Click()
Dim OutByte(1 To 1) As Byte
Dim ML1 As Byte
Dim i%
Frame1.Enabled = False

For i = 0 To 255
        ML1 = i
        OutByte(1) = ML1
        Comm1.Output = OutByte
        Call Delay(0.001)    '当波特率低时,此延时值要增大
        List2.AddItem ML1 & "," & Hex(ML1) & "h"
        Mn = Mn + 1
        Label8 = Mn
Next i
Frame1.Enabled = True
End Sub

Private Sub Comm1_OnComm()
Dim Buf$
Dim InByte() As Byte
Dim OutByte(1 To 1) As Byte

 Select Case Comm1.CommEvent
    Case comEvReceive   ' 收到数据
      Do While (Comm1.InBufferCount > 0)
           InByte = Comm1.Input
            Buf = Str$(InByte(0))
            Nn = Nn + 1
            Label3.Caption = Nn
            Sj(Nn) = Val(Buf)
            List1.AddItem Buf & "," & Hex(Buf) & "h"
      Loop
 End Select
'Form1.Refresh
End Sub

Private Sub Command1_Click()
Nn = 0
Label3 = Nn
List1.Clear
End Sub

Private Sub Command2_Click()
Mn = 0
Label8 = Mn
List2.Clear
End Sub

Private Sub Command3_Click()
Dim OutByte(1 To 1) As Byte
Dim ML1 As Byte
Dim X
    If Val(txtSend.Text) > 255 Then
        X = MsgBox("要发送的数不能大于255!", 48)
        Exit Sub
    End If
ML1 = Val(txtSend)
OutByte(1) = ML1
Comm1.Output = OutByte

List2.AddItem ML1 & "," & Hex(ML1) & "h"
Mn = Mn + 1
Label8 = Mn
End Sub

Private Sub Command4_Click()
If Command4.Caption = "修改" Then
   Command4.Caption = "确认"
   Combo1.Enabled = True
   Combo2.Enabled = True
   Combo3.Enabled = True
Else
   Command4.Caption = "修改"
   Combo1.Enabled = False
   Combo2.Enabled = False
   Combo3.Enabled = False
   Call Set_change
End If
End Sub

Private Sub Form_Activate()
Call Port_init
End Sub


Private Sub txtSend_Change()
On Error Resume Next
Label5 = Hex(txtSend) & "h"
End Sub

Private Sub txtSend_KeyPress(KeyAscii As Integer)
If KeyAscii <> 13 Then Exit Sub
Call Command3_Click
End Sub

Private Sub VScroll1_Change()
txtSend.Text = VScroll1.Value
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -