好,知道了。感谢 dmarruco 对 REST 的建议,它启发了我对 xml 使用类似的解决方案。客户端如下:
Imports System.IO
Imports System.IO.Ports
Imports System.Text
Imports System.Xml
Imports System.Web
Imports System.Net
Module Module1
Sub Main()
Dim myport As SerialPort = New SerialPort("COM3", "4800", Parity.None, 8, StopBits.One)
Dim availableSerialPorts As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.Ports.SerialPortNames
If availableSerialPorts.Contains("COM3") Then
Dim dstring As String = ""
Try
If myport.IsOpen Then
myport.Close()
End If
myport.Open()
System.Threading.Thread.Sleep(1500)
Dim datastring As String = myport.ReadExisting
dstring += datastring
myport.Close()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If myport IsNot Nothing Then
myport.Close()
End If
End Try
Dim loc As Integer
Dim search As String = "$GPGGA"
loc = InStr(1, dstring, search, CompareMethod.Text)
Dim latstart As Integer = loc + 18
Dim lonstart As Integer = loc + 30
Dim lat As String = Mid(dstring, latstart, 9)
Dim lon As String = Mid(dstring, lonstart, 10)
Dim gpgga As String = Mid(dstring, loc, 50)
If Left(lat, 1) <> "," And Left(lon, 1) <> "?" Then
Dim HH As String = Left(lat, 2)
Dim mmmm As String = Mid(lat, 3, 7)
Dim mconv As String = Val(mmmm) / 60
mconv = mconv.Replace("0.", "")
Dim latcoord As String = HH & "." & mconv
Dim lH As String = Left(lon, 3)
Dim lmm As String = Mid(lon, 4, 7)
Dim lmC As String = Val(lmm) / 60
lmC = lmC.Replace("0.", "")
Dim loncoord As String = "-" & lH & "." & lmC
Dim xmldoc As New XmlDocument
Dim xmldec = xmldoc.CreateXmlDeclaration("1.0", "ASCII", "")
Dim outx As XmlElement
outx = xmldoc.CreateElement("Location")
xmldoc.AppendChild(outx)
Dim root As XmlElement = xmldoc.DocumentElement
xmldoc.InsertBefore(xmldec, root)
Dim xmlat As XmlElement
xmlat = xmldoc.CreateElement("lat")
xmlat.InnerText = latcoord
outx.AppendChild(xmlat)
Dim xmlon As XmlElement
xmlon = xmldoc.CreateElement("lon")
xmlon.InnerText = loncoord
outx.AppendChild(xmlon)
Dim machineid As String = "1" 'arbitrary for testing purposes
Dim xmach As XmlElement
xmach = xmldoc.CreateElement("MID")
xmach.InnerText = machineid
outx.AppendChild(xmach)
Dim url As String = "http://fakeurl.com/getxml.aspx" 'the receiving page goes here
Dim wreq As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
Dim encoding As New ASCIIEncoding()
Dim bytestowrite As Byte() = encoding.GetBytes(xmldoc.ToString)
wreq.Method = "POST"
wreq.ContentLength = bytestowrite.Length
wreq.ContentType = "text/xml"
Dim mystream As Stream = wreq.GetRequestStream()
mystream.Write(bytestowrite, 0, bytestowrite.Length)
mystream.Close()
Else
MsgBox("No Fix")
End If
Else
MsgBox("Disconnected")
End If
End Sub
End Module
原谅 try/catch 这是我发布之前的测试代码。