Instantly share code, notes, and snippets.
Created
December 10, 2020 07:15
-
Save dj1711572002/c5c063351db05f4892ed7b33d04466b5 to your computer and use it in GitHub Desktop.
RTK_Monitor_NAV-PV_RELPOSNED Binary Recieving VB.NET DGV In debugging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Serial Port Recieve Sample Program | |
'Form1 Object must create ,button1,2 & textbox 1,2 | |
Imports System.IO.Ports | |
Public Class Form1 | |
'Dim bdat As Byte() | |
Dim sdat As String() | |
Dim i, j, k, l As Integer | |
Dim NVC As Integer = 0 'NAV data Counter | |
Dim NVi As Integer = 0 | |
Dim PVTflag As Integer = 0 | |
Dim RELPflag As Integer = 0 | |
Dim startIndex As Integer | |
Dim strPVT As String | |
Dim sbPVT As String() | |
Dim bPVT As Byte(,) | |
Dim strRELP As String | |
Dim sbRELP As String() | |
Dim bRELP As Byte(,) | |
Dim sw As New System.Diagnostics.Stopwatch() | |
'================PVT Paramers============================ | |
Dim parPVT As Integer() | |
Dim parRELP As intger() | |
Delegate Sub DataDelegate(ByVal sdata As String) | |
'========================================================================================================================================== | |
Private Sub PrintData(ByVal sdata As String) | |
k = k + 1 | |
'Debug.Print("slen=" & CStr(sdata.Length) & "sdata(" & CStr(k) & ")=" & sdata) | |
startIndex = sdata.IndexOf("B5-62-01-07") | |
If startIndex >= 0 And sdata.Length >= 299 Then | |
sw.Stop() | |
Debug.Print("--------------PVT_time=" & sw.ElapsedMilliseconds) | |
sw.Start() | |
strPVT = sdata.Substring(startIndex, 299) 'PVT 100bytex3=300 最後のデリミタが無いから299 | |
sbPVT = strPVT.Split("-") | |
' Debug.Print("PVT:start=" & CStr(startIndex) & "len=" & CStr(sdata.Length) & "strPVT=" & strPVT) | |
PVTflag = 1 | |
End If | |
startIndex = sdata.IndexOf("B5-62-01-3C") | |
If startIndex >= 0 And sdata.Length >= 215 Then | |
sw.Stop() | |
Debug.Print("--------------RELPOSNED_time=" & sw.ElapsedMilliseconds) | |
sw.Start() | |
strRELP = sdata.Substring(startIndex, 215) ' RELPOSNED 72bytex3=216 最後のデリミタが無いから215 | |
sbRELP = strRELP.Split("-") | |
' Debug.Print("RELPOSNED:start=" & CStr(startIndex) & "len=" & CStr(sdata.Length) & "strRELP=" & strRELP) | |
RELPflag = 1 | |
End If | |
If PVTflag = 1 And RELPflag = 1 Then | |
NVC += 1 'NAV data set Counter | |
Debug.Print("NVC=" & CStr(NVC)) | |
'--------PVTとRELPOSNED16進数string配列を2次元Byte配列に変換-------------------------------------------- | |
For i = 0 To 99 | |
bPVT(NVC, i) = Convert.ToByte(sbPVT(i), 16) | |
' Debug.Print("bPVT(" & CStr(NVC) & "," & CStr(i) & ")=" & CStr(bPVT(NVC, i)) & ":" & sbPVT(i)) | |
Next | |
For i = 0 To 71 | |
bRELP(NVC, i) = Convert.ToByte(sbRELP(i), 16) | |
' Debug.Print("bRELP(" & CStr(NVC) & "," & CStr(i) & ")=" & CStr(bRELP(NVC, i)) & ":" & sbRELP(i)) | |
Next | |
'------------------------------------------------------------------------------------------------------- | |
'=====================データ表示================ | |
'------dgv1 にバイナリーPVT RELPOSNEDを1行にまとめて表示----------------------------------------------------------------------------------- | |
' '---dgv1 PVT----------------------------------- | |
For i = 0 To 99 | |
dgv1.Rows(NVC).Cells(i).Value = sbPVT(i) | |
Next i | |
For i = 0 To 71 | |
dgv1.Rows(NVC).Cells(100 + i).Value = sbRELP(i) | |
Next i | |
'-------------------------------------------------------------------------------------------------------------------------------------------- | |
PVT_Trans(NVC) ' PVT Binary を実数データに変換 parPVT(1-33) as integer | |
PVTflag = 0 | |
RELPflag = 0 | |
End If | |
If CheckBox1.Checked = True Then | |
RichTextBox1.AppendText(sdata & vbCrLf) | |
End If | |
End Sub | |
'============================================================================================================================================= | |
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click | |
SerialPort1.PortName = "COM18" ' TextBox1.Text 'オープンするポート名を格納 | |
SerialPort1.Open() 'ポートオープン | |
End Sub | |
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click | |
If SerialPort1.IsOpen = True Then 'ポートオープン済み | |
SerialPort1.Close() 'ポートクローズ | |
End If | |
End Sub | |
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived | |
Dim ReceivedData As String = "" '受信データ用変数を宣言します | |
Try | |
Dim arrByte As Byte() = New Byte(SerialPort1.BytesToRead - 1) {} | |
SerialPort1.Read(arrByte, 0, arrByte.GetLength(0)) 'Binary Receive | |
ReceivedData = BitConverter.ToString(arrByte) ' & vbCrLf | |
Catch ex As Exception | |
ReceivedData = ex.Message '例外処理を行います | |
End Try | |
'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します | |
Dim adre As New DataDelegate(AddressOf PrintData) | |
Me.Invoke(adre, ReceivedData) | |
End Sub | |
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated | |
'============DIM initialize=============================== | |
ReDim sbPVT(100) | |
ReDim sbRELP(72) | |
ReDim bPVT(30000, 100) | |
ReDim bRELP(30000, 72) | |
dgv1.ColumnCount = 172 | |
dgv1.RowCount = 2000 | |
ReDim parPVT(100) | |
ReDim parRELP(72) | |
End Sub | |
Private Sub PVT_Trans(ByVal NVC As Integer) | |
'----------------PVT Trans--------------------------------------- | |
'Byte OFFSET=4 (header: B5,62,01,07 4Byte) | |
Dim off As Integer = 4 | |
'1:iTOW 0-3 | |
parPVT(1) = pvtI4(NVC, 0 + off) | |
'2:Year 4-5 | |
parPVT(2) = pvtI2(NVC, 4 + off) | |
'3:month 6 | |
parPVT(3) = CInt(bPVT(NVC, 6 + off)) | |
'4:day 7 | |
parPVT(4) = CInt(bPVT(NVC, 7 + off)) | |
'5:hour 8 | |
parPVT(5) = CInt(bPVT(NVC, 8 + off)) | |
'6:min 9 | |
parPVT(6) = CInt(bPVT(NVC, 9 + off)) | |
'7:sec 10 | |
parPVT(7) = CInt(bPVT(NVC, 10 + off)) | |
'8:valid 11 | |
parPVT(8) = CInt(bPVT(NVC, 11 + off)) | |
'9:tAcc 12,13,14,15 | |
parPVT(9) = pvtI4(NVC, 12 + off) | |
'10:nano 16 | |
parPVT(10) = CInt(bPVT(NVC, 16 + off)) | |
'11:fixType 20 | |
parPVT(11) = CInt(bPVT(NVC, 20 + off)) | |
'12:flags 21 | |
parPVT(12) = CInt(bPVT(NVC, 21 + off)) | |
'13:flags2 22 | |
parPVT(13) = CInt(bPVT(NVC, 22 + off)) | |
'14:numSV 23 | |
parPVT(14) = CInt(bPVT(NVC, 23 + off)) | |
'15:lon'24,25,26,27 | |
parPVT(15) = pvtI4(NVC, 24 + off) | |
'16:lat 28,29,30,31 | |
parPVT(16) = pvtI4(NVC, 28 + off) | |
'17:height 32,33,34,35 | |
parPVT(17) = pvtI4(NVC, 32 + off) | |
'18:hMSL 36,37,38,39 | |
parPVT(18) = pvtI4(NVC, 36 + off) | |
'19:hAcc 40,41,42,43 | |
parPVT(19) = pvtI4(NVC, 40 + off) | |
'20:vAcc 44,45,46,47 | |
parPVT(20) = pvtI4(NVC, 44 + off) | |
'21:velN 48,49,50,51 | |
parPVT(21) = pvtI4(NVC, 48 + off) | |
'22:velE 52,53,54,55 | |
parPVT(22) = pvtI4(NVC, 52 + off) | |
'23:velD 56,57,58,59 | |
parPVT(23) = pvtI4(NVC, 56 + off) | |
'24:gSpeed 60,61,62,63 | |
parPVT(24) = pvtI4(NVC, 60 + off) | |
'25:headMot 64,65,66,67 | |
parPVT(25) = pvtI4(NVC, 64 + off) | |
'26:sAcc 68,69,70,71 | |
parPVT(26) = pvtI4(NVC, 68 + off) | |
'27:headAcc 72,73,74,75 | |
parPVT(27) = pvtI4(NVC, 72 + off) | |
'28:pDOP 76,77 | |
parPVT(28) = pvtI2(NVC, 76 + off) | |
'29:flags3 78 | |
parPVT(29) = CInt(bPVT(NVC, 78 + off)) | |
'30:reserved1 79,80,81,82 | |
parPVT(30) = pvtI4(NVC, 79 + off) | |
'31:headVeh 83,84,85,86 | |
parPVT(31) = pvtI4(NVC, 83 + off) | |
'32:magDec 88,89 | |
parPVT(32) = pvtI2(NVC, 88 + off) | |
'33:magAcc 90,91 | |
parPVT(33) = pvtI2(NVC, 90 + off) | |
End Sub | |
Private Function pvtI4(ByVal dnum As Integer, ByVal sN As Integer) As Integer | |
Dim i4 As Integer | |
If (bPVT(dnum, sN + 3) & 0x80) & 0x80 Then '{//4番目のBYTEの最上位ビットたっていればマイナス | |
i4 = -(bPVT(dnum, sN) * 1 + bPVT(dnum, sN + 1) * 256 + bPVT(dnum, sN + 2) * 65536 + bPVT(dnum, sN + 3) * 256 * 256 * 256) | |
Else | |
i4 = bPVT(dnum, sN) * 1 + bPVT(dnum, sN + 1) * 256 + bPVT(dnum, sN + 2) * 65536 + bPVT(dnum, sN + 3) * 256 * 256 * 256 | |
End If | |
Return i4 | |
End Function | |
Private Function pvtI2(ByVal dnum As Integer, ByVal sN As Integer) As Integer | |
Dim i2 As Integer | |
i2 = bPVT(dnum, sN) * 1 + bPVT(dnum, sN + 1) * 256 | |
Return i2 | |
End Function | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment