Author Topic: Problem when setting IP by code  (Read 2329 times)

Homie

  • Newbie
  • *
  • Posts: 22
    • View Profile
Problem when setting IP by code
« on: March 29, 2016, 11:22:20 AM »
Good Morning

I have a question/problem with changing the IP adress of the driver when starting an application. I am using 3.99d and EtehrnetIPforCLXcom.

To be able to have one application for different machines, I read a setup file and try to change the IP of the driver in the form.open routine.
I am using this code in a called subroutine.

 Private Sub Set_System(ByVal ID As String, Val As String) 'Use incoming values to set the system.
        If ID = UCase("IP1") Then                                              'Set SystemIP for Driver
            IP1 = Val                                                                     'Public String to use this IP on other pages too.
            EthernetIPforCLXCom2.IPAddress = IP1            'Reconfigure Driver.

As soon as I have the last shown line active, the driver won't connect to my CLX. Even overwriting the .IPaddress with its preconfigured value will stop the driver from working. I checked the string that is writen to the diver, and it is fine.
Is there something like a driver/communication restart I have to add, or is this the wrong way of doing things anyway?

Best Regards
Thomas




Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Problem when setting IP by code
« Reply #1 on: March 29, 2016, 11:31:46 AM »
There is an easier way to do this. In the IPaddress property, put the name of a file such as "CLX.ini", then create a file of that name with contents like this:

[IPAddress]
192.168.0.10

Homie

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Problem when setting IP by code
« Reply #2 on: March 29, 2016, 11:51:21 AM »
Thanks for the fast reply.
CLX.ini has to be stored in the applications root directory?
Can I add additonal setup info to that file?


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Problem when setting IP by code
« Reply #3 on: March 29, 2016, 11:59:05 AM »
The file has to be in the same directory as the executable file. The method I use is to add the INI file to the AdvancedHMI project in Solution Explorer and set the CopyToOutputDirectory Property of the file to CopyIfNewer.

The parser will parse the complete file, so you can add anything you want as long as it is in the same INI file format, otherwise the parser could possibly error and not return any information.

If you wanted to make use of the INI parser, this is an example of how to use it:

        Dim p As New AdvancedHMIDrivers.IniParser("CLX.ini")
        Dim ip As String = p.GetSetting("IPADDRESS")

Homie

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Problem when setting IP by code
« Reply #4 on: March 31, 2016, 04:56:05 AM »
Thanks! After some issues it is working.
I got a strange message that the variable "IPAdress" is already existing, and can't be recreated. Didn't use "IPAdress" anywhere. After trying today the fault is gone. Strange!

I tried to expand the CLX.ini to set also the processor slot. But I can't write a string into the ProcessorSlot field.
In our line setup we use a master rack, and have processors running almost the same program in every second slot.
It would be nice to adress also the slot through the ini file.
Is there a way?


Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Problem when setting IP by code
« Reply #5 on: March 31, 2016, 09:19:00 AM »
Edit EthernetIPforCLXCom.vb and go to line 102 then edit this code like this:
Code: [Select]
                        Try
                            If Not Me.DesignMode Then
                                Dim p As New IniParser(value)
                                MyBase.IPAddress = p.GetSetting("IPADDRESS")

                                MyBase.ProcessorSlot = CInt(p.GetSetting("SLOTNUMBER"))
                            End If
                            m_IPIniFile = value
                        Catch ex As Exception

In your CLX.ini file, add this
[SLOTNUMBER]
2

Homie

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Problem when setting IP by code
« Reply #6 on: March 31, 2016, 10:59:05 AM »
I added that one line, but it is not working.
MyBase.ProcessorSlot = CInt(p.GetSetting("SLOTNUMBER"))

As soon as I change the Slotnumber in the driver to a "wrong" slot, communication won't work.
I stepped through the logic and found a line where the slotnumber is overwritten later.

Sub InitializeComponent
Line 137:  Me.EthernetIPforCLXCom1.ProcessorSlot = 0

I thought about a boolean that blocks that line if needed, but that wouldn't be "nice".





Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5267
    • View Profile
    • AdvancedHMI
Re: Problem when setting IP by code
« Reply #7 on: March 31, 2016, 01:04:10 PM »
I was hoping that would be a quick fix, but it didn't take into account the ProcessorSlot would be set after the IPAddress. You should be able to set it in the FormLoad event like this:

Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim p As New AdvancedHMIDrivers.IniParser("CLX.ini")
    EthernetIPforCLXCom1.ProcessorSlot = CINT(p.GetSetting("PROCESSORSLOT"))
End Sub

Homie

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Problem when setting IP by code
« Reply #8 on: April 01, 2016, 05:51:09 AM »
Works perfect now. :-)

What took me some time was the naming change from "Slotnumber" to "Processorslot" during the thread.
I was trying to get the Processorslot, but the ini file was offering the Slotnumber only.
So anyone reading this thread: Use Slotnumber OR Processorslot, but don't mix :D.

Thanks for all the help!