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

📄 config.frm

📁 VB上位机程序VB上位机程序VB上位机程序VB上位机程序
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Config 
   BackColor       =   &H00C0FFFF&
   Caption         =   "设置参数"
   ClientHeight    =   2985
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   3795
   LinkTopic       =   "Form1"
   ScaleHeight     =   2985
   ScaleWidth      =   3795
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton cmdOk 
      Caption         =   "确定"
      Height          =   375
      Left            =   600
      TabIndex        =   5
      Top             =   2040
      Width           =   975
   End
   Begin VB.TextBox txtSetting 
      BeginProperty Font 
         Name            =   "Times New Roman"
         Size            =   9
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1680
      TabIndex        =   4
      Text            =   "9600,n,8,1"
      Top             =   1200
      Width           =   975
   End
   Begin VB.ComboBox Combo1 
      Height          =   300
      Left            =   1680
      TabIndex        =   3
      Text            =   "Combo1"
      Top             =   600
      Width           =   975
   End
   Begin VB.CommandButton cmdCancel 
      Caption         =   "取消"
      Height          =   375
      Left            =   2040
      TabIndex        =   0
      Top             =   2040
      Width           =   855
   End
   Begin VB.Label Label2 
      BackColor       =   &H00C0FFFF&
      Caption         =   "串口设置:"
      BeginProperty Font 
         Name            =   "Times New Roman"
         Size            =   9
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   600
      TabIndex        =   2
      Top             =   1320
      Width           =   975
   End
   Begin VB.Label Label1 
      BackColor       =   &H00C0FFFF&
      Caption         =   "串口选择:"
      BeginProperty Font 
         Name            =   "Times New Roman"
         Size            =   9
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   600
      TabIndex        =   1
      Top             =   600
      Width           =   975
   End
End
Attribute VB_Name = "Config"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub cmdCancel_Click()
    
    Config.Hide
    Unload Config

End Sub

Private Sub cmdOk_Click()
        
    On Error GoTo SettingError
        
    intPort = Val(Config.Combo1.ListIndex)
    strSet = Config.txtSetting.Text
    
    
    
    If Not Main.MSComm1.PortOpen Then
        
        Main.MSComm1.CommPort = intPort
        Main.MSComm1.Settings = strSet
        Main.MSComm1.PortOpen = True
    End If
    Config.Hide
    Unload Config
    
    Exit Sub
    
SettingError:
    intPort = 2
    strSet = "9600,n,8,1"
    Config.Show
    Config.Combo1.Text = Str(intPort)
    Config.txtSetting.Text = strSet
    MsgBox (Error(Err.Number))
End Sub


Private Sub Combo1_Change()
If Combo1.ListIndex + 1 <> com_last_num Then            '选的端口跟上次一样就不检测了
    
    '先关闭上一个打开的端口
    If com_last_open_num <> 0 Then
    Main.MSComm1.PortOpen = False
    End If
    If Test_COM(Combo1.ListIndex + 1) = True Then
    com_last_open_num = 0                              '哈哈,注意此处要清零
    End If
    com_last_num = Combo1.ListIndex + 1
End Sub
'检测端口号函数'
Private Function Test_COM(com_num As Integer) As Boolean
If com_num <> com_last_num Or Command1.Caption = "打开端口" Then                        '选的端口跟上次一样就不检测了
    On Error GoTo Comm_Error
    
        Main.MSComm1.CommPort = com_num                         '这里接收传入的串口号
        Main.MSComm1.PortOpen = True
        Main.MSComm1.PortOpen = False
        Test_COM = True                                    '如果操作成功,则说明当前串口可用,返回1,表示串口可用
    Exit Function
Comm_Error:
        If Err.Number = 8002 Then
            MsgBox "串口不存在!"
        ElseIf Err.Number = 8005 Then
            MsgBox "串口已打开!"
        Else
            MsgBox "其它错误"
        End If
    Test_COM = False                                       '如果出错,则返回0
    Exit Function
    Resume Next
End If
End Function
'端口初始化子程序'
Private Sub initial_com(com_num As Integer)
 
Main.MSComm1.CommPort = com_num           ' 设置通讯串口
Main.MSComm1.Settings = "9600,n,8,1"     '设置波特率和发送字符格式
Main.MSComm1.OutBufferSize = 1024       ' 设置发送缓冲区1024Byte
Main.MSComm1.InBufferSize = 1024       '设置接收缓冲区1024Byte
Main.MSComm1.InputMode = 1
Main.MSComm1.InputLen = 0               '设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
Main.MSComm1.InBufferCount = 0
Main.MSComm1.SThreshold = 1
Main.MSComm1.RThreshold = 1        ' 每个字符到接收缓冲区都触发接收事件

MSComm1.PortOpen = True         ' 打开串口
End Sub
'界面初始化'
Private Sub Form_Load()
Combo1.Text = "COM4"
End Sub

⌨️ 快捷键说明

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