Replies: 1 comment
-
Hello @PBaswat The problem is with the way you call a program. The following code would work to call the program: #include <iostream>
using namespace std;
extern "C" {
// Declare a struct for the function block
typedef struct {
bool ci;
int printer;
char in[81];
} print_string;
// Declare a struct for the program
typedef struct {
print_string printstr;
} myprogram_struct;
// Declare the instance of the program as external
extern myprogram_struct myprogram_instance;
// Declare imported global variables as external
extern unsigned int gnExternalIf;
// Declare the external "public" method by using the program call signature
void myprogram(myprogram_struct*);
}
int main(int argc, char * argv[]){
gnExternalIf = 56;
myprogram(&myprogram_instance);
std::cout << "Number is " << std::to_string(gnExternalIf) << std::endl;
return 0;
}
Note that var_temp don't need to be part of the struct. Also note that the function block struct has to be represented. The names don't matter, just the sizes and that they exist. I will add an issue to document this in the book. It is mentioned only in passing in the Architecture As a sidenote, you can format code blocks on Github by inclosing it in three consecutive tick symbols:
Will produce void main() {
} and
will produce
If you don't mind could you edit the post to format the code? I could also format it for you if you like. |
Beta Was this translation helpful? Give feedback.
-
i am trying to invoke st program(myprogram.st) from main.cpp & myprogram.st invokes another function block(print_string.st) function from it.
when i tried to compile & run program, it compiles successfully but during execution i am getting segmenation fault.
following commands are used for compilation and linking
#rustyc -c print_string.st myprogram.st -o myprogram.o
#clang++ -c main.cpp -o main.o
#clang++ myprogram.o main.o -o Test
-------------------------------------------------------------myprogram.st--------------------------------------------------------------------
var_global
gnExternalIf : dint; (* defaults to zero *)
end_var
{external}
FUNCTION puts : DINT
VAR_INPUT {ref}
text : STRING;
END_VAR
END_FUNCTION
{external}
FUNCTION printf : DINT
VAR_INPUT {ref}
format : STRING;
END_VAR
VAR_INPUT
args: ...;
END_VAR
END_FUNCTION
program myprogram
var_temp
asign1 : dint ;
asign2 : dint ;
asign3 : dint ;
end_var
var
printstr : print_string;
end_var
if gnExternalIf = 0 then
asign1 := gnExternalIf + 0;
elsif gnExternalIf = 1 then
asign1 := gnExternalIf + 3;
else
asign1 := gnExternalIf + 6;
end_if;
printstr( CI := TRUE, PRINTER := gnExternalIf, IN := '1111111');
gnExternalIf := 50;
end_program
-------------------------------------------------------------print_string.st--------------------------------------------------------------------
FUNCTION_BLOCK print_string
VAR_INPUT
CI : BOOL := TRUE ; (* Control in. )
PRINTER : DINT := 1 ; ( Printer number )
IN : STRING ; ( The value to print. *)
END_VAR
VAR_OUTPUT
CO : BOOL ; (* Control out. *)
END_VAR
printf('CI : %d, PRINTER : %d, IN : ', CI, PRINTER);
puts(IN);
END_FUNCTION_BLOCK
-------------------------------------------------------------main.cpp--------------------------------------------------------------------
#include
#include
extern "C" unsigned int gnExternalIf;
extern "C" void myprogram(void);
int main(int argc, char * argv[]){
gnExternalIf = 56;
myprogram();
std::cout << "Number is " << std::to_string(gnExternalIf) << std::endl;
return 0;
}
Beta Was this translation helpful? Give feedback.
All reactions