SoFunction
Updated on 2025-04-13

Some notes about MSCOMM controls page 2/2


2. Communication problems and solutions under Chinese Win 95/98
1. Received less data than sent
If more binary data are transmitted at one time through the MSComm control, then the data received is likely to be insufficient. For example, if the transmission rate is set to 24oobps,
2048 character data can be transferred at one time, so in most cases. You can only receive about 1,200 characters at a time, and this address is the new version of MSComm32. There is a
A bug that affects the transmission of binary data. Note that this is not a feature.
The 32-bit Windows API function (hereinafter referred to as API) uses several time-limited variables represented by the COMMTIMEOUTS structure, and WriteTotalTimeOutConstant is it.
One of them, which is set internally to 5000 (i.e. 5 seconds), is set to 5000 by Windows, which determines the time spent in the sending buffer before the communication driver stops transmission.
The length of 5 seconds means that only 600 characters can be sent at a communication speed of 1200bps, and only about 1200 characters can be sent at a 24oobps. In fact, in a
It is very likely to send more data in the buffer at one time. This bug can also cause problems, even in the case of high-speed serial port communication, even if the system is using flow control
whether it is a software stream (Xon/XofI) or a hardware stream (CTS/RTS). If the data is in the send buffer, the flow control stops transmission, if the stop time exceeds 5
In seconds. Then the data will be lost. In some environments, 5 seconds can be quite short. But don't worry, there is a new important thing to add to the MSComm control in VB 5.0/6.0 version.
The attribute is called CommID. CommID refers to the serial port handle or flag called by the API when the serial port is opened. This also means that the API interface function can be used to modify this
constant. After each serial port is turned off, Windows will automatically restore it to 5000. Therefore, after opening the serial port each time, you need to re-cut the following API statement. The code is shown in the following program.
Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type
Declare Function SetCommTimeouts Lib "Kernel32"
(BYVal hFile As Long, lpComm TimeoutsAs COMMTIMEOUTS) As Long
Declare Function GetCommTimeouts Lib "Kernel32"
(ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Dim timeouts As COMMEOUTS
Dim Ret As Long
If  = False Then
 = True
End if
Ret=GetCommTimeouts (  , timeouts )
'Set some default timeouts
 = 1
 =1
 =1
 =1
=
( \Val())*10000+1000
Ret=SetCommTimeouts(  , timeouts )
(Program 2)

2. How to send character data greater than 128
In a communication program, when data is sent one by one in a single character, each data range is 0-255 (i.e., 00-FF in hexadecimal). In single character version of English Win95 or
In the DOS version of BASIC program, you only need to convert the corresponding data into corresponding characters and send it to the communication port. But it doesn't work under Chinese Win95/98, assuming it's in Chinese
Run the following programs under Win95/98:
Dim i
For i=0 to 255
=chr(i)
Next i

I hope to get the expected data between 0-255 on the receiving end, but the result is: the first 129 data is received correctly, 0-128, and the next 127 data are 126 0s and 255.
The reason for this is that the Chinese Windows uses a double-byte character set (DBCS) system. DBCS system uses numbers between 0-128 to represent ASCII characters, which is greater than
The number of 128 is only a leading character, it only shows that it is a non-Latin-based character and does not represent the actual meaning. The above program is used when calling the CHR() function
DBCS character set, this error occurred. So, how to send data from 128? The answer is to use a character array, change the above program to:
Dim cc(255) As Byte
For i = 0 To 255
cc(i) = i
Next i
 = cc
Do
DoEvents
Loop Until  = 0
'Receive process MSComm1_OnComm()
Select Case 
Case comEvReceive
Dim Buffer As Variant, b1,i
=comInputModeBinery
 = 0
Buffer = 
For i=LBound (Buffer) To UBound (Buffer )
 Buffer ( i ) ;
Next i
Case . . . . . 

3. How to send 0 characters (00H, NULL)
It is a bit troublesome to use serial controls to send 0 characters in VisualI C++, but in VB5.0/6.0, just pay attention to the following two points:
(1) Set the properties of the MSComm control NullDiscard=False;.
(2) Use binary reception, that is, use =ComInputModeBinary to solve the problem;

4. How to send Chinese string (DBcS characters)
VB5.0/6.0 reference books indicate that the MSComm communication control cannot send or receive binary data from the double-byte character set system DBCS), which is for some Chinese and Asian countries.
The country that uses DBCS character sets cannot be said to be a pity for a great man. However, in practice, I found that using the MSComm control can also send Chinese characters. There are two specific methods:
(1) Send directly
Direct sending means equating Chinese characters with English characters. For example: = " This is a line of Chinese data!" , but the Chinese data sent by this method cannot be too much
The size of the sending buffer and receiving buffer must be set to more than twice the Chinese characters, and the operating system version of the sending and receiving system should be consistent, otherwise it will
An error occurred such as receiving or sending buffer overflow. This method is used in situations where generally demanding is not high.
(2) Indirect send
On the sending end, convert Chinese characters or characters into machine code or position code data array, and then send the converted data to the serial port. After receiving the data at the receiving end, follow
The data obtained in the opposite order are converted into corresponding Chinese characters or characters during the conversion process. To use the in-place operation, if you need to separate the high byte and low byte after obtaining the inner code of the Chinese character,
However, such functions are not provided in VB5.0/6.0. The following is a function to find the high and low bytes of integers.

Public Function HiByte(a As Integer ) 
Dim b
b= a And &HFF00
b = b / 256
If b<0 Then b = b + 256
HiByte = b
End Function

Public Function LowByte(a As Integ`er)
Dim b
b = a And &HFF
LowByte = b
End Function

5. How to use stand-alone communication test
Usually, two PCs or one PC and one microcontroller are needed after writing the communication program. After connecting the communication port, it is tested, but often there is only a single PC due to conditions.
The test project is very simple, so can it be tested? Of course, it can, and the method is very simple. For the Jiupin serial port, find an abandoned serial port mouse, peel off the mouse cable, and connect it
Just connect the threads of 2 or 3 pins; for the 25 pin serial port, find a paper pin (preferably with a plastic jacket) to straighten it, exploit the plastic at both ends and bend it in a circle at both ends.
Circle, and then directly connect it to the 2 or 3 pins of the serial port. If your heart is not safe enough, you can press 5 needles on the ground.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Regarding the usage of mscomm, improvement article...[mgwmj]©

MSCOMM control is a good thing, and if you can fully understand it, he will serve you sincerely.

After roughly looking at the topic of discussing MSCOMM in the afternoon, I felt it was necessary to talk about my experience. I usually only do hardware and have not learned software systematically, but only my spare time
I have mastered a little bit, and I have taken it out here to play with it. I don’t know if there is any mistake, but I think I have done a good job^_^

This is a VB universal serial port event driver receiver program. Receive one data packet at a time, and the data packet can be any byte, ensuring that no data is lost!
Private Sub MSComm_OnComm()
    Dim S() As Byte
    Dim SS(1024) As Byte
    Static N As Long
    Static T As Variant

    If ( = comEvReceive) Then
S=                                                                                                                              �
If (Timer - T > 0.01) Then          'The interval is more than 10MS and is considered a new package
text1=""                                                                                                                             �
            N = 0
        End If
        T = Timer
For i = 0 To UBound(S)                                                    �
             =  & Right("0" & Hex(S(i)) & "H", 3) + " "
'Received packets are cached in SS()
            N=N+UBound(S)
        Next i
    End If
End Sub
Previous page12Read the full text