我试图在通用Windows平台应用中设置套接字侦听器。该应用程序将在Raspberry PI上运行,并从动态范围内收听通信。
我在mainVM文件中设置了StreamSocketListener。我有命令启动和停止它。我已经按照我可以找到的在线指南进行了跟踪,但是当比例发送数据时,它不会触发事件。我已经使用我下载的监听应用程序进行了测试,因此我可以确认正在发送消息并且我的计算机能够接收它们(没有防火墙问题)。 (我也知道只有一个应用程序可以监听端口,因此在测试我的程序时,此测试程序已关闭。)
首次启动连接时,我会在处理程序函数上获得一次命中,但我再也没有得到任何东西了。没有错误,只是没有发生任何事情。
命令:
Public Property cmdStart As New Command(Async Sub() Await StartListener(), True)
Public Property cmdStop As New Command(Sub() StopListener(), True)
命令子程序:
Private Async Function StopListener() As Task
If Connected Then
Await _prePriceListener.CancelIOAsync()
RemoveHandler _prePriceListener.ConnectionReceived, Nothing
_prePriceListener.Dispose()
Connected = False
End If
End Function
Private Async Function StartListener() As Task
If ValidateInput() Then
Try
_prePriceListener = New StreamSocketListener()
_prePriceListener.Control.KeepAlive = False
AddHandler _prePriceListener.ConnectionReceived, AddressOf HandlerReceived
Await _prePriceListener.BindServiceNameAsync("6021")
Connected = True
Catch ex As Exception
Message = ex.Message
End Try
End If
End Function
事件处理程序:
Private Async Function HandlerReceived(sender As StreamSocketListener, args As StreamSocketListenerConnectionReceivedEventArgs) As Task
Dim msgReceived As String = ""
Dim inStream As Stream
Dim reader As StreamReader
Try
inStream = args.Socket.InputStream.AsStreamForRead()
reader = New StreamReader(inStream)
Catch ex As Exception
Return
End Try
Try
If reader IsNot Nothing Then
msgReceived = Await reader.ReadLineAsync()
HandlePrepriceRead(msgReceived)
End If
Catch ex As Exception
Return
End Try
End Function
我已尝试将_prePriceListener.Control.KeepAlive设置为true和false。如果为true,则挂起在处理程序中的msgReceived = Await reader.ReadLineAsync()上。我怀疑这是问题,但我发现的每一个修复都没有区别。我唯一的另一个怀疑是不正确使用Async / Await但我无法找到很多信息来确认或否认。 (我想我知道我在做什么,但我可能处于那种了解危险的程度......)
无论如何,任何输入都将非常感激。感谢。
答案 0 :(得分:0)
您确定音阶是否正在发送EOL(\ n或\ r或\ r \ n或\ r \ n)HEX 13和/或10?我可以看到你的代码正在等待规模永远不会发送的东西。 *仅供参考我不确定您使用的是什么样的规模,或者它的谈话速度或应用程序的速度有多快但是如果规模是连续饲料的话,这可能不适合您作为规模和沿途的所有部件都会缓冲物品,并且在您处理此事件时(稍等一段时间后)重量将会消失。