如何从重量以太网秤读取重量?

时间:2019-05-16 14:20:04

标签: vba access-vba

我有一个体重秤,它是Giropes GI / 400,已连接到我的LAN,并且可以使用Internet浏览器通过HTTP连接。就像一台TCP服务器一样,它始终发送权重,并通过URL“ http://someip:3000”在浏览器中显示它。

我想读取这些值并写入SQL表,但不知道如何。我正在使用一个访问项目和VBA。我一直在互联网上寻找示例,但只能通过通讯端口或USB适配器找到。拜托,你能帮我吗?

1 个答案:

答案 0 :(得分:0)

如果页面很简单,并且您可以轻松地分析显示的重量,则可以使用如下函数将整个页面下载到要解析的字符串中:

Option Compare Database
Option Explicit

' API declarations.
'
Private Declare Function URLDownloadToFile Lib "Urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long

' Download a file or a page with public access from the web. 
' Returns 0 if success, error code if not. 
' 
' If parameter NoOverwrite is True, no download will be attempted 
' if an existing local file exists, thus this will not be overwritten. 
' 
' Examples: 
' 
' Download a file: 
'   Url = "https://www.codeproject.com/script/Membership/ProfileImages/%7Ba82bcf77-ba9f-4ec3-bbb3-1d9ce15cae23%7D.jpg" 
'   FileName = "C:\Test\CodeProjectProfile.jpg" 
'   Result = DownloadFile(Url, FileName) 
' 
' Download a page: 
'   Url = "https://www.codeproject.com/Tips/1022704/Rounding-Values-Up-Down-By-Or-To-Significant-Figur?display=Print" 
'   FileName = "C:\Test\CodeProject1022704.html" 
'   Result = DownloadFile(Url, FileName) 
' 
' Error codes: 
' -2146697210   "file not found". 
' -2146697211   "domain not found". 
' -1            "local file could not be created." 
' 
' 2004-12-17. Gustav Brock, Cactus Data ApS, CPH. 
' 2017-05-25. Gustav Brock, Cactus Data ApS, CPH. Added check for local file. 
' 2017-06-05. Gustav Brock, Cactus Data ApS, CPH. Added option to no overwrite the local file. 
' 
Public Function DownloadFile( _ 
    ByVal Url As String, _ 
    ByVal LocalFileName As String, _ 
    Optional ByVal NoOverwrite As Boolean) _ 
    As Long 

    Const BindFDefault  As Long = 0 
    Const ErrorNone     As Long = 0 
    Const ErrorNotFound As Long = -1

    Dim Result  As Long

    If NoOverwrite = True Then 
        ' Page or file should not be overwritten. 
        ' Check that the local file exists. 
        If Dir(LocalFileName, vbNormal) <> "" Then 
            ' File exists. Don't proceed. 
            Exit Function 
        End If 
    End If     

    ' Download file or page. 
    ' Return success or error code. 
    Result = URLDownloadToFile(0, Url & vbNullChar, LocalFileName & vbNullChar, BindFDefault, 0)   

    If Result = ErrorNone Then 
        ' Page or file was retrieved. 
        ' Check that the local file exists. 
        If Dir(LocalFileName, vbNormal) = "" Then 
            Result = ErrorNotFound 
        End If 
    End If   

    DownloadFile = Result 

End Function

它在我的文章中有详细描述:

Show pictures directly from URLs in Access forms and reports

其中还有一个演示可供下载。

完整代码也位于 GitHub VBA.PictureUrl