> For the complete documentation index, see [llms.txt](https://palviewer.twelite.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://palviewer.twelite.info/source-code/format-parser/sample-c.md).

# sample C\#

```csharp
String PalSerialDataToString(String dataText)
{
    System.Text.StringBuilder resultStr = new System.Text.StringBuilder(1024);

    try
    {
        // dataText の文字列をバイト列に変換し、末尾を 0xD で終端する。
        Byte[] b1 = System.Text.Encoding.ASCII.GetBytes(dataText);
        Byte[] b2 = new byte[b1.Length + 1];

        for(int i = 0; i < b1.Length + 1; i++)
        {
            if (i < b1.Length && b1[i] >= 0x20) b2[i] = b1[i];
            else b2[i] = 0x0d;
        }

        // データの解釈
        ITweDataPresentation dataArrival = PAL_Utils.genDataPresentation(ref b2);
        // Pal形式の共通ヘッダオブジェクトの取得（ObjFormatが他の型の場合は例外）
        ClsDataFormatPalApp dataFmt = (ClsDataFormatPalApp)dataArrival.ObjFormat;
        // Pal形式のセンサー部オブジェクトの取得（objPayloadが他の方の場合例外）
        ClsDataPayloadPalSensors dataPay = (ClsDataPayloadPalSensors)dataFmt.objPayload;

        // PAL PCB 形式
        int iPalPcb_CD = (int)dataFmt.BPalPcb & 0xf;
        if (iPalPcb_CD > astrPalPcbNames.Length) iPalPcb_CD = -1; //範囲外は-1
        string sPalPcb_Name = (iPalPcb_CD >= 0) ? astrPalPcbNames[iPalPcb_CD] : "UNKNOWN";

        resultStr.Append(sPalPcb_Name);
        resultStr.Append("/ad=");
        resultStr.Append(Convert.ToString(dataFmt.UiSrcAddr, 16));
        resultStr.Append(",");
        resultStr.Append(dataFmt.BSrcAddr.ToString());
        resultStr.Append("/lq=");
        resultStr.Append(dataFmt.BLQI.ToString());
        
        // 電圧
        int iVolt = 0;
        foreach(ClsDataPalSenseElement m in dataPay.SensorCode((uint)ClsDataPalSenseElement.E_SNSCD.VOLT))
        {
            // 拡張バイトによる判定
            if((byte)(m.Ex) == (byte)ClsDataPalSenseElement.E_EXCD_VOLT.POWER) // ライブラリの戻し型が object になっているため byte にキャスト
            {
                if (m.Data is ushort[])
                {
                    iVolt = ((ushort[])m.Data)[0];
                }
            }
        }

        resultStr.Append(",V=");
        resultStr.Append(iVolt.ToString());
        resultStr.Append("|");


        //磁気センサー
        uint uMagPol = 0;
        bool bMagSense = false;
        foreach (ClsDataPalSenseElement m in dataPay.SensorCode((uint)ClsDataPalSenseElement.E_SNSCD.HALL))
        {
            if (m.Data is byte[])
            {
                uint v = ((byte[])m.Data)[0];
                uMagPol = (v & 0b11);
                bMagSense = (v & 0x80) == 0x80 ? false : true; // false:定期送信 true:磁界の変化を検出
            }

            string strMagPol = "?";
            switch(uMagPol)
            {
                case 0: strMagPol = "-"; break; // 磁石が遠く
                case 1: strMagPol = "N"; break; // N極が近く
                case 2: strMagPol = "S"; break; // S極が近く
            }

            resultStr.Append("MAG=");
            if (bMagSense) resultStr.Append("!");
            resultStr.Append(strMagPol);
            resultStr.Append("|");
        }

        // MOT or CUE
        if (iPalPcb_CD ==3 || iPalPcb_CD == 5)
        {
            const uint MAX_ENT_TABLE = 32;
            short[] x = new short[MAX_ENT_TABLE]; // x,y,z各軸のデータとして一時格納
            short[] y = new short[MAX_ENT_TABLE];
            short[] z = new short[MAX_ENT_TABLE];
            
            uint ent_max = 0; // 格納された最大エントリ番号(格納数は固定ではない場合があります)
            foreach (ClsDataPalSenseElement m in dataPay.SensorCode((uint)ClsDataPalSenseElement.E_SNSCD.ACCEL))
            {
                uint ent = (uint)((byte)m.Ex & 0xF);
                if (ent == 0 || ent > MAX_ENT_TABLE) continue; // not expected
                if (ent > ent_max) ent_max = ent; // update ent_max

                if (m.Data is short[]) // should be short[]
                {
                    short[] v = (short[])m.Data;
                    x[ent - 1] = v[0];
                    y[ent - 1] = v[1];
                    z[ent - 1] = v[2];
                }
            }

            // 結果文字列へ格納
            for (int i = 1; i <= ent_max; i++)
            {
                resultStr.Append("(");
                resultStr.Append(x[i].ToString());
                resultStr.Append(",");
                resultStr.Append(y[i].ToString());
                resultStr.Append(",");
                resultStr.Append(z[i].ToString());
                resultStr.Append(")");
            }
        }

        // AMB or ARIA
        if (iPalPcb_CD == 2 || iPalPcb_CD == 6)
        {
            Double fTemp = 0.0, fHumid = 0.0;

            // 温度データ (℃の100倍、符号あり16bit)
            foreach (ClsDataPalSenseElement m in dataPay.SensorCode((uint)ClsDataPalSenseElement.E_SNSCD.TEMP))
            {
                if (m.Data is short[])
                {
                    short[] v = (short[])m.Data;
                    fTemp = (Double)v[0] / 100.0;
                }
            }

            // 温度データ (%の100倍、符号なし16bit)
            foreach (ClsDataPalSenseElement m in dataPay.SensorCode((uint)ClsDataPalSenseElement.E_SNSCD.HUMD))
            {
                if (m.Data is ushort[])
                {
                    ushort[] v = (ushort[])m.Data;
                    fHumid = (Double)v[0] / 100.0;
                }
            }

            resultStr.Append("T=");
            resultStr.Append(fTemp.ToString());
            resultStr.Append("H=");
            resultStr.Append(fHumid.ToString());
        }

    }
    catch (Exception ex)
    {

    }

    return resultStr.ToString();
}
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://palviewer.twelite.info/source-code/format-parser/sample-c.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
