Stopwatch in PLCnext Engineer using C#
Hello,
I want to create a class in C# that includes a stopwatch and a switch that is counting how much time the switch has been in the state "ON".
Below is an example of the usage.
using System.Diagnostics; //... void StopwatchUsingMethod() { //A: Setup and stuff you don't want timed var timer = new Stopwatch(); timer.Start(); //B: Run stuff you want timed timer.Stop(); TimeSpan timeTaken = timer.Elapsed; string foo = "Time taken: " + timeTaken.ToString(@"m\:ss\.fff"); }
The problem is I get this error:
CS0246 The type or namespace name 'Stopwatch' could not be found (are you missing a using directive or an assembly reference?)
Can anyone help me how to achieve this?
Thank you in advance!
Comments
Hello Dimitri,
please try the following code, I don't test it but it should work.
BR Eduard
using System;
using System.Iec61131Lib;
using Eclr;
TimeSpan dtDiffTime;
DateTime dtInitTime;
DateTime dtNowTime;
void StopwatchUsingMethod()
{
dtInitTime = DateTime.Now;
dtNowTime = DateTime.Now;
dtDiffTime = dtNowTime - dtInitTime;
Eclr.Log.Info(dtDiffTime.ToString());
}
Or:
DateTime dtNowTime;
void StopwatchUsingMethod()
{
dtNowTime = DateTime.Now;
long startTick = dtNow.Ticks;
long endTick = dtNow.Ticks;
long diffTick = endTick - startTick;
}
The explanation for the error message is that the
Stopwatch
class is not included in the eCLR, which is a subset and specialisation of the complete .NET Framework. The Help file that is included with each C#/eCLR project lists all the classes that can be used in eCLR projects.Hello again,
I have the following problem. I used DateTimes as you mentioned but I think it does not work correctly through PLCnext. The thing is on the first loop of my Program I get the following different results when running my code using Console Application vs running my code through PLCnext. It seems all the DateTime values are null on my PLCnext. Some of these are supposed to be using Datetime.Now
Any thoughts on that? Could you maybe test some DateTime code to verify this?
See below image for more info.