Query method for calling WIN95 API to implement serial communication

zhaozj2021-02-11  239

A query method for calling WIN95 API implementation serial communication Summary In Win95, serial communication mechanism has been improved and standardized relative to Windows 3.x. This paper combines an example from the angle of calling the API interface, specifically, a simple serial communication is implemented in a simple serial port query method. Keywords API, serial communication, Visual Basic, multi-thread, Win95visual Basic is a popular application development platform under Windows systems. Its convenient interface design allows programmers to save a lot of time and focus on the maximum energy of the application core code. VB4 has now become one of the most effective flexible mainstream development tools in Win95 systems, and call API application interfaces are the powerful expansion of VB4 function, which allows VB4 to directly control and process PC hardware resources, without Depending on the MS-DOS system service. It can be said that it is a difficulty of hiding most of the Windows programming, while providing flexibility using the Windows environment. Thus, the method of using the calling API interface in serial communication is to implement a simple and feasible solution to directly control the hardware hardware in VB. 1. Description of Win95 multi-threaded and 32-bit API describes Microsoft's Win95 operating system in 1995 is a comprehensive improvement of its old version of Windows performance. It took the lead in proposing and implementing the programming idea of ​​the application multi-thread, which is one of the most significant and important features of the Win95 system. The process is an execution instance of the application, and the thread is the path executed inside the process. As a result, the thread is a simplest code unit that can be scheduled by the system, each thread has its own set of CPU registers and stacks. Win95 arranges some CPU times for each independent thread, and the operating system provides time films to thread in rotation, so that each thread is as follows from the appearance. A multi-threaded application actually implements a multi-task extension inside, which gives the code to the characteristics of parallel execution. The main advantage of multi-threaded applications is to respond to users with as little time. Multi-thread does not mean that the code will run faster and accurately, it only means that the application can better utilize system resources (scheduling mechanisms, memory and physical devices). This can create a very flexible application that receives user input at any time and is satisfactory. Therefore, in order to establish faster, reliable and robust code in 32-bit Win95 systems, multi-threading technology is one of the key technologies that must be mastered. API (ie, Application Programming Interface) is an extremely important part of the internal WIN95. Win95's 32-bit API is mainly a series of complex functions, and the collection of messages. It can be seen as an open general function enhancement interface provided by the WIN95 system to various development systems underwater. It allows programmers to make more easily using different types of languages ​​to run the application on the Win95 system. Compared with the 16-bit API of Windows 3.x, Win32 APIs included in Win95 is the most prominent upgrade of API. It is gradually become a wide range of target platforms. The Win32 API includes more than 1,000 API calls, plus hundreds of Windows constants, messages, and data type structures included with the API, enabling a series of complex and effective tools for Win95 programming. The API interface is the most important expansion of VB function. Almost all issues with VB's standard feature cannot be handled can be solved by calling an API interface function. The API interface call not only gives VB to provide a large amount of expandable functional function. More importantly, it provides a thinking and approach to the most embarrassed VB function. Especially in low-level operations of some direct and hardware interactions, call API interface functions is often a unique way of feasibility. In summary, call the API interface and combine multi-threaded programming technology has become the most popular advanced technique in the Win95 environment. Most applications have the code to involve this advanced topic. Second, WIN95 Working Mechanism The common DOS system is mainly working in response to interrupt mode. Most of the PC serial communication program uses its BIOS block's INT14H interrupt to complete asynchronous serial communication in a query serial port.

Unlike the DOS response interruption, Windows is an event-driven and multi-user operating system that is not related to the device. At the same time, Windows prohibits application direct and hardware interactions, and programmers can only manage hardware through all kinds of drivers provided by WINDOWS. In this case, the Windows system acts as an intermediary between applications and hardware. The Windows system function contains the communication support interrupt function. The WIN95 system opens up user-defined input output buffers (ie read / write buffers) for each communication device, and the data entry and exit communication port is completed by the system background. The application only needs to complete the input / output buffer operation. The actual process is to generate a low-level hardware interrupt every receive a character. The serial driver in the WIN95 system has achieved control and puts the received characters into the input data buffer. Then return the control to the application that is running. If the input buffer data is full, the serial driver notifies the sender to stop sending data with the currently defined stream control mechanism. The data in the queue is handled in the order of "advanced first". Windows 3.x provides a Comm-VDRV driver to support serial communication, while Win95 has improved and standardized these serial communication standards, and integrating serial ports with the basic input output driver set. Win95 is like the operation of the serial port like the file operation, both of which use the same open, off and read and write functions. The following is a comparison of the corresponding serial communication functions in the 16-bit and 32-bit API: 16-bit API only provides 16 functions related to communication. Compared to these 16-bit communication functions, the communication function provided by Win95 is more rich and more powerful. In the WIN95 system, the serial port and serial communication driver is configured by a data structure, which is referred to as a Device Control Block, referred to as DCB. Win95 DCB is more complex than the DCB of Windows 3.x, which is more complete for the definition of the data structure. Below is the definition of the device control block data structure in Win95: Type DCB 'has a detailed definition in the Win95 API, you can get DCBLBLENGTH AS longbaudrate as longfbinary as longfparity as longfbinary as longfparity as longfbinary as longfparity as longfbinary as longteend typewin95 is available from VB4 API Text Viewer The status of the port provides an important GetCommState function. The function puts the configuration information of the port into a device control block DCB to obtain the configuration of the port. Another important COMSTAT structure is usually used to describe the current state of the serial port. It is defined as follows: Type Comstat has a detailed definition in the Win95 API, you can get fctshold as longfdsrhold as long in the API Text Viewer of VB4 ... Cbinque as longcboutque as longend type usually implemented serial communication in four steps: (1 ) Initialize and open the serial port according to the protocol, this is to inform Windows This application requires this serial port and blocks other applications to make them unable to use this serial port. (2) Configure this serial port. (3) Transfer data from the serial port and verify during transmission. (4) Close the serial port when this serial port is required. That is, release the serial port for other applications. In these four steps, the main program code is concentrated in step (3). Third, the implementation of the VB serial port query method (1) The mechanism of serial port query law VB serial port query is a primary operation in the query mode implementation method. When the communication program works in the "query" mode, you may not consider the problem of Win95 process and threads. When you have data on the serial port, you can go to the serial port buffer. Under this method, it is determined that the timing of serial port reading, handshake protocol, and software error correction is the main problem that the programmer should consider. The following is a flow chart when reading the received buffer data: Since this method works, it works in the query method.

The programmer must complete a considerable part of the communication status, many details (even the transformation of character properties during communication) must also be completed by the program code. Therefore, the comparative communication control method is in particular, this query method is particularly dependent on the dependence of the communication between the communication. The agreement between the communication protocols between the two parties has a great impact on the difficulty of the procedure. In the serial port query, the general serial initialization process is as follows: It is worth noting that the agreement under this method must meet the following conditions: That is, Party B is sent, Party B must enter the cycle reception state before Party A, until receiving After the character, the loop state is jumped through the judgment of the serial port reading function readfile returns. At the same time, in order to enter the "dead loop" state in order not to wait for the system to wait for reception due to the cycle, it is possible to set the number of cycles of the serial port, usually 1000 ~ 10000 times. VB4 is an extremely flexible high-level language, so that it is convenient to introduce the thinking language, and uses its GOTO steering statement to easily control the process of the program. Very flexible and convenient. (2) Comparison communication threads with communication threads to monitor communication devices by creating a communication thread outside the application's main thread, and the biggest advantage is that the program has autonomous ability to receive data. Once the communication thread query is sent to the serial port, the thread automatically sends a data received by the application, and the application can use the message to read the data from the communication device. And use the communication thread to do not occupy the CPU time. This system actually has the ability to simultaneously control multiple communication devices (such as modem). Communication threads are used in C development systems, which is closely related to the characteristics and advantages of C . Due to the characteristics of VB itself, the method of creating a communication thread is comparable to VB. And doing so, it is also very difficult to sacrifice VB and the complexity of the procedure is not economical. The serial port query is easily introduced to some thinking of assembly language, and the most streamlined code is implemented. It is not necessarily the most complete, but for general VB programmers, this method does achieving simplified ideas and reduces difficulty. Thus simple. Fourth, the program example of the serial query method is now appointed Party A is a PC, and Party B is a single-chip system (such as a card reader of the electricity meter system). The communication format is set to 2400 baud rate, 8-bit data bit, 1 bit stop bit, no parity. Below is a program example of a conventional communication protocol, the protocol is shown in the following flowchart: The following is a program example of several sub-functions of Party A (PC).

Private Function OpenThePort (cPort as String, cBaud as String, cParity as String, cData as String, tStops as String) As BooleanDim lResult as LongDim lHandle as LongDim DCB_COMM as DCBDim cDCBConfig as StringlHandle = CreateFile (cPort, GENERIC_READ Or GENERIC_WRITE, 0 &, 0 & , Open_EXISTING, 0 &, 0 & IF LHANDLE = -1 Then 'open serial port failure OpenTHEPORT = FALSEMSGBOX "The serial port may be occupied by other applications!" LRESULT = CloseHandle (LHANDLE)' First turn off the serial port After opening if Lresult = 0 ThenopentheportExit Functionend IfEnd IfcDCBConfig.band = 2400 'disposed DCBcDCBConfig.parity = NonecDCBConfig.data = 8cDCBConfig.stop = 1lResult = BuildCommDCB (cDCBConfig, DCB_COMM)' set by the user to configure a DCB structure If lResult = 0 ThenOpenThePort = FalseMsgBox "DCB device control can not be established block "Exit FunctionEnd IflResult = SetCommState (lHandle, DCB_Comm) 'actually set a serial DCBIf lResult = 0 ThenOpenThePort = FalseMsgBox" can not be established DCB device control block "Exit FunctionEnd IfOpenThePort = TrueEnd FunctionPrivate sub SendHand ()' transmission sub-process handshake Dim nchars as longstatic readbuff as string * 1static WriteBuff As string * 1DIM LPDCB AS DCBDIM LRET AS Longdim Lhandle As longdim lpover rnum as integermsgbox "Please put the card reader on the serial port 2! ", 48," Tips window "LHANDLE = OpenTheport (Comm1, 2400, none, 8, 1) Lret = purgecomm (LHANDLE, 1) 'Clear Output Buffer LRET = PurgeComm (LHANDLE, 0)' Clear Input Buffer LRET = Getcommstate (LHANDLE, LPDCB) 'Gets the status of the communication port: WriteBuff $ = CHR $ (& H8F) Lret = Writefile (LHANDLE, WRITEBUFF $, 1, NCHARS,

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

New Post(0)