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

📄 serialcomms.frm

📁 VB串口通信的方法
💻 FRM
字号:
VERSION 5.00
Begin VB.Form frmSerial 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "API串口通讯模块 枕善居 http://www.mndsoft.com"
   ClientHeight    =   4680
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   6540
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   4680
   ScaleWidth      =   6540
   StartUpPosition =   3  '窗口缺省
   Begin VB.Timer TMRComm 
      Enabled         =   0   'False
      Interval        =   1000
      Left            =   5430
      Top             =   4230
   End
   Begin VB.Frame Frame1 
      ForeColor       =   &H00C00000&
      Height          =   3015
      Left            =   90
      TabIndex        =   7
      Top             =   1440
      Width           =   6390
      Begin VB.TextBox txtRec 
         Enabled         =   0   'False
         Height          =   1395
         Left            =   105
         MultiLine       =   -1  'True
         ScrollBars      =   2  'Vertical
         TabIndex        =   10
         Tag             =   "NC"
         Top             =   1500
         Width           =   6150
      End
      Begin VB.CommandButton BTNSend 
         Caption         =   "发送数据(&S)"
         Default         =   -1  'True
         Enabled         =   0   'False
         Height          =   375
         Left            =   4815
         TabIndex        =   9
         Tag             =   "NC"
         Top             =   990
         Width           =   1455
      End
      Begin VB.TextBox txt 
         Enabled         =   0   'False
         Height          =   315
         Index           =   2
         Left            =   120
         TabIndex        =   8
         Tag             =   "NC"
         Top             =   540
         Width           =   6135
      End
      Begin VB.Label Label1 
         Caption         =   "接收数据:"
         Height          =   255
         Index           =   3
         Left            =   120
         TabIndex        =   12
         Top             =   1260
         Width           =   1215
      End
      Begin VB.Label Label1 
         Caption         =   "发送到串口的字符:"
         Height          =   255
         Index           =   2
         Left            =   120
         TabIndex        =   11
         Top             =   270
         Width           =   3075
      End
      Begin VB.Shape Pic 
         BorderStyle     =   0  'Transparent
         FillColor       =   &H0000FFFF&
         FillStyle       =   0  'Solid
         Height          =   255
         Left            =   6045
         Shape           =   3  'Circle
         Top             =   180
         Width           =   195
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "串口设置"
      ForeColor       =   &H00C00000&
      Height          =   1215
      Left            =   105
      TabIndex        =   0
      Top             =   135
      Width           =   6330
      Begin VB.CommandButton BTNCloseCom 
         Cancel          =   -1  'True
         Caption         =   "关闭串口"
         Enabled         =   0   'False
         Height          =   435
         Left            =   4380
         TabIndex        =   4
         Tag             =   "NC"
         Top             =   660
         Width           =   1035
      End
      Begin VB.CommandButton BTNOpenCom 
         Caption         =   "打开串口"
         Height          =   435
         Left            =   4380
         TabIndex        =   3
         Tag             =   "NO"
         Top             =   180
         Width           =   1035
      End
      Begin VB.TextBox txt 
         Height          =   315
         Index           =   1
         Left            =   1980
         TabIndex        =   2
         Tag             =   "NO"
         Text            =   "9600,n,8,1"
         Top             =   570
         Width           =   1455
      End
      Begin VB.TextBox txt 
         Height          =   315
         Index           =   0
         Left            =   1020
         TabIndex        =   1
         Tag             =   "NO"
         Text            =   "COM1:"
         Top             =   570
         Width           =   855
      End
      Begin VB.Label Label1 
         Caption         =   "参数设置:"
         Height          =   255
         Index           =   1
         Left            =   1980
         TabIndex        =   6
         Top             =   330
         Width           =   1335
      End
      Begin VB.Label Label1 
         Caption         =   "串口:"
         Height          =   255
         Index           =   0
         Left            =   1020
         TabIndex        =   5
         Top             =   330
         Width           =   915
      End
   End
End
Attribute VB_Name = "frmSerial"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'****************************************************************************
'人人为我,我为人人
'枕善居汉化收藏整理
'发布日期:05/05/29
'描  述:API串口读写测试
'网  站:http://www.mndsoft.com/
'e-mail:mnd@mndsoft.com
'OICQ  : 88382850
'****************************************************************************
Private Sub BTNCloseCom_Click()
    TMRComm.Enabled = False
    Call fin_com
    SwitchTags
End Sub

Private Sub BTNOpenCom_Click()
    If Not Init_Com(txt(0).Text, txt(1).Text) Then
        MsgBox txt(0).Text & " 无效!"
        Exit Sub
    End If
    SwitchTags
    TMRComm.Enabled = True
End Sub

Private Sub BTNSend_Click()
    If WriteCOM32(txt(2)) & vbCr <> Len(txt(2)) Then
        MsgBox "写入错误"
        Exit Sub
    End If
    txtRec.Text = ""
    Pic.FillColor = &HFF0000
End Sub

Private Sub TMRComm_Timer()
    Dim Ans As String, i As Integer, RtnStr As String
    Ans = ReadCommPure()
    If Pic.FillColor = &HFFFFFF Then
        Pic.FillColor = &H808080
       Else
        Pic.FillColor = &HFFFFFF
    End If
    If Ans = "" Then Exit Sub
    Pic.FillColor = &HFF
    For i = 1 To Len(Ans)
        RtnStr = RtnStr & Hex(Asc(Mid$(Ans, i, 1))) & " "
    Next
    RtnStr = RtnStr & vbCrLf & vbCrLf & CleanStr(Ans)
    txtRec.Text = RtnStr
    FlushComm
End Sub

Function CleanStr(TextLine As String) As String
    Dim i As Integer, RtnStr As String
    RtnStr = ""
    For i = 1 To Len(TextLine)
        Select Case Asc(Mid$(TextLine, i, 1))
            Case &H5D
                RtnStr = RtnStr & "<ACK>"
            Case &H5B
                RtnStr = RtnStr & "<NAK>"
            Case Is >= &H30
                RtnStr = RtnStr & Mid$(TextLine, i, 1)
            Case 13
                RtnStr = RtnStr & "<CR>"
            Case 10
                RtnStr = RtnStr & "<LF>"
            Case Else
                RtnStr = RtnStr & "@"
        End Select
    Next i
    CleanStr = RtnStr
End Function

Sub SwitchTags()
    Dim xs As Control
    For Each xs In Me
        If xs.Tag <> "" Then
            xs.Enabled = Not xs.Enabled
        End If
    Next
End Sub

⌨️ 快捷键说明

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