使用Visual Studio Community 2017和AdvancedHMI创建基于PC的HMI应用程序。我有几个没有问题的跑步,所以转向新事物。在每一行上,我都有7个数组(每个数组500个实数),并希望每天捕获此数据并保存为Excel。使用Epplus和Advanced Hmi,我有以下代码。
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
Console.WriteLine("About to read the data")
Dim MyValues() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
Console.WriteLine(MyValues.Length & "elements read.")
'* Transfer the values to Excel
Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New System.IO.FileInfo("c:Data.xlsx"))
For I = 0 To MyValues.Length - 1
Console.WriteLine("Element " & I & "=" & MyValues(I))
ExcelPackage.Workbook.Worksheets(1).Cells(1, I + 1).Value = MyValues(I)
Next
End Using
End If
End Sub
运行此操作并触发我的标签谷来执行DataScriber,我将获得以下内容。
即将读取数据
500元素阅读。
元素0 = 87.945
异常抛出:'system.indexoutofrangeException'epplus.dll
一切看起来都应该起作用,但是我对VB或任何类型的编码都是非常新的。我的长处是PLC梯子逻辑。
谢谢。
这是我工作的。DataScriber在PLC中具有触发标签。触发后,它将收集数组数据,所有500个元素。创建一个新的Excel文件并将数据写入A列,单元1-500
Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
If e.ErrorId = 0 AndAlso e.Values.Count > 0 AndAlso e.Values(0) = "True" Then
Console.WriteLine("About to read the data")
Dim MyValues() As String = EthernetIPforCLXCom1.Read("VCell_1A_FES_Cycle_Average[0]", 500)
Console.WriteLine(MyValues.Length & "elements read.")
Transfer the values to Excel
Using ExcelPackage As New OfficeOpenXml.ExcelPackage(New IO.FileInfo("C:Data.xlsx"))
ExcelPackage.Workbook.Worksheets.Add("test")
For Index = 0 To MyValues.Length - 1
Console.WriteLine("Element " & Index & "=" & MyValues(Index))
Dim ws As OfficeOpenXml.ExcelWorksheet = ExcelPackage.Workbook.Worksheets(1)
Console.WriteLine("Worksheet OK")
Dim CellNum As String = "A" & (Index + 1)
ws.Cells(CellNum).Value = MyValues(Index)
Next
ExcelPackage.Save()
End Using
End If
End Sub