Difference between revisions of "MC-Basic:IMPORT C"
m (Text replace - "Versions 3.6.20 and higher" to "Since Version 3.6.20") |
m |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 13: | Line 13: | ||
Imports a user C function into a task. In order for the Translator to match function parameters the user has to provide C-function prototype. | Imports a user C function into a task. In order for the Translator to match function parameters the user has to provide C-function prototype. | ||
{{Note/Important| | {{Note/Important| | ||
− | Important to note that matching between provided | + | Important to note that matching between provided softMC prototype and actual C implementation cannot be tested by Translator, thus it is the user’s (programmer) responsibility to keep consistency between function prototype and C implementation.}} |
The MC-Basic Translator needs a function prototype in order to translate a user program. All the function prototypes will be concentrated in the single predefined file - proto.pro. Since all the C-functions are global it is enough to have only one prototypes file. The Translator will use this file during translation of user programs and for command line. It will include a list of C-function prototypes. The Translator is optimized to re-translate this file only if needed.Prototypes do not include parameters’ names. A prototype of a C-function with no parameters is written with empty parenthesis: | The MC-Basic Translator needs a function prototype in order to translate a user program. All the function prototypes will be concentrated in the single predefined file - proto.pro. Since all the C-functions are global it is enough to have only one prototypes file. The Translator will use this file during translation of user programs and for command line. It will include a list of C-function prototypes. The Translator is optimized to re-translate this file only if needed.Prototypes do not include parameters’ names. A prototype of a C-function with no parameters is written with empty parenthesis: | ||
Line 41: | Line 41: | ||
|SCOPE= | |SCOPE= | ||
− | Task, Library | + | Task, Library, PROTO.PRO |
|LIMITATIONS= | |LIMITATIONS= | ||
Line 82: | Line 82: | ||
CFunc2 (DoubleVar, 2, “OK”) | CFunc2 (DoubleVar, 2, “OK”) | ||
+ | |||
+ | |||
+ | Sending handle of motion element is done with elementID (integer) property. | ||
+ | |||
+ | Sending group location is done with generic location: | ||
+ | |||
+ | int example(int axisHandle, SYS_POINT * point) | ||
+ | { | ||
+ | } | ||
+ | |||
+ | IMPORT_C example(byval as long, as generic location) as long | ||
+ | |||
+ | |||
|SEE ALSO= | |SEE ALSO= | ||
− | * [[ | + | * [[MC-Basic:FUNCTION ... END FUNCTION|FUNCTION ... END FUNCTION]] |
− | * [[ | + | * [[MC-Basic:PROGRAM ... END PROGRAM|PROGRAM ... END PROGRAM]] |
− | * [[ | + | * [[MC-Basic:SUB ... END SUB|SUB ... END SUB]] |
− | [[Category | + | [[Category:MC-Basic:C-Interface|IMPORT C]] |
}} | }} |
Latest revision as of 06:04, 11 September 2016
Imports a user C function into a task. In order for the Translator to match function parameters the user has to provide C-function prototype.
IMPORTANT | |
Important to note that matching between provided softMC prototype and actual C implementation cannot be tested by Translator, thus it is the user’s (programmer) responsibility to keep consistency between function prototype and C implementation. |
The MC-Basic Translator needs a function prototype in order to translate a user program. All the function prototypes will be concentrated in the single predefined file - proto.pro. Since all the C-functions are global it is enough to have only one prototypes file. The Translator will use this file during translation of user programs and for command line. It will include a list of C-function prototypes. The Translator is optimized to re-translate this file only if needed.Prototypes do not include parameters’ names. A prototype of a C-function with no parameters is written with empty parenthesis:
IMPORT_C<Function_Name> ( ) {AS <return_value_type>}
A C-function will accept any combination of double-, long- and string-type parameters. The parameters may be passed “by value”(with the BYVAL prefix) or “by reference”(without any prefix). Arrays cannot be passed as parameters.
The returned value may be either long, double, string, ornone (for C functions with void returned value).
IMPORTANT | |
Translator is case-insensitive, while object module loader is case-sensitive, so the name of a C function should be written in capital letters within the C code. On the other hand, MC-Basic prototypes of C functions, as well as C function calls through MC-Basic, are not case sensitive. |
Object files , including user C functions , must be loaded (using oload command) prior to using the C-functions that it contains. This means that a single MC-Basic application program cannot use load an object file and use it’s functions – if an application task wishes to use C-functions, the object file must be loaded before loading the task.
Syntax
IMPORT_C <Function_name>({ {ByVal} as parameter_type>}, { {ByVal} AS <parameter_type>}) {AS <return_value_type>}
Availability
Since Version 3.6.20
Scope was expanded to libraries since version 4.9.12
Scope
Task, Library, PROTO.PRO
Limitations
Library must be loaded into memory (using load command).
Examples
Import_C Cfunc1 (As Long, ByVal As Double, As String) As Long
Import_C Cfunc2 (As Double, ByVal As Long, ByVal As String)
Import_C Cfunc3 ( ) As String
Where C-implementation of CFUNC1 will be as follows:
int CFUNC1 (int * par1, double par2, char ** par3)
{
}
Whereas the C-implementation of CFUNC2 and CFUNC3 will be, respectively:
void CFUNC2 (double * par1, int par2, char * par3)
{
}
char* CFUNC3 (void)
{
}
User C Functions calling from MC-Basic :
LongVar = cfunc1 (LongVar, 3.3, StringVar)
Print CFUNC3 ()
CFunc2 (DoubleVar, 2, “OK”)
Sending handle of motion element is done with elementID (integer) property.
Sending group location is done with generic location:
int example(int axisHandle, SYS_POINT * point) { }
IMPORT_C example(byval as long, as generic location) as long