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

📄 public.bas

📁 个人写的一个串口通讯程序
💻 BAS
字号:
Attribute VB_Name = "Module1"




Function sends(str As String)

   Dim count As Integer
   Dim instring As String
   
   
    If Not Form1.MSComm1.PortOpen Then
         sends = "CAN NOT OPEN PORT!"
         Form1.MSComm1.PortOpen = True
    End If
    ' 当输入占用时,
    ' 告诉控件读入整个缓冲区。
    'Form1.MSComm1.InputLen = 0
    Form1.MSComm1.Output = str
    ' 关闭串行端口。
    Form1.MSComm1.PortOpen = False
sends = str
End Function
Function rec() As String
    Dim insting As String
    Dim count As Integer
    Dim CONT1 As Boolean
    Dim cont2 As Boolean
    CONT1 = False
    cont2 = True
    Form1.Timer1.Enabled = True
    Form1.Timer1.Interval = 2000
    If Not Form1.MSComm1.PortOpen Then
        Form1.MSComm1.PortOpen = True
    End If
    Form1.MSComm1.InputLen = 0
    Do
         DoEvents
         If Form1.Timer1.Enabled = False Then
            CONT1 = True
            Exit Do
         End If
         cont2 = True
    Loop Until Form1.MSComm1.InBufferCount >= 3
      If CONT1 Then
       rec = "TIME  OUT!缓冲区未收到数据!"
       Else
      If cont2 Then
        rec = "缓冲区已收到数据!"
      End If
    ' 从串行端口读 "OK" 响应。
       count = Form1.MSComm1.InBufferCount
    ' 关闭串行端口。
     instring = Form1.MSComm1.Input
     rec = instring
     End If
'Form1.MSComm1.PortOpen = False
End Function
Function receive() As String
    Dim insting As String
    Dim count As Integer
    If Not Form1.MSComm1.PortOpen Then
        Form1.MSComm1.PortOpen = True
    End If
    Form1.MSComm1.InputLen = 0

    ' 检查数据。
    If Form1.MSComm1.InBufferCount Then
    ' Read data.
    instring = Form1.MSComm1.Input
    Else
    instring = "缓冲区还未收到数据!"
    End If
     count = Form1.MSComm1.InBufferCount
    ' 关闭串行端口。
     receive = instring
End Function

⌨️ 快捷键说明

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