Data collection with Delphi in industrial control and automation

zhaozj2021-02-11  198

Data acquisition technology plays an important role in industrial control and automation. The general process of data acquisition is this:

1 Send channel selection instructions to the capture card. 2 Select the channel number to collect. 3 Start A / D conversion. 4 Wait until the conversion is completed. 5 read the data from the capture card. For multi-channel acquisition, two methods generally adopted in the design of the program. Query method or interrupt method. The so-called query method is to use a loop to capture each data channel sequentially. The advantages of the query method are simple and easy to implement; the disadvantage is that the CPU most time is waiting, resulting in waste of resources. The interrupt method is the form of hardware interrupts & # 0; & # 0; first starts A / D conversion, issue an interrupt signal & # 0; & # 0; CPU responds to the interrupt of the capture card The data. Thus, in the time waiting for the conversion, the CPU can perform other calculations without having to wait. The advantage of the interrupt method is that the resources can be fully utilized; however, the program is complex, especially when the system's hardware interrupts the resources, it is easy to cause interrupt conflicts; in addition, in Windows or Win95 and other operating systems, the user is not allowed to install the interrupt handler When it is not possible.

---- The two methods discussed above are methods under DOS; in Win95, now there is a better way to Method & # 0; & # 0; multi-thread technology. Now we can use multi-threaded technology to make data acquisition.

---- 1. Adoption of multi-threaded data collection

---- WIN95 / 98 is the most lovely except for the beautiful interface, it is multi-thread and multitasking. In the DOS environment, the execution program can exclude all resources; in the Windows environment, although it is a slightly prototypeful multitasking environment, as long as you like, your program can still master all CPU time. However, in Windows 95 and Windows NT, a program cannot exclile all CPU execution time. Moreover, a program is not from beginning to end. Instead, a program can be divided into multiple program segments in the execution, executing. These program fragments that can be simultaneously executed are called threads. In Windows 95 and Windows NT, the operating system can perform multiple programs at the same time, which is multitasking.

---- Multi-threaded data acquisition can effectively speed up the reaction speed of the program, increase the efficiency of the execution. The user's input is handled in the general program, but the user's input speed is the same as the plane to the plane compared to the execution of the CPU. In this way, the CPU will waste a lot of time to wait for the user's input (eg in the DOS environment). If you use multiple threads, you can use a thread to wait for the user's input; another thread performs data processing or other work. For data acquisition programs, data acquisition can be performed with a separate thread. In this way, the real-time guarantees to ensure the acquisition of the real-time, while the additional thread can also respond to the user's operation or data processing in time. Otherwise, the program cannot respond to the user's operation when collecting data; data acquisition cannot be performed when responding to user operation. Especially when the amount of data collected is very large, the data processing task is very heavy, and if you don't use multithreaded, the long wait in the collection is very acceptable.

---- However, multithreading is much more complicated than ordinary programming. Since all threads may be executed at any time, many variables, data may be modified by other threads. This is the synchronous control problem between the most critical threads in multi-threaded programs.

---- 2. Multi-threaded issues should be solved by data collection

---- In fact, multi-threaded program is complex is temporary; if you use traditional C to multi-threaded design, then you must control the synchronization between the thread. That will be very complicated. However, if the object-oriented design method is used, Delphi is used for multi-threaded programming, and the problem is simple. This is because Delphi has handled multithreaded complexity, and what we have to do is inheritance. ---- Specifically, multi-threaded data collection needs to complete the following work:

---- 1 derived a self-class Samplethread from the TTHREAD class. This is the class we use for data acquisition. When collecting, you only need to simply create an instance of SampleThread.

- 2 Overloads the super class TTHREAD's Execute method. Data acquisition tasks are performed in this way.

---- 3 If you want to display one side, you will write several processes for displaying the progress of the acquisition, for the Execute method call.

---- The most common attribute / method in the TTHREAD class is as follows:

Create method: constructor crete

CreateSuspended: boolean;

---- Where the CreateSuspended parameter determines whether the thread is executed immediately when it is created. If true, the new thread is hanged after being created; if false, the thread is executed immediately after being created.

FreeOnterminate properties:

Property FreeOnterminate: Boolean;

---- This property determines if the programmer is responsible for undo the thread. If this property is True, the VCL will automatically undo the thread object when the thread is terminated. Its default value is false.

Onterminate property:

Property Onterminate: TNOTIFYEVENT;

---- This property specifies an event that occurs when the thread is terminated.

---- Look at a specific example below:

---- 3. Realization of multi-threaded data acquisition

---- This is a procedure for a measurement drum for the author developed. Its function is to collect load and displacement data of the fumping machine hosted, and the power map of the suction machine is processed. Fig. 1 () is shown in the interface of data acquisition. After the "Acquisition Data" button, the program will create a new thread and set its properties. This new thread will complete the data acquisition task. The procedure is as follows:

Procedure TsampleForm DoSampleBtnClick (Sender: TObject); Begin ReDrawBtn.Enabled:. = True; DoSampleBtn.Enabled: = False; FFTBtn.Enabled: = True; TheSampler: = SampleThread.Create (False);

Create a collection thread

THESAMPLER.Onterminate: = fftbtnclick;

Tasks to be executed after collected

THESAMPLER.FREEOnterminate: = true;

Undo

END;

---- The class of collecting thread is defined as follows:

TYPE SAMPLETHREAD = Class (TTHREAD) Public Function Adread (ACH: BYTE): Integer; SaFECALL

Read the A / D card function

Procedure UpdateCaption;

Display time used

Private {private declarations} protected the, thep: real;

This is the key.

END;

---- Defining a function Adread in this class is used to operate the A / D card, and the two processes are used to display the progress of the acquisition and the time used. It should be noted that the Adread function is written in accordance with us, and the parameter call format must be SaFECALL. ---- The key to overload method Execute is as follows:

Procedure SampleThread.execute; Begin Startticker: = GettickCount; ID: = 0; Repeat Shes: = Adread (15) * AD2MV * MV2L;

Collection of the 15th channel

Thep: = adread (3) * AD2MV * MV2N;

Collecting the 3rd channel

DT: = GettickCount - StartTicker; Sarray [id]: = the; parray [id]: = thep; tarray [id]: = dt; inc); Synchronize (UpdateCaption);

Note: Display the progress of the acquisition

Until ID> = 4096; ed: = gettickcount; synchronize;

Note: Display time

END;

---- It can be seen from the above code, and Execute has no essential difference from the general code. Only the difference is to display the acquisition schedule and display the time used, and cannot directly call the process, but by calling SYNCHRONIZE indirectly. This is to keep synchronization between processes.

---- 4 Conclusion

---- The above program is programmed with Delphi 4.0, implemented on AMD-K6-2 / 300. The test results are like this: use multithreading, collecting 4096 points generally consumes 10 to 14 s time; if multiple threads are not used, it takes 1 minute to 1 minute and a half. Visible multi-threads can significantly improve the performance efficiency of the program.