如果你用得是串口,就可以用MSCOMM控件

解决方案 »

  1.   

    串口收发例子:VERSION 5.00
    Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX"
    Begin VB.Form Form1 
       Caption         =   "Form1"
       ClientHeight    =   4350
       ClientLeft      =   60
       ClientTop       =   345
       ClientWidth     =   7800
       LinkTopic       =   "Form1"
       ScaleHeight     =   4350
       ScaleWidth      =   7800
       StartUpPosition =   3  '窗口缺省
       Begin VB.CommandButton cmdOpen 
          Caption         =   "Open"
          Height          =   315
          Left            =   4590
          TabIndex        =   4
          Top             =   3315
          Width           =   1230
       End
       Begin VB.CommandButton cmdSend 
          Caption         =   "Send"
          Height          =   330
          Left            =   5955
          TabIndex        =   2
          Top             =   3300
          Width           =   1245
       End
       Begin VB.ListBox ListRevive 
          Height          =   1680
          Left            =   315
          TabIndex        =   1
          Top             =   1530
          Width           =   6900
       End
       Begin VB.TextBox txtSend 
          Height          =   315
          Left            =   315
          TabIndex        =   0
          Top             =   795
          Width           =   4155
       End
       Begin MSCommLib.MSComm MSComm1 
          Left            =   4920
          Top             =   630
          _ExtentX        =   1005
          _ExtentY        =   1005
          _Version        =   393216
          DTREnable       =   -1  'True
       End
       Begin VB.Label lblSend 
          AutoSize        =   -1  'True
          Caption         =   "Label1"
          Height          =   180
          Left            =   315
          TabIndex        =   3
          Top             =   1200
          Width           =   540
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Private Sub cmdOpen_Click()
        MSComm1.CommPort = 1
        MSComm1.InputMode = comInputModeBinary
        MSComm1.RThreshold = 1
        MSComm1.PortOpen = True
        MSComm1.RThreshold = 1
        cmdOpen.Enabled = False
    End SubPrivate Sub cmdSend_Click()
        Dim OutByte() As Byte
        Dim tmpstr As String
        Dim i As Integer
        
        If txtSend.Text = "" Then
            MsgBox "请输入要发送的数据!", vbExclamation, "提示"
            txtSend.SetFocus
        End If
        
        OutByte = StrConv(txtSend.Text, vbFromUnicode)
        
        tmpstr = ""
        For i = LBound(OutByte) To UBound(OutByte)
            tmpstr = tmpstr & Hex(OutByte(i)) & " "
        Next i
        lblSend.Caption = tmpstr
        
        MSComm1.Output = OutByte
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        If MSComm1.PortOpen Then MSComm1.PortOpen = False
    End SubPrivate Sub MSComm1_OnComm()
        Select Case MSComm1.CommEvent
            Case comEventBreak '收到中断讯号
            Case comEventCDTO '
            Case comEventCTSTO
            Case comEventDSRTO
            Case comEventFrame
            Case comEventOverrun '数据遗失
            Case comEventRxOver '接收缓冲区漫溢
            Case comEventRxParity '极性错误
            Case comEventTxFull '传送缓冲区漫溢
            Case comEventDCB '未预期错误
            Case comEvCD
            Case comEvCTS
            Case comEvDSR
            Case comEvRing
            Case comEvReceive '收到字符
                Dim InByte() As Byte
                Dim i As Integer
                Dim buf As String
                
                InByte = MSComm1.Input
                
                buf = ""
                For i = LBound(InByte) To UBound(InByte)
                    buf = buf & Hex(InByte(i)) & " "
                Next i
                ListRevive.AddItem buf
            Case comEvSend
            Case comEvEOF
        End Select
    End Sub
    点Open开始接受与发送