Author Topic: Read values from plc, do calculation, log  (Read 1439 times)

g.mccormick

  • Newbie
  • *
  • Posts: 29
    • View Profile
Read values from plc, do calculation, log
« on: July 23, 2020, 08:42:26 AM »
I need to do the following, read some vaues from a compactlogix, do some calculations, log the read values and the calculated results.   Should I use datasubscriber? 

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5325
    • View Profile
    • AdvancedHMI
Re: Read values from plc, do calculation, log
« Reply #1 on: July 23, 2020, 08:50:47 AM »
The DataSubscriber would probably be the easiest way. In the DataReturned event handler you can do the calculations then write it to a file

g.mccormick

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Read values from plc, do calculation, log
« Reply #2 on: July 23, 2020, 02:31:28 PM »
So I have the data subscriber working and calculations running.  I am viewing it all on the screen.  I am trying to log to a file at a timer interval.  I kinda sorta got it, but I can't figure out how to:
1. Create a new file everytime the application is started. Just enw filename.
2. On the first pass on this new file, log the header.
3. Log data until the form is closed.
4. Gracefully close the file.

This is all on a Page2 , not on the main.  THat maybe an issue I guess.  I created a timer and am trying to write the data at the timer interval.

 Dim FILE_NAME As String = "C:\Users\MYPERSON\Desktop\IMP\Data\CH1Data.txt"
    Dim LineOfData As String
    Dim CondRTstr As String
    Dim TimeStamp As String
    Dim Firstpass As Boolean = True
    Dim HeaderData As String
    'Opens the file I think
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME)

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick




        If Firstpass Then
            'If this is the first time through, then write the header
            HeaderData = "Time,Cond Rtn T [F], Cond Sup T [F], Cond Delta T [DT_F], Cond Flow [GPM], Cond Meter Flow [GPM], Cond Mass Flow [LB/HR], Cond Heat [BTU/HR], Cond Tons [RT], _
                            Evap Rtn T [F], Evap Sup T [F], Evap Delta T [DT_F], Evap Flow [GPM], Evap Mass Flow [LB/HR], Evap Heat Rej [BTU/HR], Evap Tons [RT]"
            Firstpass = False
            objWriter.WriteLine(HeaderData)

        End If
        TimeStamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
        CondRTstr = CondRtnT
        LineOfData = TimeStamp _
            & "," _
            & CondRtnT _
            & "," _
            & CondSupT _
            & "," _
            & CondDT _
            & "," _
            & CondFlo _
            & "," _
            & CondFloComms _
            & "," _
            & WaterMF _
            & "," _
            & CondBTU_HR _
            & "," _
            & Cond_TONS _
            & "," _
            & EvapRtnT _
            & "," _
            & EvapSupT _
            & "," _
            & EvapDT _
            & "," _
            & EvapFlo _
            & "," _
            & GlycolMF _
            & "," _
            & EvapBTU_HR _
            & "," _
            & Evap_TONS

        objWriter.WriteLine(LineOfData)



    End Sub



Godra

  • Hero Member
  • *****
  • Posts: 1449
    • View Profile
Re: Read values from plc, do calculation, log
« Reply #3 on: July 23, 2020, 11:21:52 PM »
Maybe take a look at the code of the attached BasicDataLogger2G file (which is available in the latest 3.99y beta version).

It does most of the stuff you are trying to do, besides for the calculations.

If you can do all these calculations in the PLC then you could use BasicDataLogger2G to directly poll all those values and log them.

« Last Edit: July 23, 2020, 11:30:04 PM by Godra »

g.mccormick

  • Newbie
  • *
  • Posts: 29
    • View Profile
Re: Read values from plc, do calculation, log
« Reply #4 on: July 24, 2020, 12:24:03 PM »
I am now trying to change to the put the code in the main form.  Since the main form is where exiting of the appliction is and that seems to make more sense for logging. I want to view the current values on the form2, but I cannot figure out how to get the values there.  I have specified the variables on the main form to be PUBLIC.  ?