如何从Windows服务到WEB应用程序获取数据?

时间:2017-06-20 05:59:43

标签: vb.net web-applications serial-port windows-services

我与称重机交互的Windows服务并将结果输入到txt文件中。而不是我希望将这些数据接收到我的Web应用程序中。我的服务代码如下。

Protected Overrides Sub OnStart(ByVal args() As String)
    ' Add code here to start your service. This method should set things
    ' in motion so your service can do its work.
    lg.WriteToLog("------------Service Started!-----------------")
    Try
        myPort.PortName = "COM2"
        myPort.BaudRate = 9600
        myPort.Parity = Parity.None
        myPort.StopBits = System.IO.Ports.StopBits.One
        myPort.DataBits = 8
        AddHandler myPort.DataReceived, AddressOf DataReceived
        myPort.Open()
    Catch ex As Exception
        lg.WriteToLog("Error Occurred while Initializing Serial Port !")
        lg.WriteToLog(ex.ToString)
    End Try
End Sub
Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
    Try
        If myPort.IsOpen Then
            ReceivedText(myPort.ReadExisting())
            lg.WriteToLog("Port is open and getting the data!")
        End If
    Catch ex As Exception
        lg.WriteToLog("Error Occurred while reading data!")
        lg.WriteToLog(ex.ToString)
    End Try    
End Sub Private Sub ReceivedText(ByVal [text] As String)
    lg.WriteToLog(output([text]))
End Sub`

任何人都可以建议我如何将相同的数据发送回WebApplication,因为我对Web应用程序更新鲜,我已经尝试使用谷歌搜索,但没有找到任何有用的答案。这里输出只是一个简单的函数,它以正确的格式执行权重转换,因此你可以忽略那个!

1 个答案:

答案 0 :(得分:0)

您可以创建Web API并将数据发送到该网络API,然后您可以使用它。

更新:检查此How to make HTTP request in windows service?

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client