Author Topic: Writing to the Recipe.ini file  (Read 2263 times)

Steve Etter

  • Newbie
  • *
  • Posts: 26
    • View Profile
Writing to the Recipe.ini file
« on: March 29, 2018, 02:01:30 PM »
I've managed to work through the RecipeButton.vb read the recipes from my Recipe.ini file way I want, but now I want to be able to over-write values for recipe changes.  It appears in the parser.vb file that this functionality should be there already, but I haven't been able to get my file to change.

Here is the code I've tried so far and while it appears to work (no errors), it doesn't do anything to the file designated in the IniFileName section.

Code: [Select]
    Protected Overrides Sub OnClick(e As EventArgs)
        MyBase.OnClick(e)

        '* Make sure an INI file was specified
        If Not String.IsNullOrEmpty(m_IniFileName) Then
            If String.IsNullOrEmpty(RecipeFileError) Then
                ' Get the file name from the button configuration
                Dim file As New AdvancedHMIDrivers.IniParser(m_IniFileName)
                ' Get the file section from the PLC as per the button configuration
                Dim section As String = m_ComComponent.Read(m_PLCAddressSaveSection, 1)(0)
                ' Verify the file section exists
                If Not String.IsNullOrEmpty(section) Then
                    Dim c() As Char = {vbNullChar}
                    Dim TrimmedSection As String = section.Trim(c)
                    Dim SettingList() As String = file.ListSettings(section.Trim(c))
                    Settings.Clear()

                    For index = 0 To SettingList.Length - 1
                        If String.Compare(SettingList(index), "ButtonText", True) <> 0 Then
                            Dim s As New RecipeSaveSetting(SettingList(index), section)
                            Settings.Add(s)
                        Else
                            Text = file.GetSetting(TrimmedSection, SettingList(index))
                        End If
                    Next
                End If
                If m_ComComponent IsNot Nothing Then
                    For i = 0 To Settings.Count - 1
                        Dim v As String
                        Try
                            v = ""
                            v = m_ComComponent.Read(Settings(i).PLCAddress, 1)(0)
                            While v Is ""

                            End While
                            file.DeleteSetting(section, Settings(i).PLCAddress)
                            file.AddSetting(section, Settings(i).PLCAddress, v)
                        Catch ex As Exception
                            System.Windows.Forms.MessageBox.Show("Faile to write " & Settings(i).Value & " to " & Settings(i).PLCAddress & " ." & ex.Message)
                        End Try
                    Next
                Else
                    System.Windows.Forms.MessageBox.Show("ComComponent Property must be set.")
                End If
            Else
                System.Windows.Forms.MessageBox.Show("INI File Error - " & RecipeFileError)
            End If
        End If
    End Sub

Any help?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Writing to the Recipe.ini file
« Reply #1 on: March 29, 2018, 02:18:36 PM »
Unless I am overlooking it, I do not see where you are calling the SaveSettings method of the IniParser

Steve Etter

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Writing to the Recipe.ini file
« Reply #2 on: March 29, 2018, 02:37:14 PM »
No, you're absolutely right, there isn't a SaveSetting method that I've found, either.  For this go-around, I tried using the DeleteSettings method followed by the AddSettings method.  Neither did anything to the file.  I'm guessing I need to "open" the file first, but that's only a guess.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Writing to the Recipe.ini file
« Reply #3 on: March 29, 2018, 02:50:33 PM »
In the IniParser.vb file, starting at line 196 is the method for saving:

Code: [Select]
    ''' <summary>
    ''' Save settings to new file.
    ''' </summary>
    ''' <param name="newFilePath">New file path.</param>
    Public Sub SaveSettings(newFilePath As String)
        Dim sections As New ArrayList()

This is what you need to call form your code.

Steve Etter

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Writing to the Recipe.ini file
« Reply #4 on: March 29, 2018, 02:58:45 PM »
I looked at that for just a moment but dismissed it thinking it was only creating a new file.  I will try working with that and see what I can do.

Thanks
Steve

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Writing to the Recipe.ini file
« Reply #5 on: March 29, 2018, 03:08:55 PM »
I looked at that for just a moment but dismissed it thinking it was only creating a new file. 
Technically that what it is doing or it is overwriting the existing.

Steve Etter

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Writing to the Recipe.ini file
« Reply #6 on: March 29, 2018, 04:14:17 PM »
Well it works, but not quite the way I need it to.  My goal is to capture the current recipe data from the PLC and write that to the file.  This appears to write the data that was loaded when the project started.  Assuming that's what's happening, if I had a method for updating the Recipe.ini file that's in memory before writing the file, that would work too.

I will try more tomorrow.