vb6.0 (automatic identification of computer port number, supporting text, hexadecimal sending)

Posted by hsn on Sat, 22 Jan 2022 08:07:35 +0100

VB is visual programming, that is, drag some icon controls onto the programming board, and then write the implementation function of each button with code (such as what happens when you press the button)

vb was very popular in the past few years and gradually faded out of people's vision in recent years, but it is very convenient to use it to write some simple upper computers, which can be used with single chip microcomputer. Such as writing a serial assistant, MCU detects the temperature and humidity and displays it on the host computer

To get down to business, I just wrote a simple host computer of serial port assistant to share with you.

First take a look at the interface:

The content is relatively rich, which is basically similar to the serial port assistant we use every day.

Introduce the important and difficult parts of this procedure

1, Serial port connection part

Automatically identifies the available ports on the computer

I checked a lot of data and found that most of the programs written by others list the serial ports one by one, which is very bad, as shown below:

                                      

The program introduced this time corresponds to the port number in the device manager one by one, rather than simply listing it.

'Function function: query the serial port number available for the computer and display it in the combo box
Private Sub Uart_Init()
Dim a
Dim temp%
temp = 0
For a = 1 To 16                                              'Cycle through 16 possible serial ports
      MSComm1.CommPort = a
      On Error Resume Next                                   'When an error occurs, it will not be interrupted(If the port available on the computer is COM1,COM2,COM4,Without this line of code a=3 An error will be reported when)
      MSComm1.PortOpen = True                                'Open serial port
      If MSComm1.PortOpen = True Then
            Combo1_select.AddItem ("COM" & a), temp          'Add the ports available for detection to the combo box
            temp = temp + 1
            MSComm1.PortOpen = False
      End If
Next

2, Send data

Timer control is adopted, which can send continuously or call sending manually

'Function: send data(Judge data format)
'How to realize: 1. Send continuously by starting the timer; 2. Call this function directly to send the word
Private Sub Timer1_Timer()
    Dim longth As Integer
    
    If Option3.Value = True Then
    intOutMode = 1
    Else
    intOutMode = 0
    End If

    strSendText = Text2.Text
    If intOutMode = 0 Then
        MSComm1.Output = strSendText
    Else
        longth = strHexToByteArray(strSendText, bytSendByte())      'As long as hex send is checked, Text2 The data in is hexadecimal ASCII code(Text form)(Hexadecimal as 1 ASCII Code 31)
        
        If longth > 0 Then
            MSComm1.Output = bytSendByte
        End If
        
    End If

End Sub

3, Accept data

OnComm event with MSComm control. When data is received, OnComm event will be triggered to receive data.

'It will occur whenever there is a communication error or event OnComm event
Private Sub MSComm1_OnComm()
    
    Dim bytInput() As Byte
    Dim intInputLen As Integer
    Dim n As Integer
    Dim teststring As String
    
    Select Case MSComm1.CommEvent
        
        
        Case comEvReceive                            'Accept event interrupt
            If Option1.Value = True Then
            MSComm1.InputMode = 1                    '1: Hexadecimal Display
            Else
            MSComm1.InputMode = 0                    '0: Text display
            End If
            
            intInputLen = MSComm1.InBufferCount
            bytInput = MSComm1.Input                 'Extract data from receive buffer(Data format: decimal ascii code)
            
            If Option1.Value = True Then
            For n = 0 To intInputLen - 1
               Text1.Text = Trim(Text1.Text) & " " & IIf(Len(Hex$(bytInput(n))) > 1, Hex$(bytInput(n)), "0" & Hex$(bytInput(n)))    'hex Functions: decimal ASCII Code returns hexadecimal String
            Next n
            Else
            teststring = bytInput
            Text1.Text = Text1.Text + teststring
            End If
            
    End Select
    Text1.SelStart = Len(Text1.Text) 'Move the cursor to the end to display the last line at a time
End Sub

The MSComm control is emphasized here.

This control is the most important control for serial communication. Serial connection and serial communication are all this control.

When a project is newly loaded, there is no such control in the left control window, as shown in the following figure:

 

Need to add manually: Project - > part - > Microsoft Comm control 6.0

 

Complete open source project: https://download.csdn.net/download/m0_59113542/76680304

The following is also in VB6 0 to do the temperature acquisition and control system, the measured data of the single chip microcomputer is transmitted to the upper computer through the serial port for display.

Open source project: https://download.csdn.net/download/m0_59113542/77294120

Welcome your valuable comments.

Have a chance to discuss the code together, hey hey!

Topics: Single-Chip Microcomputer stm32 vb6