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

📄 frmini.frm

📁 基于VC++串口编程。经过好长时间的寻找
💻 FRM
字号:
VERSION 5.00
Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
Begin VB.Form Frmini 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "系统连接测试"
   ClientHeight    =   2205
   ClientLeft      =   3600
   ClientTop       =   2130
   ClientWidth     =   3195
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   2205
   ScaleWidth      =   3195
   Begin VB.Timer Timer1 
      Enabled         =   0   'False
      Interval        =   800
      Left            =   240
      Top             =   480
   End
   Begin VB.Timer Timer2 
      Enabled         =   0   'False
      Interval        =   15000
      Left            =   240
      Top             =   1080
   End
   Begin VB.CommandButton QuitCommand 
      Caption         =   "退出系统"
      Height          =   375
      Left            =   840
      TabIndex        =   4
      Top             =   1680
      Width           =   1695
   End
   Begin VB.CommandButton RunCommand 
      Caption         =   "系统连接与初始化"
      Height          =   375
      Left            =   840
      TabIndex        =   3
      Top             =   1080
      Width           =   1695
   End
   Begin VB.ComboBox Combo1 
      BeginProperty DataFormat 
         Type            =   0
         Format          =   "0"
         HaveTrueFalseNull=   0
         FirstDayOfWeek  =   0
         FirstWeekOfYear =   0
         LCID            =   2052
         SubFormatType   =   0
      EndProperty
      Height          =   300
      ItemData        =   "Frmini.frx":0000
      Left            =   840
      List            =   "Frmini.frx":0002
      TabIndex        =   2
      Top             =   480
      Width           =   1695
   End
   Begin MSCommLib.MSComm Comm1 
      Left            =   2520
      Top             =   0
      _ExtentX        =   1005
      _ExtentY        =   1005
      _Version        =   393216
      DTREnable       =   -1  'True
   End
   Begin VB.TextBox Text1 
      Height          =   2055
      Left            =   4560
      TabIndex        =   0
      Text            =   "Text1"
      Top             =   600
      Width           =   2415
   End
   Begin VB.Label Label1 
      Caption         =   "请选择您使用的端口:"
      Height          =   255
      Left            =   0
      TabIndex        =   5
      Top             =   120
      Width           =   1935
   End
   Begin VB.Label Label4 
      Appearance      =   0  'Flat
      BackColor       =   &H80000005&
      BackStyle       =   0  'Transparent
      Caption         =   "Label4"
      ForeColor       =   &H80000008&
      Height          =   255
      Left            =   4920
      TabIndex        =   1
      Top             =   2640
      Width           =   1095
   End
End
Attribute VB_Name = "Frmini"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim IntNo As Integer
Dim arr_return() As Variant

Private Sub Combo1_Click()
On Error GoTo err
portitem = CInt(Combo1.Text)   'PortItem为全局变量,保存串行端口号
Comm1.CommPort = portitem
Comm1.PortOpen = True        '打开端口
Exit Sub
err:
  MsgBox "对不起,您选择的端口已经被打开,请选择别的端口", vbOKOnly, "通知"

End Sub

Private Sub Form_Load()
Dim aa As Integer
For aa = 1 To 10
   Combo1.AddItem aa
   Next

IntNo = 0
End Sub

Private Sub RunCommand_Click()
Timer2.Enabled = True   ' Timer2控件开始计时
If Comm1.PortOpen = False Then    ' 如果端口没有打开,则打开端口
    Comm1.CommPort = portitem
    Comm1.PortOpen = True
           
End If
  Comm1.Output = "AT" + Chr(13) + Chr(10)   ' 发送AT指令,GSM模块应当返回"OK"
  Timer1.Enabled = True             ' Timer1控件开始计时


End Sub

Private Sub QuitCommand_Click()
If Comm1.PortOpen = True Then
Comm1.PortOpen = False
End If
Unload Me
End
End Sub




Private Sub Timer1_Timer()


If Comm1.InBufferCount > 0 Then
   Timer1.Enabled = False        'Timer1停止计时
   Call getdata                 '子函数,读取串口的短消息,存放到arr_return()数组中
                                'arr_return()为一维数组,存放的是短消息每个字节的16进制数
  If IntNo = 0 Then              ' IntNo 存放当前删除的短消息数量,初始值为0
        If arr_return(UBound(arr_return) - 4) = Hex(Asc("O")) And _
arr_return(UBound(arr_return) - 3) = Hex(Asc("K")) Then
                               '判断是否接收到"ok"字符串,接收到证明连接正常
          Comm1.Output = "AT+CMGF=1" + Chr(13) + Chr(10)   '使用AT指令设置短消息的格式为TEX                                                            'T类型
          IntNo = IntNo + 1
          Timer1.Enabled = True
        Else
          Timer1.Enabled = True
        End If
Else
      If IntNo = 16 Then              '如果IntNo计数达到16,则测试完毕
           MsgBox "测试完毕:初始化已经结束!", vbOKOnly, "通知"
           Unload Me
           MDIForm1.Show           '打开主窗口
       Else
         If arr_return(UBound(arr_return) - 4) = Hex(Asc("O")) And _
arr_return(UBound(arr_return) - 3) = Hex(Asc("K")) Then
            If IntNo < 10 Then    '按删除短消息的数量分为两种:大于10和小于10
               Comm1.Output = "AT+CMGD=" + Chr(IntNo + 48) + Chr(13) + Chr(10)  '删除短消息
               IntNo = IntNo + 1
               Timer1.Enabled = True
            Else
               Comm1.Output = "AT+CMGD=1" + Chr(IntNo + 38) + Chr(13) + Chr(10)
               IntNo = IntNo + 1
               Text1.Text = IntNo
               Timer1.Enabled = True
            End If
          End If
        End If
    End If
End If

End Sub


Public Function getdata()
Dim Count As Integer
Count = 0
Comm1.InputLen = 1     '设置Comm1的InputLen属性为1,读取缓冲区时一个一个字节的读取
While Comm1.InBufferCount > 0  '循环读取缓冲区中的数据
ReDim Preserve arr_return(Count + 1)  '动态定义数组的上限
arr_return(Count) = Hex(Asc(Comm1.Input))
Count = Count + 1
Wend

End Function

Private Sub Timer2_Timer()
Dim IntChk As String
If IntNo <> "16" Then     '判断是否已经删除完毕:如果IntNo<>"16",则还没有初始化完毕,出错!
Timer2.Enabled = False
IntChk = MsgBox("对不起,初始化发生错误,是否重新初始化?", vbOKCancel, "通知")
If IntChk = vbOK Then   '如果选择"OK",重新初始化
IntNo = 0
Timer2.Enabled = True
Call RunCommand_Click
Else
Unload Me
MDIForm1.Show
End If
End If


End Sub

⌨️ 快捷键说明

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