Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.Data
Imports System.Linq
Public Class PopupForm
Dim Ports As New List(Of IO.Ports.SerialPort)
Private WithEvents serPort As New IO.Ports.SerialPort
Private data As String = ""
Private table As New DataTable
Private DateStart As DateTime
Public Sub New()
InitializeComponent()
Icon = My.Resources.TrayIcon
End Sub
Private Sub CloseAppButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Visible = False
End Sub
Private Sub serPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serPort.DataReceived
'Wait for the buffer to fill up....
Dim Writer As System.IO.StreamWriter
Dim StrPath As String = TextBox4.Text
Dim Heading As String
Dim Word As String
Dim curvalue As Decimal = 0
Dim prevvalue As Decimal = 0
Dim watts(20) As Decimal
Dim i As Integer = 0
Dim result As Decimal = 0.0
Try
System.Threading.Thread.Sleep(250)
data = serPort.ReadExisting()
Dim words As String() = data.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
For Each Word In words
Dim Wordstring() As String = Split(Word, ",")
If Wordstring.Length = 5 Then
If Not Decimal.TryParse(Wordstring(2), result) Then
Wordstring(2) = 0
End If
If Not Decimal.TryParse(Wordstring(3), result) Then
Wordstring(3) = 0
End If
If Not Decimal.TryParse(Wordstring(4), result) Then
Wordstring(4) = 0
End If
Wordstring(3) = Wordstring(3) * CDec(TextBox6.Text)
Wordstring(4) = Wordstring(4) * CDec(TextBox5.Text)
table.Rows.Add(Wordstring(0), Wordstring(1), Math.Round(CDec(Wordstring(2)), 1), Math.Round(CDec(Wordstring(3)), 1), Math.Round(CDec(Wordstring(4)), 2), Math.Round(CDec(Wordstring(2)), 1) * Math.Round(CDec(Wordstring(3)), 1) * Math.Round(CDec(Wordstring(4)), 1))
End If
Next
If DateDiff(DateInterval.Minute, DateStart, DateTime.Now) >= 1 Then
StrPath = TextBox4.Text & "_MIN.CSV"
If File.Exists(StrPath) Then
Writer = File.AppendText(StrPath)
Else
Writer = File.CreateText(StrPath)
Heading = "ted_timestamp, ted_clientname, ted_gwid,ted_id, ted_device, ted_KW, ted_voltage, ted_amp, ted_pf,ted_cost" & vbCrLf
Writer.Write(Heading)
End If
Dim queryFirst = From r In table Group By key = r.Field(Of String)("ted_device") Into Group
For Each grp In queryFirst
Dim Grptbl As New DataTable
Grptbl = grp.Group.CopyToDataTable()
For Each Grprow As DataRow In Grptbl.Rows
curvalue = Grprow("ted_watts")
If prevvalue <> 0 Then
watts(i) = watts(i) + (prevvalue + curvalue) / 2
Else
watts(i) = 0
End If
prevvalue = curvalue
Next Grprow
watts(i) = (watts(i) / Grptbl.Rows.Count) * (Grptbl.Rows.Count - 1)
i = i + 1
Next
Dim querysecond = From row In table
Group row By ted_device = row.Field(Of String)("ted_device") Into tedGroup = Group
Select New With {
Key ted_device,
.id = tedGroup.Min(Function(r) r.Field(Of String)("ted_id")),
.voltage = tedGroup.Average(Function(r) r.Field(Of Decimal)("ted_voltage")),
.amp = tedGroup.Average(Function(r) r.Field(Of Decimal)("ted_amp")),
.pf = tedGroup.Average(Function(r) r.Field(Of Decimal)("ted_pf"))
}
i = 0
For Each x In querysecond
Word = Format(Date.Now, "MM/dd/yyyy HH:mm") & ", " & TextBox1.Text & ", " & TextBox2.Text & " , " & x.id & " , " & x.ted_device & ", " & Math.Round((watts(i) / 60000), 2) & ", " & Math.Round(x.voltage, 2) & ", " & IIf(x.amp <= 0.5, 0, Math.Round(x.amp, 2)) & ", " & Math.Round(x.pf, 2) & " , " & TextBox3.Text & vbCrLf
Writer.Write(Word)
i = i + 1
Next
Writer.Flush()
Writer.Close()
DateStart = DateTime.Now
table.Clear()
End If
Catch ex As Exception
' Show the exception's message.
MessageBox.Show(ex.Message)
' Show the stack trace, which is a list of methods
' that are currently executing.
MessageBox.Show("Stack Trace: " & vbCrLf & ex.StackTrace)
Finally
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ports As String() = SerialPort.GetPortNames()
Dim port As String
Button2.Enabled = False
If serPort.IsOpen Then
serPort.Close()
End If
Try
' Create four typed columns in the DataTable.
table.Columns.Add("ted_id", GetType(String))
table.Columns.Add("ted_device", GetType(String))
table.Columns.Add("ted_voltage", GetType(Decimal))
table.Columns.Add("ted_amp", GetType(Decimal))
table.Columns.Add("ted_pf", GetType(Decimal))
table.Columns.Add("ted_watts", GetType(Decimal))
For Each port In ports
With serPort
.PortName = port
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.ReceivedBytesThreshold = 2048
.RtsEnable = True
End With
DateStart = DateTime.Now
serPort.Open()
If serPort.BytesToRead < 1 Then serPort.Close()
Next port
Catch ex As Exception
Console.Write(ex)
End Try
Console.ReadLine()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.DialogResult = Windows.Forms.DialogResult.Abort
Me.Close()
End Sub
End Class