Communication for Java and Matlab based on VC

zhaozj2021-02-16  74

2003-08-29 ■ Author: Chen Yongzhou ■ Source: yesky

Foreword System Implementation: Passing the noise standard deviation from the Java program and two data to MATLAB, that is, DLL to which the specific data calculation function is implemented (via VC Generate DLL), producing white noise data of Gaussian distribution. Where VC requires two files "Normrnd.m, Rndcheck.m" comes with MATLAB software and generating white noise file "noise.m". The development environment is: Window2000 operating system, Visualc 6.0 and JBuilder6.0 development tools, MATLAB6.5 software. Align = "Right" marginwidth = "0" marginheight = "0" src = "http://www.chinabyte.com/tag/cont_flash_software.html" frameborder = "0" width = "360" scrolling = "no" height = "300"> To implement Java, MATLAB communication must solve two problems: First, the communication of Java and VC ; the other is VC to communicate with MATLAB. 1 Implementing the interaction of Java and VC Communication Method, the interaction of JAVA and VC can be done by read writing of the conventional files respectively run, but the real-time performance of communication cannot be guaranteed; the CORBA mechanism can be utilized, customers who define the IDL interface The form of aircraft / server (Client / Server) can also be implemented by JNI (Java Native Interface) or RMI (REMOTE METHOD INVOCATION). On Microsoft's operating system, the communication application of the machine program is more convenient and fast, stable and reliable, and can be realized. This mechanism is used in the implementation of this sample system. 1.1 Implementing Java and VC (DLL) interconnect in the Jubilder6 development environment, the system is Mr. By the NNSD, the framework class is Frame1. In order to use the VC implementation DLL, the system is added to the function in the Frame1.java file to add some of the following code:

Public Native Void Makenoise (FLOAT M, INT N); // Implement the function in the call DLL declares static {system.loadLibrary ("makenoise"); // Use the DLL file, its name is "makenoise.dll"} void jbutton1_actionperformed ( ActionEvent E) // Frame By button message implementation DLL call {INT N = 160; // white noise data amount float m = 1.0; // white noise square flat makenoise (m, n); // java program to DLL Use of functions}

1.2 System run settings and compilation

When developing using JBuilder, you must set up the Java file compilation attribute using the DLL. This system finds Frame1.java in the Project view area in the JBuilder tool (different file names may be different), select the right mouse button, select "Properties" item in the menu, then "Build" item -> RMI / JNI-> Generate JNI Header File "found project selected. Finally, compile this file, you can have the corresponding H file. The system generates "NNSD_Frame1.h", where the call is called: JNIEXPORT VOID JNICALL JAVA_NNSD_FRAME1_MAKENOISE (JNienv *, Jobject, JFLOAT, JINT);

If you only use Sun's JDK to implement, you must use javah.exe to generate the Class file generated by Javaah.exe to generate the corresponding H file, and then modify the H file, you must use the file's package level on the called function name Reflected.

1.3 Java application system operation

Copy the VC generated DLL file to the Directory of the JBuilder running program, execute the program, and implements communication of the Java system and MATLAB. The DLL of this sample system is "makenoise.dll", which is generated by the method described below.

2 Implementing the VC, MATLAB Communication Method MATLAB provides many ways to communicate with the VC program: MATLAB Engine (Engine) uses a computing mode of the client / server, and is related to functions in the MATLAB Application Interface (API). In the VC environment, call the MATLAB function, but this method requires the MATLAB background support, so the system must install MATLAB; MCC tools provided by the MATLAB itself, compile the M file to generate VC or C language, directly in VC; MATLAB MEX The tool compiles the M file into a DLL for VC calls, but this increases the complexity of the program, no second method is flexible.

MATLAB's current MATLAB Add-in (which packaged MCC) is more convenient for the application of the second method. The implementation of the current system is based on it.

2.1 MATLAB and VC Realize interconnection

In order to use the specific function of MATLAB, the MATLAB tool software should be installed in the development environment, then run this tool software, execute "MEX -SETUP; MBUILD -SETUP; CD (PrefDir);" command. Now run the VC development tool, open "Tools-> Customize-> Add-Idd-Ins and Macro Files" from the menu, select the MATLAB Add-IN item, you can see more than one toolbar.

. Now VC can use it to get C, C files corresponding to the M file.

2.2 Development Tool VC Operation Environment Settings

When the system is developed, the development environment variable of the VC must be set. Add environment variables in the Directories item found by "Tools-> Options-> Directories".

Add header file and library file search path [3] Yes (this computer's file search path is):

F: / matlab6p5 / extern / include f: / matlab6p5 / extern / include / cpp f: / matlab6p5 / extern / lib f: / matlab6p5 / extern / lib / win32 f: / matlab6p5 / extern / lib / win32 / microsoft / msvc60

The above-mentioned M files that generate the MATLAB of the VC generate C code. At the same time, in order to compile the JNI H file, you must also add the menu Tools. And Project to add the following file and library file search path: d: /jbuilder6/jdk1.3.1/include d: /jbuilder6/jdk1.3.1/include/win32

2.4 VC Generate DLL [4]

In this system, the VC establishes the "MFC AppWizard (DLL)" Project named Makenoise, and completes the foundation of the project according to the VC default option.

Copy the NNSD_FRAME1.H file generated in 1.2 to the system directory and add it to "Headfiles" in the "FileView" of the VC. Add "Normrnd.m, RndCheck.m, noise.m" file to the VC via MATLAB Add-in, and automatically generates VC files. The specific steps are as follows:

Click on the "Add M-Files To Current Project" item in the toolbar, select the following options in the figure:

Figure 1 Add-IN selection of the M file

The above three M files are added to the project according to the prompts of the next step.

In the VC, in order to provide a callable function, you must add: Makenoise.cpp files:

#include "nnsd_frame1.h" jniexport void JNICALL JAVA_NNSD_FRAME1_MAKENOISE (JNIENV * ENV, JOBJECT OBJ, JFLOAT M, JINT N) {} // The original file variable name is made, and the function code is implemented below.

In order to get the above-described export function to the call to the white noise function in matlab, you must add it in this file:

#include "math.h" #include "dblmtrx.h" // This declares that the global MATLAB function extern void initialization_noise (); // Initialize Extern Void Noise (Mwarray N, Mwarray M); // Generate white noise function Extern void TerminateModule_noise (); // Close // dll resources derived JAVA function embodied portion JNIEXPORT void JNICALL Java_nnsd_Frame1_makeNoise (JNIEnv * env, jobject obj, jfloat m, jint n) {AfxMessageBox ( "start generating white noise!"); InitializeModule_noise (); noise (n, m); terminatemodule_noise ();

Compile the entire program, so you get "makenoise.dll".

2.3 Support files from the MATLAB environment

In order to use the above-described DLL, you must add some files to the directory of this DLL, and these files can be obtained by "Matlab Add-in Packager" in the toolbar. Follow this tool to get a ZIP compressed file that runs the desired file. When using the DLL file, you can use the DLL from the MATLAB environment.

3 conclusions

With Java to complete graphical interface design, data management, etc. In particular, the compiler calls the MATLAB dynamic connection library to achieve the close combination of the two under the MATLAB environment, which greatly expands the application range of MATLAB. Based on VC 6.0 to implement Java and Matlab communication, the application capabilities of Java and Matlab have deepened. Note: The content of the noise.m file is as follows:

Function Makenoise (N, M) C = Normrnd (0, M, 1, N); S = 'sample file .txt'; FID = FOPEN (S, 'W'); fprintf (FID, '% 6.2f', c); fclose (FID);

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

New Post(0)