l Reprinted: Research and Implementation of Serial Communications in Computer Fax

xiaoxiao2021-03-06  38

Research and Implementation of Serial Communications in Computer Fax, Peng Aiguo Li Jianhua in modern society, fax transmission has become one of the main tools in remote communication, and with the development of computer communications and network technology, fax transmission is no longer limited to dedicated Fax machine, in fact, using computer and fax modems to implement fax function has been more and more extensive applications. The most basic part of the design fax software is also a part of serial communication, and whether the correct or not the serial communication is directly related to whether the fax can receive and send correctly. 1 Computer fax work principle Computer fax Basic idea is based on serial communication, using computer, Fax MODEM, and telephone, according to two protocols of T.30 and T.4 to simulate the fax machine, implement all fax features. The T.30 is a fax communication protocol, which specifies the process of a fax session: First, the two modems will establish a telephone connection on the PSTN; then, through a 300 bps (optional 2400bps) half-duplex synchronous connection To exchange HDLC information frames, these two modems will implement T.30 "session" with error-controlled control; third, in the start of the session, the two modems endorse acceptable parameters, then the sender will be issued A high-speed carrier pulse to verify the quality of the telephone line, then two modems will switch to a high-speed state, thereby transporting an image with a half-duplex mode; fourth, at the end of each page, modem back Go to a lower speed (usually 300bps) session protocol to negotiate the next page (if necessary, negotiating on page); Finally, the modem is broken when there is no more page transmission. T.4 is a fax image protocol, which specifies the size and coding specifications of data in the stage C in T.30, such as the size of the fax file, the resolution of the scanner / printer, the encoding algorithm used by compressed data, with The row synchronization sequence, that is, the line end code EOL, the band in the stage C, that is, the return control signal RTC, and the filler technology to be adapted to the low speed output device. 2 Differentiations of Fax and Data Transport (1) All data transmitted between the fax stations are image data, transmitted between the main system and the remote station during the interactive dialog. (2) The transmission unit is different. In the XMODEM or YMODEM protocol, the package is a transmission unit, and in the fax transmission, the term used is the frame or HDLC (Advanced Data Link Control) frame. The minimum unit of fax data is a page. (3) Data communication is typically transmitted oriented, and the fax transmission is a bitstream, and the information describing the data on one page is a series of variable bit strings. From a computer's point of view, these bit strings are used as a series of bytes to receive and transmit, and each bit of a bit string is interpreted until it is in T.4 describes the form of fax data. Something when a match is used. 3 Several methods of serial communication in the fax use Fax MODEM and computer simulation fax machines to receive and send, is based on serial communication, plus the operation of Modem, according to T.30 and T.4 protocol Implemented. There are several common serial communication methods in Windows: (1) Query method Windows 3.1, WIN95 / 98 Environment The basic programming ideology of the communication mode is basically consistent, using the CPU timing query, detect whether to receive new data or Whether the data is sent, the basic program structure is shown in Figure 1.

Figure 1 Inquiry Mode Implementing Fax Transmission Box (2) Using Communication Controls Under Windows3.1, VB3.0 provides VBX control MSCOMM. VBX, and VC1.0 / 1.5 class libraries provide CVBControl classes, making applications conveniently Handle the VBX control. Under Win95 / 98, from VC2.0, discard VBX controls, gradually replaced by OLE controls and ActiveX controls, correspond to communication VBX control MSCOMM.vbx to MSCOMM16.OCX (16-bit) and MScomm32.ocx (32 bits). (3) Thread event drive mode Under Windows 3.1, when the communication event is set, each specified communication event occurs, the communication driver will automatically send communication notification messages to the application to the application, which indicates a window transmission and reception. The state of the buffer. Win95 / 98 communication mechanism has a big difference in Windows3.1, no longer have a WM_COMMNOTIFY notification message, in order to implement interrupt communication, Win95 / 98 provides a thread processing method, making programmers fully control the execution of the program, perform real-time transmission and reception data . Using Method 1 Implementing fax communication is very simple, but time consuming, the efficiency is not high, so this method cannot be real-time transmission and reception data, which is easy to cause data loss. Method 2 uses the communication control, greatly simplifies communication programming, and can perform real-time transmission and receiving data through its communication event, and provide a large number of attributes and methods, which are not a good method for ordinary serial communication. The method will make the transmitted ASCII characters and ASCII 0 (that is, the ASCII value of the character) will not be able to get strongest, not correctly completed data transmission, and in the fax application, many of the flag characters in the transmission unit frame in the fax Some is extending ASCII, and the fax data is transmitted after the image data is encoded. It is a data-oriented data, so that in the transmitted data, it will often encounter the expansion ASCII characters and ASCII 0, so this The method is also not suitable for transmission of serial data. Method 3 overcomes the shortcomings of the above methods, with high efficiency, accuracy, can meet the basic requirements of fax transmission. 4 The implementation of the thread event mode is in the WIN95 / 98 environment, the thread is the only execution unit, which is the basic entity that Win32 assigns the CPU time. Each running application constitutes a process, each process includes one or more threads, each thread coordination completion of the specified operation, the process in the application is starting as a separate thread. The MFC will be executed in the CWINTHREAD class, including a series of member variables and member functions created, managed, and delete threads. The main idea of ​​thread communication is to create a communication thread alone, which is mainly monitoring the communication event, such as detecting whether overlapping I / O operation is completed and whether new data is received. In order to facilitate maintenance, the serial port communication is designed into a class ccommctr, which implements a method such as opening the serial port, configuring serial port, sending data to serial port, and closing serial ports, and also completing monitoring of communication events. When the process starts, first create a primary thread, then create a communication thread, and hang it, then call the member function in CCOMMCTRL to complete the initialization of the serial port, create a synchronization object, set the event mask, etc., then continue at the same time Perform a communication thread, which is monitored by it. When there is a communication event, a custom message is sent to the main thread. Figure 2 is the primary process.

Communication between the thread 2 outputs a flowchart fax (1) creates a thread m_commctrl = (CCommCtrl *) AfxBeginThread (RUNTIME_CLASS (CCommCtrl), THREAD_PRIORITY_NORMAL, 0, REATE_SUSPENDED); wherein m_commctrl is CCommCtrl objects, CCommCtrl CWinThread derived object to RUNTIME_CLASS, THREAD_PRIORITY_NORMAL of The priority of the thread, create_suspended indicates that the creation of the thread is hang. (2) Open the serial port and configure the serial port 1) Open the serial resource with the CREATEFILE function. CreateFile function prototype: Handle CreateFile (LPCTSTSTR LPFILENAME, / / ​​Pointer to the string, such as "COM2"; DWORD DWDesiredAccess, // Access mode, can be read or written or written or written; dWord dwsharemode, // shared mode , the communication resource, can only be 0; LPSECURITY_ATTRIBUTES lpSecurityAttibutes, // pointer to the security descriptor mode; DWORD dwCreationDistribution, // the communication resources, it must OPEN_EXISTING; DWORD dwFlagsAndAttributes, // the specified attributes and flags; HANDLE hTemplateFile / / Template file handle, for communication resources, must be 0) If the function call is successful, return a handle that specifies the communication resource; otherwise returns invalid_handle_value. 2) Use the getcommstate function to obtain the current configuration of the serial resource, and then use the setcommstate function to reconfigure the serial resource parameters. Then use the setupcomm function to set the buffer size. 3) When all of which are completed, readfile and writefile can be used to read and write the serial port data. (3) Communication Event 1) Setting the event mask to establish an event mask through the setcommmask function to monitor the specified communication resource, whose prototype is: BOOL SETCOMMMASK (Handle Hfile, // Communication Equipment handle, returned by CreateFile; DWORD DWEVTMASK / / Monitable Event) 2) Monitoring Communication Events After specifying the monitored event mask, the process waits for one of the events to occur with the waitcommEvent function. The prototype of Waitcommevent is: BOOL WAITCOMMEVENT (Handle Hfile, // Communication Equipment handle, returned by createfile; LPDWORD LPEVTMASK, // Point to the current change of the current event; LPOVERLAPPED LPOVERLAPPED // Point to a LPoverLapped structure) (4) overlap I / O The operation overlapping operation allows the thread to free from the I / O operation of the time, so that the I / O operation is executed in the background, and the thread can freely perform other tasks. When the overlapping operation is used, the I / O operation returns immediately whether it is completed. If the communication device is to be able to perform overlapping operations, the DWFlagsandAttributes parameter must be specified as a combination of File_Flag_overlapped and other flags when using CreateFile to open the communication device. At the same time, according to the functions performed by overlapping operations, you must specify a pointer to the Overlapped structure in its parameter. Finally, the Overlapped structure must contain manual reset event objects.

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

New Post(0)