Jump to content

Calling functions within C function


Kpotmake

Recommended Posts

Hello all,

I was asked to write some software for a machine using a Unitronics PLC, programming in Unilogic is new to me but I've got the machine up and running quite easily, the software is verry intuitive.

However it does lack structured text programming... for the more complex tasks I was told I could write my own C functions. This does however bring some limitations with it, the worst one being not being able to acces the global tags and being limited to 256 bytes of data within the functions.

Is there a way to call other functions within the C function? This would be a workaround for these limitation. For example: I need to loop through an array that has 100 items, and then perform a calculation on these items. The looping part would fit in a C function but the calculation combined with the array size would require more then 256 bytes. If I could call another function when looping through the for loop this would solve my problem.

  • Upvote 1
Link to comment
Share on other sites

Hello,

Well... calling ladder functions from C functions is possible, as you can see below, but although calling C functions from within C functions is also possible, it is not sure it'll help you regarding local variables limitation as I can see in Help the following line

image.png

image.png

  • Thanks 1
Link to comment
Share on other sites

@Saragani, do you know if the 256 bytes limitation is only on local variables or include in/out variables as well?

If it is only on local variables,ย Kpotmake can get along by creating ladder function that declares all local variables and passes them as Function inputs to the C function.

Do I make sense?

ย 

  • Thanks 1
Link to comment
Share on other sites

  • 1 year later...

Hello folks,

I'm trying to use as input variables to aย  C function a couple of unsigned integer arrays.

My output is a single integer variable that contains the total result of the addition of both vectors or arrays.

I have already compiled the code for C and it works well but as soon as I compile again the code on Unilogic the followingย  errors arise.

I would appreciate your help with this issue.

image.thumb.png.7dba3aa529dbae582f357e6288f7af51.png

ย 

The arrays are declared like this ones.

image.png.ab46772944ebc9ff2b96e0e25a5e131d.png

ย 

My output variable is an UINT32 type.

The errors that the Unilogic Compiler shows are the following ones.

image.thumb.png.3f6adcf06dfc59f38c674d8e07499df5.png

ย 

ย 

Link to comment
Share on other sites

Dear fellas,

Let me thanks you for your kindness to share technical solutions.

Concerning my last technical support request, I was making a mistake in the code.

This symbol "*"ย  must be written before the result of a function, for my case (ย  *Profile_Time_Totalizer_1ย  )

this is specific for the C functions in Unilogic because the C compilers doesn't require that.

The arrays are well, also the inputs and output variables.

Please see my basic C code with that mistake corrected below.

image.thumb.png.5ea6723fdcdf64c7327cf6197fca536a.png

ย 

Very truly yours,

Marco๐Ÿ˜€

Link to comment
Share on other sites

Hello@Marco Antonio Mendoza

Happy to hear that you solved your issue!

Here, UniLogic probably saved you some time as the code you wrote before did not operate the intended operation.

It saved a value into the pointer itself and not to the value pointed out by the pointer.

The fact that it worked in another compiler was probably due to a different setting of warnings and errors and the other compiler permitted an assignement of type int* = int, which usually points out an error in the code.

You might have a look onย https://www.programiz.com/c-programming/c-pointersย to understand better how pointers work.

You can also use our new ladder element called "for loop" (search UL help) that would have solved your issue without writing a line of C code.ย 

Hope that helped

Rivka

  • Thanks 1
Link to comment
Share on other sites

Thanks Riva for your help and advice.

I'm taking a look at the article in the link you shared to us.

Just a detail:ย ย  After I compiled the code showed above.

Nor the C compiler "https://www.onlinegdb.com/online_c_compiler"ย ย ย ย  using standard C version,

neither the Unilogic Compiler showed me mistakes.

I need to learn,ย  how a pointer work in fact?.ย  the article point at that.ย 

One more time, thanks for your kindness and guidance.

Very truly yours,

Marco๐Ÿ˜€๐Ÿ‘Œ๐Ÿšฉ

ย 

Link to comment
Share on other sites

Hello,

The corrected code you posted above is indeed correct : you don't assign a value to the pointer itself anymore, but to the value pointed by the pointer.

That's what was intended and both compiler agree that it is ok.

If you want to continue using C functions, you probably need to get acquainted with C pointers (and with C subtilities in general), but if your needs are close to the simple sum loop you just did, consider using the loop ladder element (have a look on the help, it is very simple and no need for C code)ย 

Hope it helped

Rivka

  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

ร—
ร—
  • Create New...