Welcome back to Micom SE Studio1 Import Filter tutorial series. In the previous tutorial, I've shown you how to create user interface. Click this link Micom SE Studio1 Import Filter - Creating User Interface (UI)
In this series, I will show you how to add class module that responsible for setting file processing. Meaning, the relay has a setting configuration which can be exported in readable file like text file, xml file or csv file & etc. In this tutorial, I'm going to use text file (.txt).
Now, add new class in the SEStudio1 folder and name it with SEStudio1Import.vb. All the data from setting file will be process in this class. Copy the code below and paste it it to import section.
Imports System.IO Imports OMXRioData
Next, we need to declare some names, functions and procedures to be used in this class module. Copy the codes below and paste them inside the SEStudio1Import.vb.
Private _Document As OMXRioData.IAutoXRioDocument 'Storage of XRIO document Private RelayParameters As String = "CUSTOM.RELAY_PARAMETERS" 'Path directory of the relay paraters node at the XRIO Private ParentNodeName As String = "RELAY_PARAMETERS" 'The node name of the last node from the path directory of the relay parameters Private ImportedSetting As List(Of Parameter) 'Storage of the imported parameter in list Public Event AddEvent(ByVal Message As String) 'For displaying messages whithout freezing the UI Public HasError As Boolean = False Public ErrorMessages As List(Of ErrorMessage) 'Storage of the error detected Public Sub New(ByVal xDocument As IAutoXRioDocument) _Document = xDocument End Sub Public ReadOnly Property Document As IAutoXRioDocument 'Storage of XRIO document Get Return _Document End Get End Property 'Get the name of the current node Private Function GetNodeName(ByVal Node As IAutoXRioBlock) As String If Node.Id = ParentNodeName Then Return "" Else Return GetNodeName(Node.ParentElementNode).Trim & "/" & Node.Name.Trim End If End Function Private Sub SetErrorMessages(mName As String, errMessage As String) If ErrorMessages Is Nothing Then ErrorMessages = New List(Of ErrorMessage) End If Dim Item As New ErrorMessage With { .Title = mName, .Message = errMessage } ErrorMessages.Add(Item) End Sub Public Sub AddCustomEvent(Message As String) 'This will be access outside this class to raise a message event RaiseEvent AddEvent(Message) End Sub
Next, we need to create function that responsible for opening and reading and parsing the setting file to load into the a list of parameters. Copy and paste the codes below to SEStudio1Import.vb.
Public Function OpenFileAndImport(ByVal Filename As String) As Boolean Dim Result As Boolean = False Try ImportedSetting = New List(Of Parameter) Dim NewSettings As List(Of Parameter) = ReadMicomFile(Filename) If NewSettings IsNot Nothing Then ImportTheSettingV2(NewSettings) Result = True End If Catch ex As Exception MsgBox("The import filter might not be compatible to current test plan/routines.", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Unexpected Error") End Try Return Result End Function Public Function ReadMicomFile(ByVal Filename As String) As List(Of Parameter) Dim Result As New List(Of Parameter) Dim BypassString As New List(Of String) Dim Grouped As New List(Of GroupToImport) Dim FileType As String = String.Empty Dim FormatVersion As String = String.Empty Dim RelayType As String = String.Empty Dim ModelNumber As String = String.Empty Dim Firmware As String = String.Empty Dim SerialNumber As String = String.Empty Dim IncludeReadOnly As Boolean Dim IncludeHidden As Boolean Using sr As New StreamReader(Filename) Do While Not sr.EndOfStream Dim Line As String = sr.ReadLine If Line.Contains("%") Then Dim SplittedLine As String() = Line.Split(":") If Line.Contains("FileType") Then FileType = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("FormatVersion") Then FormatVersion = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("RelayType") Then RelayType = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("ModelNumber") Then ModelNumber = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("Firmware") Then Firmware = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("SerialNumber") Then SerialNumber = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("IncludeReadOnly") Then IncludeReadOnly = SplittedLine(UBound(SplittedLine)).Trim ElseIf Line.Contains("IncludeHidden") Then IncludeHidden = SplittedLine(UBound(SplittedLine)).Trim Else GoTo OptionElse End If Else OptionElse: Dim SplittedLine As String() = Line.Split("|") If SplittedLine.Count = 7 Then '0 ID '1 Block/Parent Node '2 Parameter name '3 Current Value '4 Range Low '5 Range Hi '6 Step If Not IsNotIncluded(SplittedLine(1).Trim) Then Dim nParam As New Parameter With nParam .Parameter = SplittedLine(0).Trim .Name = SplittedLine(2).Trim .LowerLimit = SplittedLine(4).Trim .UpperLimit = SplittedLine(5).Trim Dim ValueSplit As String() = SplittedLine(3).Trim.Split(" ") If ValueSplit.Count = 2 Then If IsNumeric(ValueSplit(0).Trim) And Not IsNumeric(ValueSplit(1).Trim) Then .Value = ValueSplit(0).Trim .Unit = ValueSplit(1).Trim Else .Value = SplittedLine(3).Trim End If Else If SplittedLine(3).Trim.Contains("%") And ValueSplit.Count = 3 Then .Value = ValueSplit(0).Trim '/ 100 .Unit = ValueSplit(2).Trim .LowerLimit = Val(SplittedLine(4).Trim) '/ 100 .UpperLimit = Val(SplittedLine(5).Trim) '/ 100 Else .Value = SplittedLine(3).Trim End If End If End With Result.Add(nParam) End If End If End If Loop End Using Return Result End Function Private Function IsNotIncluded(Name As String) As Boolean Dim exparams As New List(Of String) With exparams .Add("RECORD CONTROL") .Add("DISTURB RECORDER") .Add("MEASURE'T SETUP") .Add("COMMISSION TESTS") .Add("CB MONITOR SETUP") .Add("OPTO CONFIG") .Add("CTRL I/P CONFIG") .Add("FUNCTION KEYS") .Add("ETHERNET NCIT") .Add("CTRL I/P LABELS") .Add("INPUT LABELS") .Add("OUTPUT LABELS") .Add("Event Control") .Add("Disturbance Records") .Add("Contacts") .Add("OUTPUT RELAYS") .Add("INPUTS") .Add("DVICE Module") End With Return MultiContains(Name, exparams.ToArray) End Function Public Function MultiContains(str As String, ParamArray values() As String) As Boolean Return values.Any(Function(val) str.Contains(val)) End Function
Public Function GetImportedSetting() As List(Of Parameter) Return ImportedSetting End Function
You don't need to change anything in ReadMicomFile function, That's pretty much it. The only thing can be change there is the IsNotIncluded function. You can add or remove some keywords inside the exparams list. If you want to include for example "OUTPUT RELAYS", just remove that line so that all the lines in the setting files that have "OUTPUT RELAYS" words will be included in the list of parameters to be imported in the XRIO document.
Where not done yet, because you will realize that the codes will give you an error from OpenFileAndImport function. We need to create another functions that responsible for reading the XRIO Document and apply the necessary setting value.
Copy and paste the codes below to SEStudio1Import.vb class and do not modify them especially the set procedures. That's the heart of the import filter I made. You can use that for all the relays if you want to. The only thing you need to change is the opening, reading and parsing file settings.
#Region "Importing setting region" 'Settings processor Private Function ImportTheSettingV2(ByVal File As List(Of Parameter)) Try Dim Block As IAutoXRioBlock = Document.GetBlockFromIDs(RelayParameters) If Block.Parameters.Count > 0 Then For Each Param As OMXRioData.AutoParameter In Block.Parameters If Not String.IsNullOrEmpty(Param.ForeignId) Then Dim Parameter As Parameter = File.Find(Function(x) x.Parameter.Trim.ToUpper = Param.ForeignId.Trim.ToUpper) If Parameter IsNot Nothing Then SetTheNewSetting(Param, Parameter) File.Remove(Parameter) End If End If Next End If If Block.Blocks.Count > 0 Then ReadAllBlocksAndSetParametersV2(Block, File) End If Return True Catch ex As Exception Throw New Exception("Error found in ImportTheSettingV2 function.", ex.InnerException) Return False End Try End Function Private Sub ReadAllBlocksAndSetParametersV2(ByVal Block As OMXRioData.IAutoXRioBlock, File As List(Of Parameter)) For Each Item As OMXRioData.IAutoXRioBlock In Block.Blocks If Item.Parameters.Count > 0 Then For Each Param As OMXRioData.AutoParameter In Item.Parameters If Not String.IsNullOrEmpty(Param.ForeignId) Then Dim Parameter As Parameter = File.Find(Function(x) x.Parameter.Trim.ToUpper = Param.ForeignId.Trim.ToUpper) If Parameter IsNot Nothing Then SetTheNewSetting(Param, Parameter) File.Remove(Parameter) End If End If Next End If If Item.Blocks.Count > 0 Then ReadAllBlocksAndSetParametersV2(Item, File) End If Next End Sub Private Sub SetTheNewSetting(ByVal ItemParameter As OMXRioData.IAutoXRioParameter, ByVal Item As Parameter) RaiseEvent AddEvent("Please wait: Importing " & Item.Value & " " & Item.Unit & " to " & GetNodeName(ItemParameter.ParentElementNode).TrimStart("/") & "/" & ItemParameter.Name & ".") If ItemParameter.DataType = OMXRioData.ParamDataType.String Then SetTheStringSetting(ItemParameter, Item) ElseIf ItemParameter.DataType = OMXRioData.ParamDataType.Integer Then SetTheIntegerSetting(ItemParameter, Item) ElseIf ItemParameter.DataType = OMXRioData.ParamDataType.Real Then SetTheDoubleSetting(ItemParameter, Item) ElseIf ItemParameter.DataType = OMXRioData.ParamDataType.Enumeration Then SetTheEnumSetting(ItemParameter, Item) ElseIf ItemParameter.DataType = OMXRioData.ParamDataType.Boolean Then SetTheBooleanSetting(ItemParameter, Item) End If End Sub Private Sub SetTheBooleanSetting(ByVal ItemParameter As IAutoXRioParameter, ByVal Item As Parameter) Try Dim ErrorMessage As String = String.Empty Item.Name = GetNodeName(ItemParameter.ParentElementNode).TrimStart("/").Trim & "/" & ItemParameter.Name.Trim Item.OldValue = ItemParameter.DisplayString If ItemParameter.SetDisplayValue(Item.Value, ErrorMessage) Then Item.Comment = "Changed parameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) Else Document.Errors.AddError(ErrorType.Warning, ErrorGroup.Filter, 0, 0, 0, Document.GetUserPathFromParam(ItemParameter), Document.GetIDsPathFromParam(ItemParameter), "Imported value could not be set.") End If Catch ex As Exception HasError = True SetErrorMessages("Could not set " & ItemParameter.ParentElementNode.Name & " " & ItemParameter.Name & " to " & Item.Value & ".", ex.Message) End Try End Sub Private Sub SetTheEnumSetting(ByVal ItemParameter As IAutoXRioParameter, ByVal Item As Parameter) Try Dim ErrorMessage As String = String.Empty Item.Name = GetNodeName(ItemParameter.ParentElementNode).TrimStart("/").Trim & "/" & ItemParameter.Name.Trim Item.OldValue = ItemParameter.DisplayString If ItemParameter.SetDisplayValue(Item.Value, ErrorMessage) Then Item.Comment = "Changed ItemParameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) ElseIf IsNumeric(Item.Value) Then If ItemParameter.SetDisplayValue(ItemParameter.ParameterEnum.EnumValues.Item(Int(Item.Value) + 1).DisplayName, ErrorMessage) Then Item.Comment = "Changed ItemParameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) Else Document.Errors.AddError(ErrorType.Warning, ErrorGroup.Filter, 0, 0, 0, Document.GetUserPathFromParam(ItemParameter), Document.GetIDsPathFromParam(ItemParameter), "Imported value could not be set.") End If ElseIf Not IsNumeric(Item.Value) Then If ItemParameter.SetDisplayValue(ItemParameter.ParameterEnum.EnumValues.Item(ConvertONOFF(Item.Value) + 1).DisplayName, ErrorMessage) Then Item.Comment = "Changed ItemParameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) End If Else Document.Errors.AddError(ErrorType.Warning, ErrorGroup.Filter, 0, 0, 0, Document.GetUserPathFromParam(ItemParameter), Document.GetIDsPathFromParam(ItemParameter), "Imported value could not be set.") End If Catch ex As Exception HasError = True SetErrorMessages("Could not set " & ItemParameter.ParentElementNode.Name & " " & ItemParameter.Name & " to " & Item.Value & ".", ex.Message) End Try End Sub Private Function ConvertONOFF(ByVal Status As String) As Integer If Status = "ON" Then Return 1 ElseIf Status = "OFF" Then Return 0 Else Return -1 End If End Function Private Sub SetTheStringSetting(ByVal ItemParameter As IAutoXRioParameter, ByVal Item As Parameter) Try Dim ErrorMessage As String = String.Empty Item.Name = GetNodeName(ItemParameter.ParentElementNode).TrimStart("/").Trim & "/" & ItemParameter.Name.Trim Item.OldValue = ItemParameter.DisplayString If Not String.IsNullOrEmpty(Item.Unit) Then Item.Value = Item.Value & " " & Item.Unit Item.Unit = String.Empty End If If ItemParameter.SetDisplayValue(Item.Value, ErrorMessage) Then Item.Comment = "Changed parameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) Else Document.Errors.AddError(ErrorType.Warning, ErrorGroup.Filter, 0, 0, 0, Document.GetUserPathFromParam(ItemParameter), Document.GetIDsPathFromParam(ItemParameter), "Imported value could not be set.") End If Catch ex As Exception HasError = True SetErrorMessages("Could not set " & ItemParameter.ParentElementNode.Name & " " & ItemParameter.Name & " to " & Item.Value & ".", ex.Message) End Try End Sub Private Sub SetTheIntegerSetting(ByVal ItemParameter As IAutoXRioParameter, ByVal Item As Parameter) Try Dim ErrorMessage As String = String.Empty Item.Name = GetNodeName(ItemParameter.ParentElementNode).TrimStart("/").Trim & "/" & ItemParameter.Name.Trim Item.OldValue = ItemParameter.DisplayString If ItemParameter.SetDisplayValue(Item.Value, ErrorMessage) Then Item.Comment = "Changed parameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) Else Document.Errors.AddError(ErrorType.Warning, ErrorGroup.Filter, 0, 0, 0, Document.GetUserPathFromParam(ItemParameter), Document.GetIDsPathFromParam(ItemParameter), "Imported value could not be set.") End If Catch ex As Exception HasError = True SetErrorMessages("Could not set " & ItemParameter.ParentElementNode.Name & " " & ItemParameter.Name & " to " & Item.Value & ".", ex.Message) End Try End Sub Private Sub SetTheDoubleSetting(ByVal ItemParameter As IAutoXRioParameter, ByVal Item As Parameter) Try Dim ErrorMessage As String = String.Empty Item.Name = GetNodeName(ItemParameter.ParentElementNode).TrimStart("/").Trim & "/" & ItemParameter.Name.Trim Item.OldValue = ItemParameter.DisplayString If Not ItemParameter.ParameterDouble.IsValidInternalMaxValue(Item.Value, Nothing) Or Not ItemParameter.ParameterDouble.IsValidInternalMinValue(Item.Value, Nothing) Then End If ItemParameter.ParameterDouble.InternalMin = Val(Item.LowerLimit.Trim) ItemParameter.ParameterDouble.InternalMax = Val(Item.UpperLimit.Trim) Dim _v As String() = Item.Value.Split(" ") If Not IsNumeric(_v(0)) Then Item.Value = ConvertWordToNumber(_v(0)) Item.Unit = _v(1) End If If ItemParameter.SetDisplayValue(Item.Value, ErrorMessage) Then If Not String.IsNullOrEmpty(Item.Unit) Then If Item.Unit.Contains("m") And Left(Item.Unit.Trim, 1) = "m" And Item.Unit.Trim <> "min" And Item.Unit.Trim <> "mn" Then ItemParameter.ParameterDouble.Unit = Item.Unit.Replace("m", "") ItemParameter.ParameterDouble.MultiplierSymbol = "m" ElseIf Item.Unit.Contains("k") And Left(Item.Unit.Trim, 1) = "k" Then ItemParameter.ParameterDouble.Unit = Item.Unit.Replace("k", "") ItemParameter.ParameterDouble.MultiplierSymbol = "k" ElseIf Item.Unit.Contains("u") And Left(Item.Unit.Trim, 1) = "u" Then ItemParameter.ParameterDouble.Unit = Item.Unit.Replace("u", "") ItemParameter.ParameterDouble.MultiplierSymbol = "u" ElseIf Item.Unit.Contains("M") And Left(Item.Unit.Trim, 1) = "M" Then ItemParameter.ParameterDouble.Unit = Item.Unit.Replace("M", "") ItemParameter.ParameterDouble.MultiplierSymbol = "M" ElseIf Item.Unit.Contains("G") And Left(Item.Unit.Trim, 1) = "G" Then ItemParameter.ParameterDouble.Unit = Item.Unit.Replace("G", "") ItemParameter.ParameterDouble.MultiplierSymbol = "G" End If End If Item.Value = Strings.FormatNumber(Val(Item.Value), ItemParameter.ParameterDouble.DecimalPlaces) Item.Comment = "Changed parameter value from " & Item.OldValue & " to " & Item.Value ImportedSetting.Add(Item) Else Document.Errors.AddError(ErrorType.Warning, ErrorGroup.Filter, 0, 0, 0, Document.GetUserPathFromParam(ItemParameter), Document.GetIDsPathFromParam(ItemParameter), "Imported value could not be set.") End If Catch ex As Exception HasError = True SetErrorMessages("Could not set " & ItemParameter.ParentElementNode.Name & " " & ItemParameter.Name & " to " & Item.Value & ".", ex.Message) End Try End Sub Private Function ConvertWordToNumber(Data As String) As String If Data = "Blocked" Then Return "+inf" Else Return Val(Data) End If End Function #End Region
In the next tutorial, I will show you how to use the SEStudio1Import.vb in the SEStudio1FilterBase.bv. See you next time.
Comments
Post a Comment