Realization of serial communication

xiaoxiao2021-03-06  52

Realization of serial communication

With the development of the current information technology, the integrated intersection of the network has become a good fault, but the serial communication is a good communication method that is reliable, and it is still not a valid communication. It is widely used in industrial control. In the practice of industrial production, it is real-time monitoring in real-time monitoring, and it is often necessary to seek PC can have data acquisition, data processing and control signals such as data collection, data processing and control signals. In this particular environment, the PC should be associated with the real-time signaling of the process control, and it is necessary to realize the active operation of the PC's serial port. The Delphi, which is a strong high-grade programming, a high-grade programming of a strong function, and specializes in the characteristics of the visualization object, and specializes in the formation of the pattern interface and user program in the WINDOWS environment. This article is to introduce the use of the Windows95 / NT operating system to realize the serial communication method between the PC and the lower PLC controller. Based on WIN95 / NT serial communication mechanism Windows operating system mechanism prohibits applications from accessing computer hardware, but it provides programmers with a series of standard API functions that make the application more convenient and except for hardware Commissioning trouble. In Windows95 / NT, the WM_COMMNOTIFY message originally Windows3.x has been canceled, the operating system opens up the user-definable size read / write buffer for each communication device, and the data entry communication port is completed by the operating system background. Simply operate on the read / write buffer. Several commonly used serial communication operation functions in Win95 / NT are as follows: CreatFile Open Serial Port CloseHandle Close Serial Port SetupComm Sets the size of the communication buffer ReadFile Readfile ReadCommState Settings Communication Parameters GetCommState Get Default Communication Parameters Clearcommorror Clear the serial port error and get the current state except for the above functions, it is often used to use an important record DCB (device control block). Recording a definable serial parameter in the DCB, you must first use the getcommstate function to fill the system default value to the DCB control block with the GetCommState function, or you can set the custom value of the user to change. In addition to the basic communication operation functions in Win95 / NT, you have to hold a multi-thread programming. The thread is the path of the internal execution of the process, which is a basic entity that operates in the form of a CPU time. Each process is implemented by a single-thread start.

Serial communication needs to be implemented using multi-threading technology. Its main processing logic can be expressed as follows: The process starts to do some necessary initialization works by the main thread first, and then the main thread establishes a communication monitoring thread monitoring communication port as needed. When the specified serial port event occurs, send the WM_COMMNOTIFY message to the main thread (since Win95 cancels the WM_COMMNOTIFY message, it must be created itself), the main thread is processed. If you do not need WM_COMMNOTIFY messages, the main thread ends the communication monitoring thread. Multi-threaded time is executed, will lead to the rush of the shared resource. To avoid rush, you must use synchronous multi-thread to visit the shared resource. Win95 has provided a number of ways to keep a thread synchronization, and the author uses a creation of events to keep the thread. Create an event object with CRAETEEVENT (), use the setEvent () or pulseevent () function to set the event pair as a letter. In the application program, use the waitsingleObject () function, etc. to be symptomatic, etc., when the specified event is set by its thread, it will continue to execute the program. Delphi's physical reality method Delphi's strong power and support for multi-threaded parallelism, so that it is not always simple to realize. It is implemented by calling the external API function. The main steps are as follows: First, use the CreateFile function to open the serial port to determine the share of this serial port, and block other applications for this serial port; secondly The device control block DCB is populated by the getcommstate function, and then configure the baud rate, data bit, check bit, and stop bit of the serial port by calling the setcommstate function. Then, create a string-line monitor thread monitoring string. On this basis, the data can be operated on the corresponding sanctuary; the last, the serial port is closed with the CloseHandle function. The specific steps are as follows, and the Delphi3.0 is prepared in the Win95 ring T environment. It has been in practical applications for the broader readers.

Program: unit comdemou; interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; const Wm_commNotify = Wm_User 12; type TForm1 = class (TForm) procedure FormCreate (Sender: TObject); private Procedure comminitialize; Procedure MsgcommProcess (var Message: Tmessage); Message Wm_commnotify; {Private declarations} public {public declarations} end; // declare thread TComm = Class (TThread) protected procedure Execute; override; end; var Form1: TForm1; hcom, post_Event: Thandle; lpol: poverlapped; implementation {$ R * .DFM} procedure TComm.Execute; // thread execution vardwEvtMask: Dword; Wait: Boolean; Beginfillchar (lpol, sizeof (toverlapped), 0); While True do Begin dwEvtMask: = 0 Wait: = Waitcommevent (hcom, dwevtmask, lpol); // Waiting for the serial port event; if Wait The begin WaitForsingleObject (post_event, infinite); // Waiting for the synchronization event set; resetEvent (post_event); // Synchronization event reset Postmessage (Form1.Handle, WM_COMMNOTIFY, 0, 0); // Send message; end; end; end; procedure tform1.comiminit, PROCEDURE TFORM1.COMMINIT ialize; // initialize the serial port varlpdcb: Tdcb; Beginhcom: = createfile ( 'com2', generic_read orgeneric_write, 0, nil, open_existing, file_attribute_normal orfile_flag_overlapped, 0); // open the serial port if hcom = invalid_handle_value then else setupcomm ( HCOM, 4096, 4096); // Setting input, output buffer is 4096 bytes getcommstate (hcom, lpdcb); // Get serial port current default settings lpdcb.baudrate: = 2400; lpdcb.stopbits: = 1; LPDCB.BYTESIZE: = 8; LPDCB.PARITY: = EVENPARITY; / / Even Volume SetCommState (hcom, lpdcb); setcommmask (hcom, ev_rxchar); // Specify the serial port event to receive characters; end; procedure tform1. Msgcommprocess (var message: tMessage); VARCLEAR: Boolean; COMS: TCOMSTAT; CBNUM, READNUMBER, LPERRORS: INTEGER

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

New Post(0)