Author Topic: Reading from a module, not a form  (Read 2019 times)

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Reading from a module, not a form
« on: January 25, 2017, 10:41:07 AM »
Is there a way to create a module in VB and read the PLC directly from code? I've tryed it but as you can imagine, it fails as does not see EthernetIPforSLCMicroCom1 as declared.
« Last Edit: January 25, 2017, 11:41:07 AM by Sprungmonkey »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Reading from a module, not a form
« Reply #1 on: January 25, 2017, 10:54:29 AM »
Can you post some code that you have tried? Are you trying to access a driver instance that has been added to the form using the visual designer?

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Reading from a module, not a form
« Reply #2 on: January 25, 2017, 11:40:43 AM »
Hi Archie

Yes, I added a driver (EthernetIPforSLCMicroCom1) in the main form from the designer, then added a new module to the project, I'm trying to read from there (I created LoadDataSave.vb and added the next read code:

        Dim st3LVDTinit As String = Nothing
        Dim st3LVDTfinal As String = Nothing
        Dim st3LVDTF As String = Nothing
        Dim st3Pressure As String = Nothing
        st3LVDTinit = (EthernetIPforSLCMicroCom1.Read("N7:62")) / 100
        st3LVDTfinal = (EthernetIPforSLCMicroCom1.Read("N7:61")) / 100
        st3LVDTF = (EthernetIPforSLCMicroCom1.Read("N7:60")) / 100
        st3Pressure = (EthernetIPforSLCMicroCom1.Read("N7:8")) / 10
        Dim st3PartNumber As String = Nothing
        st3PartNumber = (EthernetIPforSLCMicroCom1.Read("ST19:0"))

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading from a module, not a form
« Reply #3 on: January 25, 2017, 06:27:08 PM »
If I try to use your code as it is, VS immediately shows errors and expects declaration.

Your module should be somewhat similar to this:

Code: [Select]
Module LoadDataSave
    Dim st3LVDTinit As String = Nothing
    Dim st3LVDTfinal As String = Nothing
    Dim st3LVDTF As String = Nothing
    Dim st3Pressure As String = Nothing
    Dim st3PartNumber As String = Nothing
    Public Sub Read()
        st3LVDTinit = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:62")) / 100
        st3LVDTfinal = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:61")) / 100
        st3LVDTF = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:60")) / 100
        st3Pressure = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:8")) / 10
        st3PartNumber = (MainForm.EthernetIPforSLCMicroCom1.Read("ST19:0"))
    End Sub
End Module

Then you can call it when the form loads or if you press a button, similar to this:

Code: [Select]
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadDataSave.Read()
    End Sub

Your variables are created within the module so they cannot be seen outside of it (maybe try declaring them as Public).

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Reading from a module, not a form
« Reply #4 on: January 26, 2017, 09:17:31 AM »
Thanks Godra! I forgot to try that! Variables will not be a problem as they are used only within the module. When ever I have access to the machine I will try your solution.


If I try to use your code as it is, VS immediately shows errors and expects declaration.

Your module should be somewhat similar to this:

Code: [Select]
Module LoadDataSave
    Dim st3LVDTinit As String = Nothing
    Dim st3LVDTfinal As String = Nothing
    Dim st3LVDTF As String = Nothing
    Dim st3Pressure As String = Nothing
    Dim st3PartNumber As String = Nothing
    Public Sub Read()
        st3LVDTinit = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:62")) / 100
        st3LVDTfinal = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:61")) / 100
        st3LVDTF = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:60")) / 100
        st3Pressure = (MainForm.EthernetIPforSLCMicroCom1.Read("N7:8")) / 10
        st3PartNumber = (MainForm.EthernetIPforSLCMicroCom1.Read("ST19:0"))
    End Sub
End Module

Then you can call it when the form loads or if you press a button, similar to this:

Code: [Select]
    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadDataSave.Read()
    End Sub

Your variables are created within the module so they cannot be seen outside of it (maybe try declaring them as Public).

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Reading from a module, not a form
« Reply #5 on: January 26, 2017, 10:42:06 AM »
Will you be looking to have the read values displayed somewhere?

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Reading from a module, not a form
« Reply #6 on: January 26, 2017, 01:34:39 PM »
No, all read values are going to be saved in an access file, using a SQL query. Just data logging.