Author Topic: Append to a CSV file  (Read 1636 times)

Astabi

  • Newbie
  • *
  • Posts: 13
    • View Profile
Append to a CSV file
« on: April 01, 2016, 03:31:43 PM »
Hello, I was wondering if I could get a bit of help. I don't do much programming and I think that's why I'm having problems. My goal is this:

I need to monitor a PLC, when a data change is detected I need to read 5 values from the PLC and the write them to a CSV file. I need to append the time,  and the ST20:19, ST20:0 values to the file each minute and when a value changes. When the run is completed, the file will be closed and it should wait for the next run.

The way I have it now, a new file is created every minute, and the PLC seems to be being polled each second.

Here is my code:
Code: [Select]
    Private Sub DataSubscriber2_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber2.DataChanged

        Dim strFilename As String = Format(Now, "yyyyMMddhhmm") & ".csv"
        Dim strDateStamp As String = Format(Now, "MM/dd/yyyy")
        Dim strTimeStamp As String = Format(Now, "hh:mm tt")
        Dim path As String = String.Concat("C:\temp\", strFilename)
        Dim w As New IO.StreamWriter(path, True)

        w.Write(strDateStamp)
        w.Write(",")

        If e.PlcAddress = "ST20:1" Then
            w.Write(e.Values(0))
            w.Write(",")
        End If
        If e.PlcAddress = "ST20:2" Then
            w.Write(e.Values(0))
            w.Write(",")
        End If
        If e.PlcAddress = "ST20:18" Then
            w.Write(e.Values(0))
            w.Write(",")
        End If

        If Timer1.Interval = 60000 Then           
            If e.PlcAddress = "ST20:19" Then
                w.Write(e.Values(0))
                w.Write(",")
            Else
                w.Write(" ")
            End If
            If e.PlcAddress = "ST20:0" Then
                w.Write(strTimeStamp & ",")
                w.Write(e.Values(0))
            End If
            w.WriteLine(vbNewLine)
        End If
        w.Close()
    End Sub

Here is an example of the output (First CSV file):
Code: [Select]
04/01/2016, 03:09 PM,00:00:00         104.7C         0.2PSI

04/01/2016,CYCLE NAME: LIQUIDS 2,

04/01/2016,CYCLE TEMP: 121.0C,

04/01/2016,CYCLE TIME: 12 MIN,

04/01/2016,CYCLE ABORTED,

followed by (next CSV file):
Code: [Select]
04/01/2016, 03:10 PM,00:00:00          71.2C         0.6PSI

04/01/2016, ,

04/01/2016, 03:10 PM,00:00:00          71.4C         0.6PSI

04/01/2016, 03:10 PM,00:00:00          71.7C         0.6PSI

04/01/2016,CONDITIONING,

04/01/2016, 03:10 PM,00:00:00          71.9C         2.1PSI

04/01/2016, 03:10 PM,00:00:00          73.4C         4.5PSI

04/01/2016, 03:10 PM,00:00:00          79.7C         4.8PSI

04/01/2016, 03:10 PM,00:00:00          88.2C         4.8PSI

04/01/2016, 03:10 PM,00:00:00          93.8C         4.7PSI

04/01/2016, 03:10 PM,00:00:00          97.2C         4.6PSI

04/01/2016, 03:10 PM,00:00:00          99.5C         4.5PSI

04/01/2016, 03:10 PM,00:00:00         101.0C         4.2PSI

04/01/2016, 03:10 PM,00:00:00         101.9C         4.2PSI

04/01/2016, 03:10 PM,00:00:00         102.7C         4.1PSI

04/01/2016, 03:10 PM,00:00:00         103.3C         4.2PSI

04/01/2016, 03:10 PM,00:00:00         103.8C         4.2PSI

04/01/2016, 03:10 PM,00:00:00         104.2C         4.2PSI

04/01/2016, 03:10 PM,00:00:00         104.4C         4.1PSI

04/01/2016, 03:10 PM,00:00:00         104.6C         4.1PSI

I really only want the date, ST20:1, ST20:2 and ST2018: on the head of the first pass and to loop through ST:2019 and ST20:0, in one CSV file.
Then close and get ready for the next run.

Thank You,
Alan

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Append to a CSV file
« Reply #1 on: April 01, 2016, 05:10:46 PM »
The order in which values come back from a DataSubcriber2 is not predictable, so you have to take that in consideration.

Here is a little bit of code that may point you in the right direction:
Code: [Select]
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Filename As String = Format(Now, "yyyyMMddhhmm") & ".csv"
        Dim DateStamp As String = Format(Now, "MM/dd/yyyy")
        Dim TimeStamp As String = Format(Now, "hh:mm tt")
        Dim path As String = String.Concat("C:\temp\", Filename)


        If Not System.IO.File.Exists(path) Then
            createDataFile(path)
        End If

 If Timer1.Interval = 60000 Then           
            If e.PlcAddress = "ST20:19" Then
                w.Write(e.Values(0))
                w.Write(",")
            Else
                w.Write(" ")
            End If
            If e.PlcAddress = "ST20:0" Then
                w.Write(strTimeStamp & ",")
                w.Write(e.Values(0))
            End If
            w.WriteLine(vbNewLine)
        End If
    End Sub

    Private Sub createDataFile(ByVal filename As String)
        Using sw As New System.IO.StreamWriter(filename)
            Dim St20 As String = EthernetIPforSLCMicroCom1.Read("ST20")
            sw.WriteLine(Now, St20)
        End Using
    End Sub

Astabi

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Append to a CSV file
« Reply #2 on: April 04, 2016, 11:48:06 AM »
Thank you for the reply,
I have tried something similar but this is precisely where my problem has been and I haven't been able to figure it out. I'm sorry if I'm missing something but with the code you gave me I now don't have 'w' declared, along with most of the Timer section, and am not sure where to do so.

You said:
Quote
The order in which values come back from a DataSubcriber2 is not predictable, so you have to take that in consideration.


Just to clarify, If I ask for ST20:19 and then ST20:0 at say, 3:10 PM and then ST20:19 and then ST20:0 at 3:11 PM this would work right? With the other values I just need them included in that run, I can place them where I want in a report later.

Again, thank you very much for your help!

Alan
« Last Edit: April 04, 2016, 01:31:28 PM by Astabi »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Append to a CSV file
« Reply #3 on: April 04, 2016, 02:50:12 PM »

Quote
The order in which values come back from a DataSubcriber2 is not predictable, so you have to take that in consideration.


Just to clarify, If I ask for ST20:19 and then ST20:0 at say, 3:10 PM and then ST20:19 and then ST20:0 at 3:11 PM this would work right? With the other values I just need them included in that run, I can place them where I want in a report later.
Let's say you define ST20:19 and ST20:0 in the DataSubscriber2. When the data is returned, the event will fire 2 times. Most likely the first time with ST20:0 and then with the value for ST20:19 next, but that is no guarantee.