Use VB to control the serial port

Install VB

Just download from MSDN,
the serial number is all 1,
install V5V6, choose No,
use ctrl+alt+del to end the process when stuck

To use XP mode, the administrator user starts the program

New Construction

insert image description here

add controls

Right click in the left space area - Parts
insert image description here

MSCOMM
insert image description here

add code

Option Explicit
 
Private Sub Command1_Click()
    Dim a(0 To 5) As Byte
    Dim s As String
    
    a(0) = &H48
    a(1) = &H45
    a(2) = &H4C
    a(3) = &H4C
    a(4) = &H4F
    
    MSComm1.Output = a
End Sub
 
Private Sub Form_Load()
 
    MSComm1.CommPort = 4       '"选用com3串行口
    
    MSComm1.Settings = "9600,n,8,1"       '"波特率9600,无奇偶校验位,8位数据位1位停止位
    
    MSComm1.InputLen = 1       '"读取input接收缓冲区全部字节
    
    MSComm1.InBufferSize = 1024       '"设置接收缓冲区的字节长度
    
    MSComm1.InBufferCount = 0       '"清除发送缓冲区数据
    
    MSComm1.OutBufferCount = 0       '"清除接收缓冲区数据
    
    MSComm1.InputMode = 1       '"输入模式为binary
    
    MSComm1.RThreshold = 1       '"控件收到数据时将触发OnComm事件
    
    MSComm1.Handshaking = 0
    
    MSComm1.PortOpen = True
    
End Sub
 
Private Sub MSComm1_OnComm()
    Dim a() As Byte
    a = MSComm1.Input
    Dim i As Long
    Debug.Print UBound(a)
    'For i = 0 To UBound(a)
    '    Debug.Print Hex$(a(i)) & " ";
    'Next i
End Sub

Guess you like

Origin blog.csdn.net/shenchen2010/article/details/121416318