VB Development Communication Software

zhaozj2021-02-11  181

VB Development Communication Software

First, the PC serial communication program mechanism commonly used PC serial communication program is 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 is in the serial communication data chain, and the computer is called DTE, ie the data terminal device; one end of 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: "Atqov1woso Kozer & C1 & D2 & Yo & WOSO1 & C1 & D2 & WOSO1 & C1 & D2 & WOSO", its meaning is: qo: return result code; V1: display result code in character form; WO: Do not return negotiation process code; SOK1: ringing once, ie answer; & c1: tracking Data carrier signal CD; & D2 When the DTR is driven from the drive, the Modem hangs into the command state; & y: MODEM re-adjusts the user program when power is powered on; & wo: Set the above setting as dynamic setting User scheme 0; once local Modem and far The MODEM is established, and 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, Most of the work in the preparation of the Modem connection remote communication software is borne by the serial communication drive 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, to access the serial communication drivers without having to consider its implementation details. 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-driven software, and MODEM communication relationships The following procedures implement 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. Software inventory communications VB3.0 prepared as follows: PC Control List: Control Name property Form1Form1Caption "PC communication" MsComm comm1CommandButton1CallCaption "dial-up" CommandButton2hangCaption "hook" CommandButton3ExitCaption "Exit" lower computer control inventory control name property Form1Form1Caption "lower computer communication" Mscomm Comm1Commandbutton1HangCaption "Mount" CommandButton2Exitcaption "Exit" Timer1Timer1Interval 3000Text (i) Text (i) 1. The serial port initialization, the setup program initializes the serial port when the program 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-loading () Comm1. Commouth Communication Comm1 in COMMPORT KA. Settingsκ "9600, N, 8, 1" 'baud rate 9600, no verification, 8-bit data bit 1 stop bit COMM1. OutbuffersizekoutPack 'Sets the output buffer size COMM1. Inbuffersizekack 'Sets the input buffer size COMM1. INPUTLEN KK1 'takes a character COMM1 from the input buffer each time. The RTSENABLEKLO-1'RTS signal is high COMM1. Rthreshold did not produce MScomm-Ev-Receive events COMM1. Portopenκ-1 'opens the serial port CDκComm1. CDHOLDING 'Take the carrier detection signal line original state COMM1. Outputκ "Atqov1woso Keri & C1 & D2 & YOSO" 'Initialization Modemdo DoeventsloopuntilComm1. Outbuffercountκ0end sub2. The upper computer dial-up call lower computer SubCallcom-click () COMM1. Outputκ "ATDT" & phonnum $ & chr $ (13) 'Direction to the Modem Dial Command Do DoeventsloopuntilComm1. Outbuffercountκ0nd sub3. The lower computer MODEM response, the connection starts the data lowering machine detection CD after the connection, and determines whether it is connected: SUB Comm1-oncomm () Select CaseComm1. Commeventcase5 'mscomm-ev-cdtimer1. EnableDκ-1 'Start Timer End SelectensEnd SubVB transmits a setup string, and 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 λCDTHENSTR1 $ -Text1 (i). TextStrin2 $ κStrin1 $ 'last string STRIN1 $ κStr1 $' Current string STRIN3 $ κFormat $ (LEN (STRIN1 $), "0000") & chR $ (8) & strin2 $ 'transmits the current string length, and the previous character String content COM M1. Outputstrin3 $ do doeventsloopuntilComm1. Outbuffercountκ0lsetimer1. EnableDκ0end Ifnd Sub The host machine detects the CD state, determines whether or not, and receives processing data. SUB Comm1-oncomm () Select Case Comm1. Commeventcase5 'MSCOMM-EV-CD Detects the CD state, determine whether communication COMM1 is connected. InbufferCountκ0 'clear input buffer COMM1. RTHRESHOLDκ4 'When there are five characters in the input buffer, Case2'MScomm-EV-Receive generates an EV-Receive event, and receives processing data do doeventsifComm1. The InbufferCountThen 'cycle is taken from the buffer STR1 $ κStr1 $ COMM1. INPUTELSEEXIT DoEnd iflooplen2k2 Kan1en1 'Current String Length Len1κVA1 (LEFT $ (STR1 $, 5))' is about to send strings length COMM1. Rthreshold Kright $ (str1 $, len2) 'Data Processing ... End SelectStr1 $ κ "" End Sub4. 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.

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

New Post(0)