Techniques and techniques for developing communication software
National Defense University Library Technology Room Wang Wei
[Abstract] With the continuous expansion of computer application, remote communication between computers is also increasingly wide, and communication software used by remote communication between computers, can be bought on the market, but general communication software is Can send and receive files, these communication software does not meet the needs of actual work in the case. This article discusses this technology, introduces relevant knowledge and technology, and explains how to develop communication software by instance steps.
I. Introduction
This article will develop communication software development with VB5.0 communication, which has approximately 30 attributes and events (which can be found from online help). This article briefly describes its main attributes and events, and classify it.
When connecting to the modem with a serial port, it is only necessary to understand the code and role of the following five lines for the following five lines. The high level / low state state of the following five lines corresponds to the TRUE / FALSE value of the corresponding attribute of the MSCOMM control.
(1) DTR line: PC sent to MODEM, indicating whether the PC is ready.
(2) RTS line: PC sent to MODEM, indicating whether the PC allows MODEM to send back data.
(3) DSR line: MODEM is sent to PC, indicating whether MODEM has been prepared well
(4) CTS line: MODEM is sent to PC, indicating whether the MODEM is allowed to send data
(5) CD line: MODEM to PC, indicating that MOEDM has been in connection with the remote MODEM of the call.
Second, the properties of MSCOMM controls
Attributes
value
effect
CommPort
Use 1, 2, ... to represent serial port COM1, COM2 ....
Set or return the number of the serial port coupled to MODEM
Settings
Use "19200, N, 8, 1" to indicate that the transmission rate is 19,200 bps, no parity bit, 8-bit data bit, and 1 stop bit.
Set or return to the communication parameters.
Handshaking
0 No handshake protocol, regardless of traffic control.
1xon / XOFF, that is, in the data stream, in the data stream, is embedded for flow control.
2RTS / CTS automatically flows flow control (common) by signal line RTS / CTS.
3 can be all available.
Set or return the hardware handshake protocol, which is the internal protocol that the PC MODEM is agreed to control the flow rate.
Portopen
True / False can open / close the port.
Open or close the port.
Outbuffersize
The number of bytes of the buffer is transmitted, such as selecting 1024.
Set or return to the transmission buffer size.
OUTPUT
Variant type variable.
Write the data stream to the transfer buffer.
When you transfer text data, you should put the string data into a Variant variable, transfer binary data (ie bytes), you should put the BYTE type data into a Variant variable.
Inbuffersize
The number of bytes of the buffer is received, such as selecting 1024.
Set or return to the receiving buffer size.
InputMode
0 Receive textual data with the Input property.
1 Receive binary data with the INPUT attribute.
Set or returns the data type of the received data.
Inbuffercount
INTEGER
Returns the number of characters that have been passed but have not yet taken in the receiving buffer.
INPUT
When the inputMode property value is 0 (text mode), the variable contains String data.
When the inputMode property value is 1 (binary mode), the variable contains BYTE type array data.
The data read in the receiving buffer is read in.
DTRENABLED
RtSenabled
DSRHOLDING
CTSholding
Cdholding
Take the value True / False
Used to read or control the interactive state between the PC and the Modem. It is necessary to use it. For example, a command should be issued to the MODEM when reading the DSRHOLDING attribute value is TRUE. The data should be sent to the Modem when the carrier is detected.
Third, the trigger event of MSCOMM control
The MSCOMM control only uses an event oncomm, distinguishes different trigger time with seventeen values of attribute commevent. There are several of the following: (1) Commevent = 1: The number of characters in the transfer buffer has less than the Sthreshold (which can be set attribute).
(2) COMMEVENT = 2: Receive the HRESHOLD in the buffer to receive the HRESHOLD, using this event to write the process of receiving data.
(3) COMMEVENT = 3: CTS line changes.
(4) COMMEVENT = 4: The DSR line changes.
(5) COMMEVENT = 5: CD line changes.
(6) COMMEVENT = 6: The ringing signal is detected.
The other ten cases are generated when the communication is wrong, that is, the error code.
4. Communication software program implementation
1, first is the communication parameter setting, mainly to set the port number, baud rate, data bit, stop bit, parity bit, and setup hardware handshake protocols, which are simpler.
2. Send a DTR (ready) signal to Modem, as follows:
IF mscomm1.portopen dam
Mscomm1.dtrenable = true
Else
Mscomm1.dtrenable = false
ENDIF
3, when open, send some commands to modem to set the parameters, where s0 = n (n> = 1) automatically answers. N is the number of bells; E0 / E1 Off / Open Command Character response; Q0 / Q1Mode returns / not return results Code; M0 / M1 Close / Open Modem Speaker, the routine is as follows:
IF mscomm1.portopen dam
Do While Not Mscomm1.ctings: loop
Outstring = "ATS0 = 1e1Q0M0" CHR (13)
Mscomm1.output = outstring
END IF
4, dial design, need to send an ATDT command to Modem, as follows:
Mscomm1.output = "ATDT" TRIM ("Phone Number) CHR (13)
5. Send a data file after dialing, and the program is waiting to wait and determines if it is turned on. If the MODEM contains "Connect" or CDHOLDING attribute value to the PC, it indicates that the remote MODEM is connected to the remote MODEM, and data can be transferred.
The following definitions are required when programming and receiving programs:
S_FileName = "Name" CHR (5) CHR (13) CHR (10)
S_filelen = "LENTH" CHR (5) CHR (13) CHR (10)
S_filesend = "begin" chr (5) chr (13) chr (10)
Sub OpenFiletosend () 'Opens a file that wants to send
Hsend = freefile
The Open Sendfn for Binary AS HSEND 'Sendfn contains file names to be transmitted by the user.
LF & = LOF (HSEND) 'File length is LF &' Start sending file name, file length, file start and other information strings.
Dim data as vrait
Data = S_FileName
Mscomm1.output = data 'issues a "filename" file name string prompt information
Data = sendfn chr (13) chr (10)
MSComm1.output = data 'issued a file name
Data = S_Filelen
Mscomm1.output = data 'issues "Filelen" prompt string
Data = TRIM (STR (LF &)) CHR (13) CHR (10)
MSComm1.output = data 'issued a file size
Data = S_FileStar
Mscomm1.output = data 'issues "fileStart" prompt information, indicating that the following file begins.
DIM Sendarr () AS BYTE 'Defines byte Arrows
Sum = 0 'records the number of bytes sent by the accumulated
Bsize = mscomm1.outbuffersize 'Each time the block size
Redim Sendarr (1 to bsize) 'redefines the read buffer
Do While Sum IF LF & -LOC (HSEND) Bsize = lf & -loc (hsend) Redim Sendarr (1 to bsize) END IF Get Hsend, SENDARR 'Put byte from the file bytes in the byte array Sendvar = sendarr 'is transferred to a Variant type variable 'When the CTS line and the CD line are high, it can be sent, otherwise wait. T = Timer 60 L: IF mscomm1.ctsholding and mscomm1.cdholding then Mscomm1.output = sendvar 'Send Sum = SUM BSIZE 'Tired count Else IF Timer Go to l 'cycle waiting Else Go to closefile 'waiting time for more than 60 seconds, exit END IF END IF 'Waiting for the system to process DO Ret = doevents () LOOP Until Mscomm1.outBuffercount = 0 Loop 'is running. 6. Receive information and data files sent back from the MODEM, which is done by writing the handler of the ONCOMM event of the MSCOMM1 control. In order to receive a handshake signal of the text type, the inputMode property is usually made to be text mode. When the received string has "FileStar" CHR (5) CHR (13) CHR (10), the inputMode property is changed to binary mode. When the file content is received (judges by the received byte number), the InputMode property is changed to a text mode. The routines are as follows: Private static sub mscomm1_oncomm () Select Case Mscomm1.commmevent The Case ComeVReceive 'receives the buffer to get rithreshold characters Dim Data As Variant DIM N As Long Dim sjarr () as Byte N = mscomm1.inbuffercount 'The total number of buffers characters if mscomm1.inputMode = 0 THEN' text mode, the received data is placed in a string variable. Mscomm1.inputlen = 0 Data = Space (n) Data = MSCOMM1.INPUT When the ELSE 'binary mode, the received data is placed in the byte array. Redim s Jarr (1TON) Data = arr Mscomm1.inputlen = n Data = MSCOMM1.INPUT END IF 'Other CASE situation End SELECT End Sub 7. Processing routines for receiving data: Public Static Sub Handledata (DISP AS Control, N as Long, Data As Variant) 'Parameters: DISP (text box, used to display accept data) 'N is the number of bytes received this time 'Data (received data .variant) If not mscomm1.inputmode = 0 THEN 'receives a string Go to L2 END IF Disp.selstart = LEN (Disp.Text) Disp.sellength = 0 Disp.selText = Data 'Display Character Data If INSTR (1, Disp.Text, S_FileStar, 0) = 0 THEN 'If there is no start sign, it will end this process. EXITSUB ENDIF V_filename = INSTR (1, disp.text, s_filename, 0) 'find file name and file length V_filelen = INSTR (1, Disp.Text, S_Filelen, 0) Fn = MID (Disp.Text, v_filename 11, (v_filelen-v_filename-13))) HJS = freefile 'opens receiving files JSFN = Pathc "/ SJFILE / S" TRIM (STR (No)) "_" FN Open JSFN for Binary AS HJS V_filename = INSTR (1, Disp.Text, s_filename, 0) V_filelen = INSTR (1, Disp.Text, S_Filelen, 0) Fn = MID (Disp.Text, v_filename 11, (v_filelen-1) - (v_filename 11))) FL = MID (Disp.Text, v_filelen 11, v_filestar- (v_filelen 10)) Sendlen = VAL (FL) 'should receive the total byte number Sendlen Redim Jsarr (0 to n-1) JSARR = DATA 'puts the byte stream into byte arrays Puth JS, JSARR 'Writes the opened reception file Jslen = JSLEN N 'This time has received the number of bytes received Close HJS End Sub