0

When I use an enum in a switch statement in C#, I am used to add a debug break statement to the Default case to prevent adding items to the enum which are not covered by the switch. During debugging, the code will then break if it hits the Default case.

Now I am programming a beckhoff PLC and want to do the same in a CASE .. OF ELSE ...END CASE in STL. Is this possible and/or normal in PLC programming?

Twan
  • 39
  • 9

4 Answers4

0

I don’t think you can. Also it wouldn’t be desirable to stop a PLC program and prevent it from executing machine relevant code.

Instead you could use the ADSLOGSTR function to log to the event logger. Or show a message box. This will work in both TC2 and TC3.

pboedker
  • 523
  • 1
  • 3
  • 17
0

You can set breakpoints when you are in online-mode, but as pboedker pointed out as soon as the breakpoint is reached (unless you have a special configuration, but this is another subject) your ethercat master will timeout, your safety module will produce a com error and your drives will need a reset aswell.

If you don't have real hardware and an ethercat master attached in your project you can use breakpoints without any worries.

I personally take another approach.

I always build a separate Debug-Visualization in the plc together with a special Debug FunctionBlock which helps me to track bugs in the project.

In your case for example I would simply call a special method of the Debug-FunctionBlock with an errror code and a string when the program flow reaches the default-case. The error code and the string would then be visualized in the Debug-Visualization.

Even if it's a little more effort than simply calling adslogstr I would rather implement a separate Debug-FunctionBlock for 3 reasons:

  1. You need more logic than simply calling adslogstr anyway because if by any chance adslogstr is called cyclically, you end up spamming the event logger.
  2. Reuse in other projects
  3. You can expand the Debug-Visualization to a Test-Suite if needed, which can come in handy

You can find more info about the beckhoff visualization here:

https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/3523377803.html&id=

Filippo Boido
  • 1,136
  • 7
  • 11
0

Breakpoints are possible like Filippo said. You can prevent outputs from being reset during breakpoint by setting KeepOutputsOnBP (see this: https://stackoverflow.com/a/52158801/8140625).

You could also set error/warning/note message to your Visual Studio when that happens by using ADSLOGSTR(see this: https://stackoverflow.com/a/51700613/8140625). So add a ADSLOGSTR call to your CASE ELSE with appropriate message and you will see it in error list / TwinCAT console.

Edit: Somehow missed pboedkers answer, he already answered the ADSLOGSTR.

Quirzo
  • 1,183
  • 8
  • 10
  • KeepOutputsOnBP is a nice hint, but I believe that the safety module will produce a comError anyway and consequently set the whole machine in error state. – Filippo Boido Sep 17 '19 at 08:22
  • 1
    Might be, haven't used with safety logic. Only with normal and no problems. – Quirzo Sep 17 '19 at 11:51
0

I like the solution of Filippo. Is could be easy to change the behavior of the debug function in the future without touching the code to much.

I was thinking to much in the C# solutions :) Thank!

Twan
  • 39
  • 9
  • Hey Twan, "To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in." https://stackoverflow.com/help/someone-answers – Filippo Boido Sep 21 '19 at 16:13