sample VB.net

Function PalSerialDataToString(ByRef dataText As String) As String
    PalSerialDataToString = ""

    Try
        Dim i, j As Integer
        Dim l, m As Object
        Dim resultStr As New System.Text.StringBuilder(1024)

        '' PAL 標準フォーマットを解釈する
        Dim b() As Byte = System.Text.Encoding.ASCII.GetBytes(dataText & vbCr)
        For i = b.Length - 1 To 0 Step -1 ' 末尾のコントロール文字を 0x0D に書き換え
            If b(i) < &H20 Then b(i) = &HD Else Exit For
        Next
        Dim dataArrival = PAL_Utils.genDataPresentation(b)
        Dim dataFmt As ClsDataFormatPalApp = dataArrival.objFormat 'データ形式、無線ノードの情報
        Dim dataPay As ClsDataPayloadPalSensors = dataFmt.objPayload '各種センサー情報

        ' PAL の種別
        Dim iPalPcb_CD As UInteger = (dataFmt.BPalPcb And &HF)
        Dim sPalPcb_Name As String
        Select Case iPalPcb_CD
            Case 0
                sPalPcb_Name = "nc"
            Case 1
                sPalPcb_Name = "MAG"
            Case 2
                sPalPcb_Name = "AMB"
            Case 3
                sPalPcb_Name = "MOT"
            Case 4
                sPalPcb_Name = "NOTICE"
            Case 5
                sPalPcb_Name = "CUE"
            Case 6
                sPalPcb_Name = "ARIA"
            Case Else
                sPalPcb_Name = "UNK"
        End Select

        resultStr.Append(sPalPcb_Name)
        resultStr.Append("/ad=")
        resultStr.Append(Conversion.Hex(dataFmt.UiSrcAddr))
        resultStr.Append(",")
        resultStr.Append(dataFmt.BSrcAddr.ToString())
        resultStr.Append("/lq=")
        resultStr.Append(dataFmt.BLQI.ToString())
        resultStr.Append("|")

        ' 電圧
        Dim iVolt As Integer
        l = dataPay.SensorCode(ClsDataPalSenseElement.E_SNSCD.VOLT)
        For Each m In l
            If m.Ex = ClsDataPalSenseElement.E_EXCD_VOLT.POWER Then
                iVolt = m.Data(0)
            End If
        Next
        
        ' 磁気センサー
        Dim iMagPol As Integer
        Dim bMagSense As Boolean
        l = dataPay.SensorCode(ClsDataPalSenseElement.E_SNSCD.HALL)
        For Each m In l
            ' 極情報
            iMagPol = m.Data(0) And &H3
            ' True 磁石の変化, False 定期送信
            If m.Data(0) >= &H80 Then bMagSense = False Else bMagSense = True

            Dim strMagPol As String
            If iMagPol = 0 Then
                strMagPol = "-" '磁石が遠く
            ElseIf iMagPol = 1 Then
                strMagPol = "N" 'N極が近く
            ElseIf iMagPol = 2 Then
                strMagPol = "S" 'S極が近く
            Else
                strMagPol = "?"
            End If

            resultStr.Append("MAG=")
            If bMagSense Then resultStr.Append("!")
            resultStr.Append(strMagPol)
            resultStr.Append("|")
        Next
        
        If iPalPcb_CD = 3 Or iPalPcb_CD = 5 Then
            Const MAX_ENT_TABLE As Integer = 32
            Dim x(MAX_ENT_TABLE), y(MAX_ENT_TABLE), z(MAX_ENT_TABLE) As Integer
            Dim ent_max As Integer ' maximum entry

            ent_max = 0 ' entries

            l = dataPay.SensorCode(ClsDataPalSenseElement.E_SNSCD.ACCEL)
            For Each m In l
                Dim ent As Integer

                ' 上位4ビットはサンプリングレートの定義、下位4ビットはサンプルの番号 1...16など。
                ent = m.Ex And &HF

                ' 通常想定しないが念の為
                If ent > MAX_ENT_TABLE Then
                    Continue For
                End If

                ' 最大エントリの更新
                If ent > ent_max Then
                    ent_max = ent
                End If

                x(ent) = m.Data(0)
                y(ent) = m.Data(1)
                z(ent) = m.Data(2)
            Next

            resultStr.Append("(x,y,z)[")
            resultStr.Append(ent_max.ToString())
            resultStr.Append("]=")

            For i = 1 To ent_max
                resultStr.Append("(")
                resultStr.Append(x(i).ToString())
                resultStr.Append(",")
                resultStr.Append(y(i).ToString())
                resultStr.Append(",")
                resultStr.Append(z(i).ToString())
                resultStr.Append(")")
            Next

        End If

        If iPalPcb_CD = 2 Or iPalPcb_CD = 6 Then ' AMB or ARIA
            ' TEMP
            Dim fTemp As Single
            l = dataPay.SensorCode(ClsDataPalSenseElement.E_SNSCD.TEMP)
            If l.Count > 0 Then
                Try
                    Dim t As Integer = CInt(l(0).Data(0))

                    '' 温度が USHORT で報告される問題に対する対応
                    If t >= 32768 Then
                        t = -(65536 - t)
                    End If

                    fTemp = CSng(t) / 100.0
                Catch ex As Exception
                    '' some error
                End Try
            End If

            ' HUMD
            Dim fHumid As Single
            l = dataPay.SensorCode(ClsDataPalSenseElement.E_SNSCD.HUMD)
            If l.Count > 0 Then
                Try
                    fHumid = CSng(l(0).Data(0)) / 100.0
                Catch ex As Exception
                    '' some error
                End Try
            End If

            resultStr.Append("T=")
            resultStr.Append(fTemp.ToString())
            resultStr.Append("H=")
            resultStr.Append(fHumid.ToString())
        End If

        PalSerialDataToString = resultStr.ToString()

    Catch ex As Exception
        '' some error
    End Try
End Functionvb

最終更新