Custom_C

Parent Previous Next

System_Language/ C_and_CPP / Custom C
Block Name: Custom_C

Code File Location: VisualSim/jni/GenericJNIActor

Block Overview

  o  This block is used to import a file containing a set of functions written in C/C++ code. To use this block, configuration files must be updated. 

Configuration

There are two files located in $VS/bin directory called vsconf.bat and vsconf.sh, and the two startup scripts in $VS directory called VisualSim.sh and VisualSim.bat. Use the .bat file for windows users and .sh for all forms of UNIX (Linux, Solaris, MAC OS etc.). Follow the below table to fill in the required information.


VisualSim.bat

1. set VS_C_Library= (Base path to where all the source code will be placed.  The default within the VisualSim install will be C:\VisualSim\VS800\UserNativeC.
2. Do not modify the setting for VS_C_DLL= (Path to where the .dll files are located).  This is preset to $VS_C_Library/blocks.
3. Make sure the JAVA_HOME variable is set to jdk and not jre.  BY default, VisualSim only requires jre.

vsconf.bat

1. SET VSDIR=(This is the path to the compiler).  For example, "C:\Program Files\Microsoft Visual Studio 9.0\VC"
2. set CC=cl (Change if using anything other than VisualStudio)

VisualSim.sh

1. set VS_C_Library= (Base path to where all the source code will be placed.  The default within the VisualSim install will be User/VisualSim/VS800/UserNativeC
2. Do not modify the setting for VS_C_DLL= (Path to where the .so files are located).  This is preset to $VS_C_Library/blocks.
3. Make sure the JAVA_HOME variable is set to jdk and not jre.  BY default, VisualSim only requires jre.

vsconf.sh

 1. export CC=g++
 2. export CCDIR=/usr/bin (Modify this only if the install is in a different location)


Compilers Supported

Windows: VisualStudio 2008, 2010 and 2013.  Support for Borland compiler is also available.

If you use Visual Studio Express, be advised that Microsoft's Platform SDK (PSDK) is not included with the Visual Studio Express installation.  You will need to install it separately and configure Visual Studio to use it (follow Microsoft's directions for this).

Linux and UNIX: g++ 4.0.x.

Block Setup

The following steps must be taken to construct a block to import code into a VisualSim model.  To understand the block usage, look at the simple example.

  1. Drag a Custom_C block from System_Language -> C_and_CPP directory.
  2. Enter the LibraryDirectory. This is relative to the VS_C_Library attribute from above.  This uses the "." as a separator rather than slashes.  This is the directory where you want to store all the details about this block instance. This is the directory where the wrapper files are generated and the compile success/failer details are written to a file called vsout.txt.
  3. Enter the NativeFile directory and name.  This uses forward slash "/" as a separator.  This is also relative to the VS_C_Library.  This is the location of the C file.
  4. Add the Ports using Customize -> Add Ports.  There will be one port for each input variable sent to the code and one output for each variable being returned.  The type of the port is not important and does not need to be set.
  5. Next Right-click to Configure Argument. For each port, add the 4 information. There must be atleast one OutputKind selected as a return. The C++ data types supported are listed below. The Name must match the port name and the Function Name is the function that the input or output is associated with.
    1. OutputName: This is the name of the port and must match case-sensitive.
    2. OutputC or C++ Type:  Look at the list of mapping from VisualSim to C++ below.
    3. OutputFunction Name: The function in the C code that this file is associated with.
    4. OutputKind: There must be one output port that is selected as a "return" type.  All others can be output or input.  The "return" is the same as a output port.
  6. Important Note:  The "return" port must be listed first in both Customize Ports and the Configure Argument windows.  They should be set first before the others.  If this is not done, a data structure from C++ to VisualSim will not output values correctly.  The rest of the arguments must be in the same order as the ports.
  7. Important Note: If the return for a function is a struct, the struct defined must have the same name as the function name.  If this is not done, the struct value will not output correctly.

Data Types supported:

Output C and C++ Type: The following table shows the C\C++ Argument types and their equivalent VisualSim types.

C \ C++ Argument Types

Equivalent VisualSim Types

char[]

string

char

string

short, long

long

double

double

bool

boolean

int[], short[]

{int} 

long[]

{long}

double[]

{double}

{ Name=char[5]; Id=int} (Available only for the C block output)

general

Table 2: C\C++ Arguments and Equivalent VisualSim Types

Note: { Name=char[5]; Id=int} is representation for the C\C++ structure like the one below:

struct Example{

char Name[ ];

int Id; };

Notes

If additional logic needs to be specified between the block port and the function, you can add this to the generated cpp wrapper file. Look at the Master-Slave Example to see what can be added.

If you need to access multiple files, this is also specified in the Wrapper file.  You can add more includes also trigger specific functions based on the input values.

The output can return a struct or Data Structure to VisualSim.  All input ports must be basic C++ types.

The following requirements must be maintained.

File names must not contain _.

The block "Customize Name" must match the Library Directory parameter. If the block name does not change by itself, then the user must change. Else a java.lang.null exception will be received.  The block name will be changed to Library Directory + "I" + "C+ file name.

If multiple c or cpp files are located in the same directory, then the $VS/bin/vsmake.{sh,bat} must be updated. All the delete lines mst be commented out: *.cpp, *.java, *.h, Jni*.cpp and Jni*.h.

If you have multiple instances of the same C code in a single model, they will each need to have their own .dll/.so.  This means that the Block Icon Name must be unique for each block even though the internal parameter refers to the same file.  This is irrespective of the hierarchical block that each one is part.


Parameter

Explanation

Type

libraryDirectory

The directory that will hold the generated and intermediate files. Make sure you have an unique file for each model.

string

nativeFunction

The name of the native library and the cpp file name. The cpp or c extension is not required. 

string





 

Created with the Personal Edition of HelpNDoc: Create iPhone web-based documentation