calling a function block from ST

carcrosser
2013-08-07
2013-08-15
  • carcrosser - 2013-08-07

    It sounds rather ridiculous, I know. But CoDeSys is not totally new to me, but i Only modified existing programs.
    Now I'm writing one from scratch, but it's rather complicated.

    I use a Wago PLC (750-880). But will use CAN as remote I/O option (with the CAN gateway 750-658) as we use several devices that have J1939 connections.

    We need the ethernet connection for logging to MySQL and updating software and settings in the program (will use FTP for that one)

    We require local logging on the internal SD card.

    As the PLC is ordered but not yet delivered, I'm basicly stuck at a rather simple point.
    I want to call a function block from my ST program.

    I've read this: http://www.plctalk.net/qanda/showthread.php?t=65890
    and this: http://www05.abb.com/global/scot/scot20 ... 4m0202.pdf (page 13 & 14)

    however things are not completely clear to me
    I should expect that I need to put the function block in same POU or function as well, am I right?
    How do I set the inputs and return the outputs?

    for example I would like to call the MYSQL_LOGIN function block from wagolibmysql.lib
    it has following inputs:
    sHost
    uiPort
    sUsername
    sPassword
    sDatabase
    oMySQL
    xStart

    and following outputs:
    diError
    sStatus
    xConnected
    oMysql
    xStart

    can I call this function like this?
    MYSQL_LOGIN.sHost := "my_server_location";
    MYSQL_LOGIN.uiPort := "3306";
    MYSQL_LOGIN.sUsername := "user";
    MYSQL_LOGIN.sPassword := "pass";
    etc. etc.

    and in return:
    diErrornumeric := MYSQL_LOGIN.diError;

    I'm struggeling as I'm basically an electronics guy, doing lots in C on Atmel microcontrollers. Also I've done more tricks on PHP, but this PLC programming professionally I've only use PRO-H from hitachi when I was still at school... So please don't kill me if i Ask pretty simple questions.

     
  • shooter - 2013-08-08

    as it is a FB you will have to declare it in the VAR.

    like MYSQL:SQLblock;
    and yes then you can use the form you describe, or
    on a empty line press F2 then find the block.
    it will give all in and outs.

     
  • carcrosser - 2013-08-09

    Thknk you for your reply,

    however I'm failing to get the declaration in the variable area right. the function block in the library I require is mysql_login
    i think for this instance i need to declare something as this function block

    so i have to link these 3 options together
    MySql_login ("the model" of the function block)
    Function_block (the declaration of a function block)
    MadeUpName (the variable i'm calling in the POU)

    then in the POU i'm able to call MadeUpName("input1", input2, input3 etc)

    However I don't even see how i can access the output of this function block. As an ST program is being executed from the top I expect the function block to be executed and straight after the call of this function I can check for success or failure.

     
  • shooter - 2013-08-09

    in the varlist:
    function_block:mysql_login;

    in the program just press F2
    change the blockname from mysql_login to function_block
    now all the ins and outs will be in in the program indeed with := and the outputs as =>
    you can also use function_block.name. this can also be done with F2 just look in the table under user defined function blocks.

    trendmaker.zip [10.55 KiB]

     
  • carcrosser - 2013-08-12

    I tried first with a more easy example: the file open function

    i tried this one:

       SYSLOG         :   SysFileOpen;
    

    SysFileOpen is a function block from the Syslibfile

    however during building I get this error: "Error 3740: INIT (5): unknown type 'SysFileOpen'

    in the code I call for the FB like this:

    SYSLOG(sSYSLOGFile, 'RW');
    

    This call to function blocks is really freaking me out, feeling so dumb....

    PS Paul: ik Took a look at your example, but that's your custom built function block. Mine comes straight out of a library

     
  • shooter - 2013-08-12

    there are two problems:
    watch how all inputs are declared, in the docs it says string (how long)
    same for output
    please do not use capital but small letters.

     
  • carcrosser - 2013-08-12

    I don't see the point in this as input 1 is already a string and the other one is even a static string in the code.

    here's the full variable declaration

    VAR
       syslog      :   SysFileOpen;
       ssyslogfile      :   STRING;
       sysevent      :   SysFileOpen;
       ssyseventfile   :   STRING;
    END_VAR
    

    and the full code

    ssyslogffile := CONCAT('S:\',statfilename);
    syslog(ssyslogfileile, 'RW');
    

    statfilename is declared globally

       statfilename   : STRING(20)   := 'eatSTAT.csv';
    

    however still suffering same error

     
  • Ralph Holz - 2013-08-12

    Hi carcrosser,

    SysFileOpen is a function not a funtion block. Means no instance-declaration is needed.

    Be carefull most of the system-functions are blocking!

    BR
    Ralph

     
  • shooter - 2013-08-12

    ralph can you please give hin an example?

     
  • carcrosser - 2013-08-12

    AHA now i notice the error I'm making.

    however by looking on the library codesys displays this as a function block.... My mistake.

    but still guessing how calling this function is neede I guess something like

    sysfileopen(input1, input2).

    but if i want to use multiple files, how to do so and how to get the output?
    somthing like

    OUTPUT := sysfileopen(input1, input2)

    so it's like C?

     
  • carcrosser - 2013-08-13

    this worked it tried...

    no errors and it executes..

    however I have some specific difficulties with the file open function.
    i tried Kurt Braun's example
    http://www.youtube.com/watch?v=ovTJ5AZY22g

    it fails to open the file on my SD card...

     
  • carcrosser - 2013-08-15

    turns out to be a problem with this specific SD card.

    written the config once, but after deletion of this config, unable to acces the SD card from the PLC

    rather weird, but worked out with another SD card (borrowed from WAGO; nice guys there!)

     

Log in to post a comment.