how to print a message with ADSLOGSTR() function in ST (TC3)
Learn more at Part 3 - Tasks, programs & “Hello world”
1) Example
PROGRAM MAIN
VAR
bRunOnlyOnce : BOOL := FALSE;
END_VAR
------------------------------------------------------------------------
IF NOT bRunOnlyOnce THEN
ADSLOGSTR(msgCtrlMask := ADSLOG_MSGTYPE_ERROR OR ADSLOG_MSGTYPE_LOG,
msgFmtStr := 'Hello %s',
strArg := 'world!');
bRunOnlyOnce := TRUE;
END_IF
2) Documentation
So how does the ADSLOGSTR()
function work?
It has three arguments,
- the first one being the message control mask (msgCtrlMask),
- the second the message formatting string (msgFmtStr),
- and the final one the optional string argument (strArg),
2.1) msgCtrlMask
With the "message control mask" (msgCtrlMask) we can define where the string should go and what type of message it should be treated as
In TwinCAT there is no "console" in the traditional sense as you might be used to using .NET, C++ or Python.
As the TwinCAT kernel and thus our TwinCAT software runs in a real-time environment we need to adhere to those rules.
What we can do is to send a message from our real-time context to the non-real time context, that is to Visual Studio or Windows.
When sending a message to Visual Studio we can present it as either:
- a hint
- a warning
- or an error
We also have a possibility to display the message as either:
- an entry in the Visual Studio error list
- or as a message popup box
We simply do an OR for these to combine it into the properties we want such as OR'ing ADSLOG_MSGTYPE_ERROR with ADSLOG_MSGTYPE_LOG to get an error message into the Visual Studio logger.
2.2) msgFmtStr and strArg
The second argument, msgFmtStr is where you provide the string that you want to be displayed.
It can contain the formatting code %s, which is the string that is provided as argument three in this function.
Whatever is at %s, will be replaced by the third argument strArg. If you done C style output with printf this is similar.
Note: You don't need to do it this way and can just write "Hello world!" directly in the second argument and leave the third argument empty.
2.3) Output (Error List and Popup)
If we run this with the LOG as output, it will be displayed in the Visual Studio Error list.
If we instead run it with the MSGBOX as output, it will be displayed as a popup.
Note: What does the ADS in the ADSLOGSTR mean? We will discuss the ADS protocol in a coming episode of this tutorial but for now you only need to know that ADS is Beckhoffs middleware protocol and it can amongst many other things be used to communicate between the real-time context of our software, that is our TwinCAT3 software in this case and the non-realtime software, in this case the Visual Studio Error List or the Windows Popup.
A word of advise: Don't overuse the ADSLOGSTR() functionality for debugging purposes. It's using a little bit of resources, but that's actually not the reason why I would advise against it. If you're used to printing loads of stuff on the screen for debugging purposes, you will soon find out Beckhoff provides great tools for debugging your software easily and quickly and I would advise to use those tools instead. We will get back to this topic in future episodes of this tutorial.