VB Development Communication Software (Cloud Repost)

zhaozj2021-02-08  240

VB development communication software

First, PC serial communication program mechanism

Commonly used PC serial communication programs are mostly interrupt by Biosint14h to complete asynchronous serial communication in query I / O.

The Windows system provides interrupt mode-driven serial communication drivers Comm.DRV. The communication program does not need to operate directly to the serial port, but is indirectly operated by the programming interface of the driver.

In the Windows operating system, serial communication uses the "Event Notification" to support data by block transfer. When communicating, Windows opens up a user-defined input and output buffer. Each time you receive a character, a low-level hardware interrupt is generated, and the serial driver gets control immediately, and puts the characters into the input data buffer, and then the control Applications are running. If the input data buffer is full, the driver notifies the sender to stop sending data with the currently defined stream control mechanism. Sending data also uses a similar processing mode, the application will put the data that needs to be sent in the output data buffer, and the serial port generates a low-level hardware interrupt every character in the serial port.

Second, the hardware configuration of the PC serial port remote communication

In the serial communication data chain, the computer is called DTE, ie the data terminal device; the modem is called DCE, ie the data communication device. The hardware connection of PC serial communication is divided into zero MODEM connection and modem connections.

The zero MODEM connection is directly connected to the two devices, and the MODEM connection connects two PCs through the DCE (MODEM).

The zero MODEM is connected to the connection between two DTE or DCE devices within 15m. The MODEM connection is used for long-distance communication through telephone networks or dedicated lines.

The TXD and RXD signal lines are used to transmit data information between computers and modems; RTS sends signals from the computer to MODEM, indicating that it is desirable to pass the data to modem, if the MODEM can receive this data, it will send the CTS signal to the computer; DTR from the computer The end signal is sent to the modem, indicating that the computer has been communicated, and the Modem uses the DTR signal as an initialized signal, and then send the computer to the data set ready signal DSR; when the local modem receives a remote MODEM, send RING signals After the computer; the MODEM responds, send the data carrier detection DCD signal to the computer, indicating that the connection has been established.

The modeM's working status is divided into command status and online status. When the command state is in the command state, the information transmitted by the serial port can be interpreted, setting, and operations to the MODEM via the PC serial port.

For example, the initialization command is: "Atqov1wosok KK1 & C1 & D2 & WOSO1 & C1 & D2 & WOS & WO", its meaning is:

QO: Return the result code;

V1: Display the result code in character form;

WO: Do not return the results code code of the negotiation;

SOK1: Rings once, ready to answer;

& C1: Tracking data carrier signal CD;

& D2 When the DTR is driven from the shutdown, the MODEM hangs into the command state;

& Yo: MODEM Re-adjusting the user program at power-on;

& Wo: Set the above setting as dynamic setting user scheme 0;

Once the local MODEM is connected to the remote MODEM, the Modem is in the online state and can be transmitted. At this point, the Modem does not respond directly to the received data directly.

Third, MODEM connection to remote communication software

Most of the work in the communication process is borne by the serial communication driver software.

This document is the operating environment of AST 4 / 66D, Hayes's Accura144 Fax144Modem, Windows 3.2, VB3.0 Professional. A MSCOMM is available in the VB3.0 Professional. VBX communication control. It is easy to consider its implementation details without having to consider its implementation details through the settings, detection, access to the control attribute, and access the serial communication driver without The control is "event driver", which only responds to the "OnComm" event, which can process the occurrence of the event or error, closely related to the commevent property. Communication software, communication drive software and MODEM communication relationship

The following procedure implements the basic process of communication. Including initializing serial port and modem, host machine dial call lowering machine, lower computer response and start timing transmission data, hang up stop transmission and other functions.

The list of communication software prepared by VB3.0 is as follows:

List of host control:

Control name properties

Form1Form1caption "host machine communication"

MScomm COMM1

CommandButton1Callcaption "Dial"

CommandButton2Hangcaption "Hanging"

CommandButton3Exitcaption "Exit"

Next machine control list

Control name properties

Form1form1caption "Next Machine Communication"

MScomm COMM1

CommandButton1Hangcaption "Hanging"

CommandButton2Exitcaption "Exit"

Timer1Timer1Interval 3000

Text (i) text (i)

1. Serial port initialization, setup program

When the program is initialized, the serial port is initialized. Because the hardware device that uses the RTS / CTS handshake signal only the RTS signal is set to receive data, the RTSENABLE property of the MSCOMM communication control is required to TRUE. Since the MODEM is in the command state, the commands sent by the serial port and command results will be elected in the input buffer, so that the EV-Receive event is disabled before entering the online state, avoiding data errors.

Since Windows is a multi-task system, when performing data transceiver loop processing, the DOEvents event should be called, and the control is controlled, allowing Windows to perform other tasks. However, it should be noted that doevents are carefully used when handling the ONCOMM event, avoiding the system to generate an ONCOMM event, resulting in nested, and a stack overflow error occurs.

SUB FORM-LOAD ()

COMM1. Commmport Ket Ket Oral Communication

COMM1. Settings κ "9600, n, 8, 1" 'baud rate 9600, no check, 8-bit data bit stop bit

COMM1. OutbuffersizekoutPack 'Set the output buffer size

COMM1. InBuffersize KitQack 'Settings Input Buffer Size

COMM1. INPUTLEN KK1 'takes a character from the input buffer every time

COMM1. RTSenable κ-1'RTS signal is high

COMM1. Rthreshold No Mscomm-Ev-Receive event

COMM1. Portopenk-1 'opens the serial port

CDκComm1. CDHOLDING 'Napping the carrier detection signal line original state

COMM1. Outputκ "Atqov1woso Keri & C1 & D2 & YOSO" 'Initialization Modemdo DoeventsloopuntilComm1. Outbuffercountκ0

End Sub

2. Upper machine dial-up call lowering machine

Subcallcom-click ()

COMM1. Outputκ "ATDT" & phonnum $ & chr $ (13) 'Paying the MODEM

Do DoEventsloopuntilComm1. Outbuffercountκ0

End Sub

3. The lower computer MODEM response, and the timing transmission data will be started after communication.

The lower computer detects the CD, and it is determined whether it is connected:

SUB COMM1-ONCOMM ()

Select CaseComm1. Commevent

Case5 'MSCOMM-EV-CD

Timer1. EnableDκ-1 'start timer

End SELECT

End Sub

The VB transmits the setup string, and the data reception processing is relatively simple. However, when real-time data is sent, the data is converted into a fixed length string, which greatly reduces the transmission efficiency. At the receiving end, there are two ways to take characters from the input buffer: First, use the polling method loop to determine if the InbufferCount is 0; it is also using the RTHRESHOLD attribute to trigger "OnComm" event and "commevent" attribute EV-Receive event. When the value of Rthreshold is not 0, an EV-Receive event is allowed. This property settings and returns the number of characters to be received before the communication control settings COMMEVENT feature is received and generated by the ONCOMM event. When VB transmits a long string, the RTHRESHOLD value of the receiving end is to make a corresponding change to ensure that the received data is timely and accurate.

The next machine timing sends a string "Strin3 $ κFormat $ (Len (Strin1 $), 0000") & chR $ (8) & strin2 $. STRIN1 $ is the current string, STRIN2 $ is the last string. The upper computer changes Rthreshold 4 according to the reception STRIN3 $ LEN (STRIN1 $). Thus, by sending the string length in advance, changing the RTHRESHOLD value can realize the transmission growth data.

The event driver feature of the Windows serial communication driver still has unstable defects. If INPUTLENK, it is possible to remove incomplete data. To ensure reliable, a method of reducing processing rate is taken, ensuring that the data stream sent by the next machine is completely entered into the input buffer. Set Inputlenκ1, that is, take a character from the buffer each time. Polling methods can also be used, and the input buffer is detected timing. Send data when the next machine is timed:

SUB Timer1-Timer ()

IF COMM1. CDHOLDING λCDThen

STR1 $ -Text1 (i). TEXT

STRIN2 $ κStrin1 $ 'last string

Strin1 $ κStr1 $ 'Current Strings STRIN3 $ κFormat $ (Len (Strin1 $), "0000") & chr $ (8) & strin2 $

'Transfer the current string length and the previous string content

COM m1. OutputκStrin3 $

Do DoEventsloopuntilComm1. Outbuffercountκ0

Else

Timer1. EnableDκ0

END IF

End Sub

The upper computer detects the CD state, determines whether or not, and receives processing data.

SUB COMM1-ONCOMM ()

SELECT CASE COMM1. Commevent

Case5 'MSCOMM-EV-CD detects the CD status, determine if communication

COMM1. InbufferCountκ0 'clear input buffer

COMM1. Rthreshold KoSride When there are five characters in the input buffer,

Case2'MScomm-EV-Receive generates EV-Receive events, receive processing data

Do Doevents

IFCOMM1. InbufferCountThen 'circulating data from the buffer

STR1 $ κStr1 $ COMM1. INPUT

Else

Exit do

END IF

Loop

Len2κ1en1 'Current String Length

Len1κVA1 (LEFT $ (STR1 $, 5)) 'is about to send string length

COMM1. RTHRESHOLDKLEN1 4

STRIN $ @ @ @, len2) 'Data Processing

......

End SELECT

STR1 $ κ "" "

End Sub

4. According to the set AT command "& D2", when the DTR signal is low, the Modem hangs into the command state. You can also use the serial output " ATH" CHR $ (13) string hang up.

Sub hang-click ()

IF COMM1. CDHOLDING λcdthen 'detects CD, determine if hang up

COMM1. DTRENABLEKLO0

COMM1. DTRENABLEKLO-1 'set DTR signal is high, preparing for the next communication

END IF

End Sub

5. When exiting the program, be sure to turn off the serial port.

SUB EXITCOMM ()

COMM1. Portopenkfalse

End

End Sub

Fourth, Windows Communication Mechanism and VB3.0 Communication Control Potential Defects

转载请注明原文地址:https://www.9cbs.com/read-2678.html

New Post(0)