<p>We all know the various ways of testing OO systems. However, it looks like I'll be going to do a project where I'll be dealing with PLC ladder logic (don't ask :/), and I was wondering if there's a good way of testing the validity of the system.</p>

<p>The only way I see so far is simply constructing a huge table with all known states of the system and which output states that generates. This would do for simple 'if input A is on, turn output B on' cases. I don't think this will work for more complicated constructions though.</p>

[{'answer_id': 71105, 'author': 'jbdavid', 'author_id': 6314, 'author_profile': 'https://Stackoverflow.com/users/6314', 'pm_score': 4, 'selected': True, 'text': '<p>The verification of "logical" systems in the IC design arena is known as "Design Verification", which is the process of ensuring that the system you design in hardware (RTL) implements the desired functionality. </p>\n\n<p>Ladder logic can be transformed to one of the modern HDL\'s like Verilog.. \ntransform each ladder </p>\n\n<pre><code>|---|R15|---+---|/R16|---------(R18)--------|\n|           |\n|---|R12|---+\n</code></pre>\n\n<p>to an expression like </p>\n\n<pre><code>always @(*) R18 = !R16 &amp;&amp; ( R15 | R12);\n</code></pre>\n\n<p>or you could use an assign statement</p>\n\n<pre><code>assign R18 = R16 &amp;&amp; (R15 | R12); \n</code></pre>\n\n<p>a latching relay</p>\n\n<pre><code>assign R18 = (set condition) || R18 &amp;&amp; !(break condition);\n</code></pre>\n\n<p>Then use a free verilog simulator like <a href="http://www.icarus.com/eda/verilog/" rel="nofollow noreferrer">Icarus</a> to develop a testbench and test your system. \nMake sure you\'re testcases give good CODE coverage of your logic!  And If your ladder editing software gives you decent naming capabilities, use them, rather than Rnn. </p>\n\n<p>(Note: in Ladder Logic for PLC convention, Rnn is for internal relays, while, Xnn is an input and Ynn is an output, as can be quickly gleaned from one of the online tutorials.</p>\n\n<p>Verilog will be an easier language to develop your tests and testbenches in!</p>\n\n<p>It may be helpful to program in some unit delays.</p>\n\n<p>Sorry, I have never looked for ladder logic to/from verilog translators.. \nbut ladder logic in my day was only just being put into a computer for programming PLC\'s - most of the relay systems I used were REAL Relays, wired into the cabinets!!</p>\n\n<p>Good luck. \njbd</p>\n\n<p>There are a couple of ladder logic editors (with simultors) available for free.. \nhere is one that runs on windows supposedly:</p>\n\n<p><a href="http://cq.cx/ladder.pl" rel="nofollow noreferrer">http://cq.cx/ladder.pl</a></p>\n'}, {'answer_id': 693089, 'author': 'rlbond', 'author_id': 72631, 'author_profile': 'https://Stackoverflow.com/users/72631', 'pm_score': 0, 'selected': False, 'text': '<p>There is a program called LogixPro which has an IO simulator for ladder logic, you can try that.</p>\n'}, {'answer_id': 1365819, 'author': 'Ira Baxter', 'author_id': 120163, 'author_profile': 'https://Stackoverflow.com/users/120163', 'pm_score': 2, 'selected': False, 'text': '<p>We\'ve experimented with test coverage tools for Rockwell Control Logix controllers.   Most procedural language test coverage tools do branch coverage or some such; because Relay Ladder Logic typically doesn\'t branch, this doesn\'t work very well. </p>\n\n<p>What we have prototyped is <a href="http://en.wikipedia.org/wiki/Modified_Condition/Decision_Coverage" rel="nofollow noreferrer">MC/DC</a> (modified/condition/decision coverage) for RLL code for Rockwell controllers.. This tells, for each condition in rung, whether that condition has been tested as TRUE, tested as FALSE, and more importantly, if there the condition controlled the output of the decision in the rung (well at least the action controlled by the decision) in both true and false directions under some test.</p>\n\n<p>This work is done using a general purpose program analysis and transformation tool called\n<a href="http://www.semanticdesigns.com/Products/DMS/DMSToolkit.html" rel="nofollow noreferrer">DMS</a> used to instrument the RLL code with additional logic to collect the necessary data.</p>\n\n<p>You still have to code unit tests.  The easiest way to do that is to get another PLC to act as a replacement for the mechanical hardware you intend to control, and simply write another RLL program to exercise the first one.</p>\n'}, {'answer_id': 7591379, 'author': 'daniel', 'author_id': 866502, 'author_profile': 'https://Stackoverflow.com/users/866502', 'pm_score': 0, 'selected': False, 'text': '<p>Sometimes on small PLC programs a test program (or subroutine, or ladder file) is written in the project, which is only run when the project is being emulated. The file has some simple logic that says when an output is energised, turn on the input associated with the feedback. You can then control your PLC through whatever HMI is wired up to it and see that the code behaves as expected. Its very important to disable or delete the test program when the software is downloaded to a real site as it can do very strange things in the real world.</p>\n\n<p>On larger projects each device has a simulation mode that does something slightly similar. <a href="http://www.batchcontrol.com/s88/01_tutorial/06-modules.shtml" rel="nofollow">http://www.batchcontrol.com/s88/01_tutorial/06-modules.shtml</a> </p>\n\n<p>This is nothing like using test frameworks for OO languages, but I haven\'t really seen any test driven development for PLCs, or even much automated testing.</p>\n'}, {'answer_id': 56534301, 'author': 'Chris Johnson', 'author_id': 6228010, 'author_profile': 'https://Stackoverflow.com/users/6228010', 'pm_score': 0, 'selected': False, 'text': '<p>My boss on a constant basis tells me that the testing is built in the logic itself . PLC’s are in fact deterministic so you should practically be able to follow logic and not need to simulate testing. However we’re not perfect. Having framework would really only allow us to step through what we already know, ladder logic really just takes practice to understand how PLCS work.</p>\n\n<p>That being said I did have some good success with a program I made that essentially flipped on and off IO , it could even simulate the counts of an encoder to test what happens when an object gets to a position. Their were assert statements that could get tripped and inform me where my logic faulted. It did catch a few bugs, and that implementation went very well for a system I’ve never touched. It itself was very beneficial and I do think that it could be useful but I’ve gotten a lot better so I find myself not needing it because of my experience.</p>\n'}]
['https://Stackoverflow.com/questions/70993', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/909/']