使用Excel通过OPC访问WinCC的实时数据

使用Excel通过OPC访问WinCC的实时数据时,数据时间和OPC时间不致,差8小时。但OPC时间与电脑时间一致。WINCC设置为本机时区

图片说明:

使用Excel通过OPC访问WinCC的实时数据   

最佳答案

”此为全局脚本中的时间转换代码
Function GetLocalDate(vtDate) ”得到当地时间,从格林尼治时间转换过来的
Dim DoY
Dim dso
Dim dwi
Dim strComputer, objWMIService, colItems, objItem
Dim TimeZone
Dim vtDateLocalDate
”—————————
”get time zone bias
”—————————
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_TimeZone")
For Each objItem In colItems
    TimeZone = objItem.Bias / 60    ”offset TimeZone In hours
Next
”—————————
”check parameter vtDate
”—————————
If IsDate(vtDate) <> True Then
  IS_GetLocalDate = False
  Exit Function
End If
”—————————
”get day of the year
”—————————
DoY = DatePart("y", vtDate)
dso = DatePart("y", "31.03") – DatePart("w", "31.03") + 1
dwi = DatePart("y", "31.10") – DatePart("w", "31.10") + 1
If DoY >= dso And DoY < dwi Then
  ”sommer
  TimeZone = TimeZone + 1  ”additional offset 1h in summer
End If
”—————————
”correction of date
”—————————
vtDateLocalDate = DateAdd("h", 1 * TimeZone, vtDate)
”—————————
”return UTC date and time
”—————————
GetLocalDate = vtDateLocalDate
End Function


直接拿去用吧  好使
如果电脑时间为北京时间,读回OPC变量的时间值后直接加8H即可

提问者对于答案的评价:
搞定

原创文章,作者:ximenziask,如若转载,请注明出处:https://www.zhaoplc.com/plc361856.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2021年7月5日 下午12:04
下一篇 2021年7月5日

相关推荐

发表回复

登录后才能评论