1 of 1 people found this helpful
This is because Bad Input is a digital state and not a string. So try:
If (CType(PreviousDayValues(counter).Value,DigitalState)).Name.ToString.Equals("Bad Input") Then
Thanks Eugene Lee!
Code is perfectly running for digital 'Bad Input' but the input tag is of datatype "float32" ,so other good data (also updating with bad Input ) is in float format.(sorry I missed to mention it earlier!)
The code line (CType(PreviousDayValues(counter).Value,DigitalState)) is converting value to digital state but not working for float 32.
Hence, throwing the error as:Unable to cast object of type 'system.single' to 'PISDK.DigitalState'.
I want to check the Bad Input values from the bunch of all the values!
Here's what is going on. When the cast in (CType(PreviousDayValues(counter).Value,DigitalState)) fails, it throws an InvalidCastException. You can use TryCast instead. The result of TryCast will be Nothing if it fails.
However, there is a more native way using ACE functions to check if a float tag has a digital state as an event.
First, for numeric PI tags, its values are considered "Bad" if the value is associated with a digital state from the System digital state set. If the value is bad, its PISDK.PIValue.Value property will be of the PISDK.DigitalState type (although you might see it as System.__ComObject in the debugger).
To inspect the name of the digital state, use digstate.Name property, which returns a string.
Therefore, you can try the below example:
Public Overrides Sub ACECalculations() Dim vals As PIValues vals = testACE.Values("y", "t", 0) Dim badCount As Integer badCount = 0 For Each item As PIValue In vals If Not (item.IsGood) Then If (item.Value.Name.Equals("Bad Input")) Then badCount += 1 End If End If Next End Sub
First, it will check if the value is bad (e.g. belongs to a float tag but value is from System digstate). If so, then it checks if the specific digital state is Bad Input. There is probably also a clever LINQ one-liner for this.
Did Eugene and Barry's suggestions help? If you were able to solve the issue, please mark the most useful answer as the correct answer. If not, please let use know how we can help further