我正在用VB做项目。我已经附上了我的代码。我可以打印电流,电压。现在我想打印瓦数购买倍增电压和电流系数。但问题是它没有正确显示瓦数。它向我展示了一些其他的阅读。让我知道如何解决这个???
而且对于它下面的瞬时瓦特小时它没有显示时间。
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static counter As Integer = 0
Static average_sum As Double = 0
Static Avg_count As Integer = 0
Dim Power As Double
Static Cc_count As Long = 0
Static watt_hour As Double = 0
Dim Counter_write As Integer = 60
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim distance As String = SerialPort1.ReadLine()
Dim voltage As Double = 24
Dim PD As Double
k = CDbl(distance)
voltage_text.Text = 24
ListBoxSensor.Text = k
PD = k * voltage
Display.Text = CStr(PD)
Dim s As New Series
counter = counter + 1
'Count_val.Text = counter
Avg_count = Avg_count + 1
' drawing graph
If Avg_count = 1 Then
Power = (k * voltage) / 3600
watt_hour = watt_hour + Power
System.Threading.Thread.Sleep(250)
If watt_hour > 1000 Then
watt_hour = 1.0 + watt_hour
End If
Avg_count = Avg_count + 1
End If
If Avg_count > 1 Then
Avg_count = 0
End If
Dim current As Double = watt_hour
'Dim current As Double = k
Dim r As DataRow = dT.NewRow
r("time") = Now
'r("current") = k
r("current") = watt_hour
'add the row
dT.Rows.Add(r)
'remove any row older than 1 minute
Dim oldestTime As DateTime = Now.AddMinutes(-1)
Do While DirectCast(dT.Rows(0)("time"), DateTime) < oldestTime
dT.Rows.RemoveAt(0)
Loop
'finally bind the chart....
Chart1.DataBind()
'write into excel sheet for every 1 minute ..................
If Cc_count < 1 Then
Cc_count = Cc_count + 1
Dim array(10) As Double
For value As Double = 0 To Cc_count
array(Cc_count) = distance
average_sum = average_sum + array(Cc_count)
value = value + 1
'V_count.Text = value
' AVG_count_text.Text = Cc_count
If value > 1 Then
average_sum = average_sum / value
System.Threading.Thread.Sleep(250)
' AVG_CR1.Text = average_sum
Cc_count = 0
End If
Next
End If
If counter = 1 Then
counter = 0
Dim headerText = ""
'Dim headerText As New StringBuilder=
Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not IO.File.Exists((csvFile)) Then
'headerText = "Date,TIME ,current, "
headerText = "Date,TIME ,Watt-Hour,Current, "
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
'Dim date1 As String = TimeOfDay.Day + "-" + TimeOfDay.Month() + "-" + TimeOfDay.Year()
Dim date1 As String = "26-09-2014"
Dim time1 As String = TimeOfDay()
'Dim date1 As String = DateAndTime.DateString
' Dim current1 As String = CStr(distance)
Dim watt1 As String = CStr(watt_hour) + " "
' Dim current1 As String =CStr(distance)
Dim x As String = date1 + "," + time1 + "," + watt1 + "," + distance
' Dim x As String = time1 + "," + distance + "," + watt1
outFile.Write(x)
End Using
End If
Catch ex As Exception
' MsgBox("Error in Timer1_Tick: " & ex.Message)
End Try
并且还可以瞬间绘制图形。但是没有按预期进行。
Dim current As Double = watt_hour
'Dim current As Double = k
Dim r As DataRow = dT.NewRow
r("time") = Now
'r("current") = k
r("current") = watt_hour
'add the row
dT.Rows.Add(r)
'remove any row older than 1 minute
Dim oldestTime As DateTime = Now.AddMinutes(-1)
Do While DirectCast(dT.Rows(0)("time"), DateTime) < oldestTime
dT.Rows.RemoveAt(0)
Loop
'finally bind the chart....
Chart1.DataBind()