第一次尝试使用UDP。我在UDP广播中从NMEA字符串中获取lat / long。下面的代码在ISE中工作正常,但在控制台中挂起。我同时使用管理员权限运行。关于该主题的其他帖子建议使用dot-sourcing,没有区别。我怀疑这与它如何处理变量有关,但已经碰壁了。在此先感谢您的帮助。
##########################################################################
#
# Read $GPGGA string from UDP broadcast on port 40181
#
# Convert Degree-Minutes-Seconds format to decimal lat/long
#
# UDP code harvested from http://winpowershell.blogspot.com/2010/01/powershell-udp-clientserver.html
#
#
##########################################################################
# Form a connection point
$endpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
Write-Host "Endpoint: " $endpoint
# Open UDP client
$udpclient = new-Object system.Net.Sockets.Udpclient(40181)
Write-Host "UDP client: " $udpclient
# Grab the datagram
$content = $udpclient.Receive([ref]$endpoint)
Write-Host "Content: " $content
# Put the datagram in $udpstring as type string
$udpstring = [Text.Encoding]::ASCII.GetString($content)
Write-Host "UDP string: " $udpstring
# Close UDP client
$udpclient.Close()
# Get Degree Min Sec format latitude from UDP string
# Explicit typing required in order to do math later
[Double]$DMSlatdeg = $udpstring.Substring(17,2)
$DMSlatmin = $udpstring.Substring(19,2)
# Get North South directional from UDP string
$directNS = $udpstring.Substring(29,1)
# Get Degree Min Sec format longitude from UDP string
# Explicit typing required in order to do math later
[Double]$DMSlongdeg = $udpstring.Substring(31,3)
$DMSlongmin = $udpstring.Substring(34,2)
# Get East West directional from UDP string
$directEW = $udpstring.Substring(44,1)
# Add decimal latitude minutes value to degrees
$declat = ($DMSlatdeg + ($DMSlatmin/60))
# Add decimal longitude minutes value to degrees
$declong = ($DMSlongdeg + ($DMSlongmin/60))
# Output formatted to three decimal places - variable value not changed
Write-Host "Decimal lat: " $declat.ToString("#.###") $directNS
Write-Host "Decimal long: " $declong.ToString("#.###") $directEW
在ISE中我得到了正确的输出:
端点:0.0.0.0:0
UDP客户端:System.Net.Sockets.UdpClient
内容:36 71 80 71 71 65 44 [等]
UDP字符串:$ GPGGA,160516.13,[etc]
十进制纬度:[正确值] N
十进制长:[正确值] W
在控制台中,它会输出前两个语句并挂起:
端点:0.0.0.0:0
UDP客户端:System.Net.Sockets.UdpClient
我设置$ content的方式有问题吗?