The summary of the invention the technical problem to be solved in the present invention is to avoid above-mentioned the deficiencies in the prior art part and the method that proposes a kind of computer and serial port external module automatic matching communication, need not the port numbers that the user distinguishes serial ports, need not manual configuration port numbers and baud rate, easy to use.
The present invention solve the technical problem can be by realizing by the following technical solutions:
Propose the method for a kind of computer and serial port external module automatic matching communication, include computer, computer serial communication software, serial port external module, when the baud rate communication that employing is made an appointment, may further comprise the steps:
1. serial port external module initialization I/O mouth is intercepted the data that the computer serial ports sends by the baud rate of agreement
2. computer serial communication software starts receiving thread by the baud rate of arranging all computer serial ports of initialization successively, sends handshake packet to each serial ports;
3. serial port external module receives handshake packet, replys the handshake response bag;
4. after computer serial communication software received the handshake response bag that certain serial ports returns, judging on this serial ports had external connection module, writes down the port numbers of this serial ports, uses this serial ports to communicate.
Such scheme is characterised in that: computer serial communication software receives the mode of suffering from bag that disappears by the detection serial ports and discerns the port numbers that serial port external module connects the RS232 serial ports of computer automatically.Also can initiatively send handshake packet by serial port external module, computer serial communication software receives the port numbers that the message bag judges that serial port external module connects by detecting certain serial ports.Why adopting the scheme that is initiatively sent handshake packet, detection handshake response bag by computer serial communication software, is to consider to unite with the technical scheme that serial port external module calculates the baud rate of computer RS232 serial ports automatically by detection waveform.
Such scheme is applicable to the situation that computer and serial port external module adopt the baud rate of making an appointment to communicate by letter.The user can be connected to serial port external module on arbitrary port of computer serial ports, need not user's manual configuration port numbers, and system can discern port numbers automatically and communicate.
Do not arrange to propose the method that a kind of serial port external module detects computer serial ports baud rate automatically under the situation of baud rate at computer and serial port external module, include computer, computer serial communication software, serial port external module, may further comprise the steps:
1. serial port external module initialization I/O mouth, circulation is intercepted the computer serial ports and whether is sent data;
2. computer serial communication software starts receiving thread by default baud rate all computer serial ports of initialization successively, sends handshake packet to each serial ports;
3. serial port external module with the counter measures low level retention time, calculates every bit time width when detecting computer serial ports transmission data, determines that computer sends the baud rate of data;
4. serial port external module is replied the handshake response bag according to the 3. definite baud rate of step to computer;
5. after computer serial communication software received the handshake response bag that certain serial ports returns, judging on this serial ports had external connection module, writes down the port numbers of this serial ports, uses this serial ports to communicate;
Compare with prior art, technique effect of the present invention is: the port numbers of the computer serial ports of discerning serial port external module automatically and being connected, and serial port external module detects the coupling baud rate automatically, need not the user and distinguishes the computer end slogan, need not manual configuration, easy to use.Technical scheme of the present invention realizes that cost is low, and interface is clear, can be embedded into easily in the schemes such as domestic electronic appliances, Industry Control, security identification.
Embodiment is described in further detail below in conjunction with the most preferred embodiment shown in the accompanying drawing.
As shown in Figure 1, computer (1) has four serial ports, comprises COM1 (13), COM2 (14), COM3 (15), COM4 (16); Serial communication software (12) is in charge of all serial initialization, data transmission/reception, and judges serial port external module (2) with which serial ports links to each other; Serial port external module (2) is connected any one serial ports of computer (1).Serial communication software provides the uniform communication interface to call for it to using software (11), and has shielded serial communication details such as port numbers, baud rate; Preferably, serial communication software (12) calls with software (11) as a dynamic link library supply, also can be packaged into a class and be embedded in the application software (11).
By the multi-serial-port card equipment of special use, computer (1) can provide the serial ports more than four, and serial communication software (12) adopts same management process to all serial ports.
Fig. 2 is the circuit theory diagrams of serial port external module (2).Serial port external module (2) is connected to any one serial ports of computer (1), obtains the 5V power supply by the stealing circuit from computer (1) serial ports; Circuit is a Central Control Module with 8 single-chip microcomputer EM78P156, realizes serial communication with software mode, and single-chip processor i/o pin P61 is responsible for sending data to computer (1) serial ports, and single-chip processor i/o pin P62 is responsible for receiving the serial data from computer (1).
Described serial port external module circuit is a preferred version cheaply, also has other to adopt special chip to realize the scheme of RS232 telecommunication circuit on the market, no longer describes in detail here.
Fig. 3 software flow pattern that to be computer (1) communicate by letter with the agreement baud rate with serial port external module (2), details are as follows for its flow process:
Serial port external module (2) initialization I/O mouth is intercepted the data that the computer serial ports sends by the baud rate of agreement;
Computer serial communication software (12) starts receiving thread by the baud rate of arranging initialization serial ports COM1 (13), COM2 (14), COM3 (15), COM4 (16) successively, sends handshake packet to each serial ports;
Serial port external module (2) receives handshake packet, replys the handshake response bag by the baud rate of agreement;
Computer serial communication software (12) is judged to be connected with serial port external module (2) on this serial ports after receiving the handshake response bag that certain serial ports returns, and writes down the port numbers of this serial ports, uses this serial ports to communicate; Other serial ports does not receive to shake hands and replys bag that computer serial communication software (12) judges on other serial ports there is not serial port external module, closes other port at the appointed time.
Fig. 4 is that computer (1) is not arranged baud rate with serial port external module (2), mates the software flow pattern of baud rate automatically; Fig. 4-the 1st, computer (1) and serial port external module (2) software main flow chart, Fig. 4-the 2nd, serial port external module (2) detects the level waveform synoptic diagram of baud rate according to handshake packet, Fig. 4-the 3rd, serial port external module (2) detects the baud rate software flow pattern;
Shown in Fig. 4-1, details are as follows with serial port external module (2) software main flow for computer (1):
Serial port external module (2) initialization I/O mouth is intercepted the computer serial ports and whether is sent data, and promptly whether cycle detection single-chip microcomputer input pin is low level (start bit that RS232 sends data is a low level);
Computer serial communication software (12) is pressed the default baud rate of computer initialization serial ports COM1 (13), COM2 (14), COM3 (15), COM4 (16) successively, starts receiving thread, sends handshake packet to each serial ports;
Serial port external module (2) detects the computer serial ports at the single-chip microcomputer input pin and sends data to when reaching, and calculates and the coupling baud rate according to the waveform of handshake packet; And reply the handshake response bag to computer (1) according to this baud rate;
Computer serial communication software (12) is then judged to be connected with serial port external module (2) on this serial ports after receiving the handshake response bag that certain serial ports returns, and writes down the port numbers of this serial ports, uses this serial ports to communicate; Other serial ports does not receive to shake hands and replys bag that computer serial communication software (12) judges on other serial ports there is not serial port external module, closes other port at the appointed time.
Fig. 4-2 is depicted as the waveform that the single-chip microcomputer input pin receives.Computer (1) is a byte to the handshake packet that serial port external module (2) sends, and is 0x00; Data layout is 1 start bit, 8 bit data positions, do not have parity check bit, 2 position of rests, 1 spare bits.At the single-chip microcomputer input pin, a lasting low level is formed in 1 start bit of handshake packet and 8 bit data positions; Serial ports receiver module (2) picks up counting at the negative edge that start bit begins, and to the rising edge end timing at end, the 8th bit data position, low level time width T is 9 bit time width, and every bit time width is T/9, and the calculating baud rate is 9/T.
In serial port external module (2) side circuit, because the influence of component parameter, negative edge and rising edge have the regular hour, baud rate 9/T is not accurate baud rate, but relatively near actual baud rate, serial ports receiver module (2) is adjusted compensation according to component parameter, is adapted for the baud rate of standard, as 300bps, 600bps, 1200bps, 2400bps, 4800bps, 9600bps, 19200bps, 38400bps etc.
Adopting the reason of 0x00 as handshake packet, mainly is to consider the influence of component parameter to negative edge and rising edge.0x00 can constitute 9 low level time widths, and every bit time width is mean value T/9, can drop to minimum to the influence of component parameter.By same baud rate computing method, also can adopt other byte value as handshake packet, only need calculate and adjust the compensation baud rate according to the low level time width that byte value forms.
Shown in Fig. 4-3, the software flow of serial port external module (2) detection handshake packet low level time width is as described below:
Serial port external module (2) software initialization I/O mouth is counter O reset; Whether cyclic query single-chip microcomputer input pin becomes low level, whether receives that promptly computer (1) serial ports sends the start bit of data; Become low level if detect the single-chip microcomputer input pin, the 10us that then delays time, counter adds 1, and whether inquiry single-chip microcomputer input pin becomes high level; If do not become high level, then continue time-delay, summary counter; After the single-chip microcomputer input pin became high level, according to width T=Counter Value computing time instruction time in Counter Value and the circulation * (other instruction time in the 10us+ circulation), the calculating baud rate was 9/T.Because negative edge and rising edge have the regular hour, baud rate 9/T is not accurate baud rate, and adjustment is compensated for as the standard baud rate to serial port external module (2) software according to component parameter, adopts baud rate and the computer (1) determined to communicate afterwards.