OTHER USER MANUAL VERSIONS
Sepasoft - MES Modules
Cirrus Link - MQTT Modules
Knowledge Base Articles
When code fails mid-execution, it always generates an error message. Where the message appears depends on where the script executed:
If there truly isn't an error message somewhere, but your code isn't doing what you expected, then ask yourself the following questions:
Question #2 is harder to answer: if you knew what it was doing, you wouldn't be stuck! The best way to answer this question is by adding
When a script doesn't perform to expectations, it can suggest a problem with the script's workflow. Some of the pages in this section can offer some suggestions on what to do.
It is common for users that are new to scripting to see an error message, immediately close it without much thought, and then stare at their code as if the problem will politely make it self known. While looking over your code line-by-line will eventually lead you to the problem, the error messages can provide you with a shortcut to the issue. Check out some of the pages in this section for more information on reading an error message.
When testing your scripts, the print command can help you verify that your code is behaving the way it should. This allows you to reconstruct what your code did when it executed. Don't be afraid to add helpful print statements to your code.
Once your code works as expected, remember to either remove or comment out the print statements, so they don't flood the console during normal use as this makes troubleshooting other issue more difficult.
myVar = system.tag.read("folder/tag") # printing out variables you are going to use in an if-statement later allows you to confirm that the values are what you expect them to be. print "myVar is set to: " + str(myVar) # Sometimes viewing the data type of the variable can prove helpful. print "myVar is a: " + type(myVar) # Should your code have multiple if-statements, adding a print statement before and after can show you where the flow of the script went. print "Starting if-statement" if myVar > 100: # If you don't see this print statement, then your if-statement evaluated to False. print "Inside if-statement" doWork() # Printing the end of your script doesn't give you any useful troubleshooting information, but if you need to trigger the script multiple times, it helps delineate each execution. print "Script Ended"
While comments are useful to remind you how your code works, you can also use them to plan your script before you write any code. Break down what you want the code to do into several smaller steps, and then leave comments describing those steps in order. This provides you a chance to review the script's workflow before worrying about syntax. It also provides natural points to stop and test your code, to make sure it is doing what you think it should do.
Unless the script is very simple, avoid writing the entire script and then testing at the end. You may have missed an important line early on, so now you have to adjust all of the code below that line to make the script run. As mentioned above, add print statements, and run your script to make sure it is doing what you think it should.
Additionally, stopping to test your code provides an excellent opportunity to save your project. Get into the habit of saving before you execute any new code.
Instead of doing this:
system.gui.messageBox("Hello you. Glad to see you")
Try to get into the habit of doing this:
message = "Hello you. Glad to see you" system.gui.messageBox(message)
Simple examples like the above don't make for the best use case, but when you have a large script that references a value multiple times, it is easier to declare the value once in a variable, and then just reference the variable throughout your code. If you need to change the value later, then you can simply change it once where you initialize the variable, and you don't have to search every line of your code looking for the value.
When creating variables, try to adopt a naming convention that comes natural to you, and stick to it. If you consistently write variables using the same conventions, you will be less likely to end up with a typo when referencing that variable later in your code. Remember that code is case-sensitive, so something as simple as forgetting to capitalize a letter will cause an error.
This is especially important if you are working in a group on the same project. It's better to get together with your colleagues and agree upon some naming conventions before you write any code.
Ignition's system.* functions use the Camel Case (camelCase) naming convention. That is, the first letter of each word is capitalized except for the very first letter. We recommend that you use it for variable names because It is easy to remember to use Camel Case for both functions and variable names instead of for just one.
When learning how to code, it's not uncommon to run into multiple issues that require you to find a workaround. However, these workarounds can cascade into other issues and make your script more complicated. Consider the following:
"The goal of my script is to access A and then output B...
Shoot, B requires C, so I'll add C...
Wait, C requires that D exists, so I'll create that...
Oh, D needs interfaces E, F, and G, so let's add those in...
Hmm. E needs H and I, F needs J and K, while G needs Y, Z and...A again?!?"
Take a step back and ask yourself "what is this script doing"? If you can't do that in a sentence or two, you may want to rethink the script. If the scope of the script is too large, then it considerably increases the complexity of the code, which in turn could add a plethora of problems later.
If you keep adding workarounds, but you're code is not getting any closer to achieving its end goal, there may be an easier way to accomplish what you're trying to do with a different approach.