Private Sub Form_Load()
    MSComm1.Settings = "9600,n,8,1"  ' 波特率9600bit/s,无校验,8位数据,1位停止位
    MSComm1.CommPort = 4 '设定串口,1为com1,这里根据自己对应的com序号
    MSComm1.InBufferSize = 8 '设定返回接收缓冲区的大小,以字符为单位
    MSComm1.OutBufferSize = 2
    If MSComm1.PortOpen = True Then MSComm1.PortOpen = False '关串口
    MSComm1.RThreshold = 4 '设置并返回产生oncomm 事件的字符数,以字符为单位
   
    MSComm1.SThreshold = 1 '为1,传输缓冲区(发送)的每一个字符都会使MSCOMM控件产生OnComm事件
    MSComm1.InputLen = 0 '设置从接收缓冲区读取的字数,为0读取整个缓冲区
    MSComm1.InputMode = comInputModeText '以文本方度接收
    If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
    MSComm1.InBufferCount = 0
    Me.Caption = "温度"
Text1.Text = "00.0"
End Sub
Private Sub MSComm1_OnComm()
    Dim rec As String
    Select Case MSComm1.CommEvent
    Case comEvReceive
    rec = MSComm1.Input
    Text1.Text = rec
    MSComm1.InBufferCount = 0 '清空接收缓存区
    End Select
End Sub
Option Explicit
    Dim a As Integer
    Dim BytReceived() As Byte
    Dim strData As String
    Dim lenInput As Integer
    Dim bytSendByte() As Byte    '发送二进制数据
    Dim strSendText As String    '发送文本数据
    Dim blnAutoSendFlag As Boolean
    Dim openFlag As Boolean
html获取input输入的数据Private Sub cmdOpen_Click() '打开关闭端口
    If openFlag Then
        cmdOpen.Caption = "打开串口"
        MSComm1.PortOpen = False '打开端口
        Shape1.FillColor = vbRed
        Label1 = "COM" & a & " 关闭"
    Else
        OpenPortNum
        cmdOpen.Caption = "关闭串口"
        Shape1.FillColor = vbGreen
        Label1 = "COM" & a & " 打开"
    End If
    openFlag = Not openFlag
End Sub
Private Sub cmdSendASc_Click() '文本发送
    Dim sj_Txt As String
    sj_Txt = TxtSend
    If MSComm1.PortOpen = True Then
        MSComm1.Output = sj_Txt
    End If
End Sub
Private Sub cmdSendHex_Click() '16进制发送
    Dim sj() As Byte
    Dim sj_Txt As String
    Dim i As Integer
    sj_Txt = TxtSend
    If Len(TxtSend) Mod 2 = 0 And Len(TxtSend) <> 0 Then '检验16进制字符串长
        ReDim sj(Len(sj_Txt) / 2 - 1)
        For i = 0 To Len(sj_Txt) - 1 Step 2
          sj(i / 2) = Val("&H" & Mid(sj_Txt, i + 1, 2))
        Next
        If MSComm1.PortOpen = True Then
            MSComm1.Output = sj
        Else
            MSComm1.PortOpen = True
            Shape1.FillColor = vbGreen
            MSComm1.Output = sj
        End If
    Else
        MsgBox ("格式不对!")
    End If
End Sub
'字符串表示的十六进制数据转化为相应的字节串,返回转化后的字节数
Private Sub OpenPortNum()
    On Error GoTo uerror
    For a = 1 To 16
        MSComm1.CommPort = a
        MSComm1.PortOpen = True
        If MSComm1.PortOpen = True Then
            'Print "可用Com号= "; a
            Exit Sub
        Else
        End If
    Next
    Exit Sub
uerror:
    'Print "出错或占用Com号= "; a
    Resume Next
End Sub
Private Sub Form_Load()
    MSComm1.Settings = "9600,n,8,1"
    MSComm1.InputMode = comInputModeBinary      '采用二进制传输
    MSComm1.InBufferCount = 0  '清空接受缓冲区
    MSComm1.OutBufferCount = 0  '清空传输缓冲区
    MSComm1.RThreshold = 1      '产生MSComm事件
    MSComm1.InBufferSize = 1024
    TxtSend = ""
    TxtSend = "" '800A00113135323634389794"
    txtReceive = ""
    Text2 = ""
    Call cmdOpen_Click
End Sub
Private Sub MSComm1_OnComm() '接收数据
    Dim strBuff As String
    Select Case MSComm1.CommEvent
        Case 2
            MSComm1.InputLen = 0
            strBuff = MSComm1.Input
            BytReceived() = strBuff
            jieshou
            lenInput = Len(strData)
            Text2 = lenInput \ 2
            '数据处理代码
    End Select
End Sub
Public Function jieshou() '接收数据处理为16进制
    Dim i As Integer
    For i = 0 To UBound(BytReceived)
        If Len(Hex(BytReceived(i))) = 1 Then
            strData = strData & "0" & Hex(BytReceived(i))
        Else
            strData = strData & Hex(BytReceived(i))
        End If
    Next
    txtReceive = strData
End Function

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。