News:

  • November 23, 2026, 11:17:57 AM

Login with username, password and session length

Author Topic: How to Make Reusable Code Blocks  (Read 28330 times)

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: How to Make Reusable Code Blocks
« Reply #15 on: November 24, 2021, 01:44:34 AM »
It doesn't appear as though you can program with unassigned structures. For example if I name a TMR tTestTimer and initially assign it as an unassigned structure, as I'm writing my generic code using unassigned variables, I'm not allowed to use tTestTimer.done elsewhere in my generic code.

That is correct. Unassigned nicknames are typeless, and there isn't currently a way to associate type information with the documentation record.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3858
    • Host Engineering
Re: How to Make Reusable Code Blocks
« Reply #16 on: November 24, 2021, 08:38:50 AM »
It doesn't appear as though you can program with unassigned structures. For example if I name a TMR tTestTimer and initially assign it as an unassigned structure, as I'm writing my generic code using unassigned variables, I'm not allowed to use tTestTimer.done elsewhere in my generic code.

An alternative to unassigned nicknames for a structure would be to use a heap item, e.g. change NN tTestTimer for T42 to a heap item called tTestTimer.  Unless you are using arrays on your T block or the original C-more driver (there's the symbolic driver), a heap item works just as well.

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to Make Reusable Code Blocks
« Reply #17 on: November 24, 2021, 11:53:37 AM »
What would it take to create an import function that would allow you to automatically reassign memory locations for variables if they were already in use in the current project and the nicknames didn't match?

If you make the "reassignable" memory locations as unassigned nicknames after you export them, the work flow to resolve them is already there.  You can even do it with your existing export file (assuming you assigned nicknames to everything that is reassignable).

Here's an example section of an export file:

Code: [Select]
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
"X1","SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
"WX1","RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
"C1","PauseSimRS","",""
#END

Say we want all the ID 1's to be "reassignable" (so X1, WX1, C1), change those lines to the following
Code: [Select]
#BEGIN ELEMENT_DOC
"X0","SimPIDLoopMode","","OFF - Manual","ON - Auto"
UB,"SP_Selection","","OFF - Thermostat","ON - Ramp/Soak Profile"
"WX0","RawOvenTemp","12 bit analog",""
UN,"RawThermostat","12 bit analog",""
"C0","JogSimRS","leading edge",""
UB,"PauseSimRS","",""
#END
UB - means Unassigned Bit
UN - means Unassigned Numeric
US - means Unassigned Struct

After you import the code using Tools->Insert Instructions from File, go to the Tools->Assign Nicknames utility and resolve them for THAT specific project, e.g. RawThemostat may be WX5 in one project, D42 in another.

As a test, I tried to insert the following from file:

STR bMasterRun
TMR tRunDlyTimer 10000

// Rung $Main#2
// Offset 3
STR tRunDlyTimer.Done
OUT bRunMotor

// End of Code Block $Main
$PGMEND $Main

#BEGIN ELEMENT_DOC
UB,"bMasterRun","",""
UB,"bRunMotor","",""
US,"tRunDlyTimer","",""
#END

I get an error upon insertion that the data size must be one of bit, but this is a structure (because of the timer). If I define it as a UB, I get an error telling me that it must be a structure but it's defined as a bit. 




franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3858
    • Host Engineering
Re: How to Make Reusable Code Blocks
« Reply #18 on: November 24, 2021, 12:38:14 PM »
STR tRunDlyTimer.Done
won't work since tRunDlyTimer is just an Unassigned Structure (not any specific structure)
You will need to do one of the following
1. define a different nickname tRunDlyTimerDone (no dot) as Unassigned Bit OR
2. Use a heap item of a Timer called tRunDlyTimer (not a nickname, but a heap item)

#2 is better because tRunDlyTimer IS a Timer (no need to "assign" it in the new project)

RBPLC

  • Hero Member
  • *****
  • Posts: 586
Re: How to Make Reusable Code Blocks
« Reply #19 on: November 24, 2021, 01:11:43 PM »
I appreciate the recommendations but the work-arounds are too cumbersome in my opinion to try to effectively implement. I would attempt the unassigned variables route but then have to create a heap item in addition to this to deal with structures is a bridge too far for me. I guess keep this on the list for future improvements. 

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: How to Make Reusable Code Blocks
« Reply #20 on: November 24, 2021, 01:54:29 PM »
I appreciate the recommendations but the work-arounds are too cumbersome in my opinion to try to effectively implement. I would attempt the unassigned variables route but then have to create a heap item in addition to this to deal with structures is a bridge too far for me. I guess keep this on the list for future improvements.

We were just talking about a manageable way (in development complexity) to do reusable code objects. Nested UDTs really opens up a lot of options and makes the problem much friendlier. DmD 2.9 is due out shortly, but hopefully we can take a run at in the next release.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Controls Guy

  • Internal Dev
  • Hero Member
  • ****
  • Posts: 3628
  • Darth Ladder
Re: How to Make Reusable Code Blocks
« Reply #21 on: November 25, 2021, 12:38:16 AM »
Have a good Thanksgiving, everyone!   :)
I retract my earlier statement that half of all politicians are crooks.  Half of all politicians are NOT crooks.  There.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: How to Make Reusable Code Blocks
« Reply #22 on: November 25, 2021, 03:35:51 PM »
Have a good Thanksgiving, everyone!   :)

Yes! You too!
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO