コード例 #1
0
ファイル: WSstructs.cs プロジェクト: lzrdig/FineOffsetNET
 public ws_record(string name, int pos, ws_types wsType, double scale) : this()
 {
     this.name    = name;
     this.pos     = pos;
     this.ws_type = wsType;
     this.scale   = scale;
 }
コード例 #2
0
ファイル: WSstructs.cs プロジェクト: lzrdig/FineOffsetNET
        int CWS_decode(byte[] raw, ws_types ws_type, float scale, float offset, out string result)
        {
            int   b = Convert.ToInt32(-(Math.Log(scale) + 0.5));
            int   i, m = 0, n = 0;
            float fresult;

            if (b < 1)
            {
                b = 1;
            }
            //if (!result) return 0;
            //else *result = '\0';
            result = "";
            switch (ws_type)
            {
            case ws_types.ub:
                m       = 1;
                fresult = raw[0] * scale + offset;
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            case ws_types.sb:
                m       = 1;
                fresult = raw[0] & 0x7F;
                if (Convert.ToBoolean(raw[0] & 0x80)) // Test for sign bit
                {
                    fresult -= fresult;               //negative value
                }
                fresult = fresult * scale + offset;
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            case ws_types.us:
                m       = 2;
                fresult = CWS_unsigned_short(raw) * scale + offset;
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            case ws_types.ss:
                m       = 2;
                fresult = CWS_signed_short(new ArraySegment <byte>(raw)) * scale + offset;
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            case ws_types.dt:
            {
                char year, month, day, hour, minute;
                year   = (char)CWS_bcd_decode(raw[0]);
                month  = (char)CWS_bcd_decode(raw[1]);
                day    = (char)CWS_bcd_decode(raw[2]);
                hour   = (char)CWS_bcd_decode(raw[3]);
                minute = (char)CWS_bcd_decode(raw[4]);
                m      = 5;
                //n = sprintf(result, "%4d-%02d-%02d %02d:%02d", year + 2000, month, day, hour, minute);
                String.Format(result, "%4d-%02d-%02d %02d:%02d", year + 2000, month, day, hour, minute);
            }
            break;

            case ws_types.tt:
                m = 2;
                //n = sprintf(result, "%02d:%02d", CWS_bcd_decode(raw[0]), CWS_bcd_decode(raw[1]));
                String.Format(result, "%02d:%02d", CWS_bcd_decode(raw[0]), CWS_bcd_decode(raw[1]));
                break;

            case ws_types.pb:
                m = 1;
                //n = sprintf(result, "%02x", raw[0]);
                String.Format(result, "%02x", raw[0]);
                break;

            case ws_types.wa:
                m = 3;
                // wind average - 12 bits split across a byte and a nibble
                fresult = raw[0] + ((raw[2] & 0x0F) * 256);
                fresult = fresult * scale + offset;
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            case ws_types.wg:
                m = 3;
                // wind gust - 12 bits split across a byte and a nibble
                fresult = raw[0] + ((raw[1] & 0xF0) * 16);
                fresult = fresult * scale + offset;
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            case ws_types.dp:
                m = 1;     //error checking for delay
                           // Scale outside temperature and calculate dew point
                fresult = (float)CWS_dew_point(raw, scale, offset);
                //n = sprintf(result, "%.*f", b, fresult);
                String.Format(result, "%.*f", b, fresult);
                break;

            default:
                //MsgPrintf(0, "CWS_decode: Unknown type %u\n", ws_type);
                break;
            }
            for (i = 0; i < m; ++i)
            {
                if (raw[i] != 0xFF)
                {
                    return(n);
                }
            }
            if (Convert.ToBoolean(m))
            {
                //MsgPrintf(0, "CWS_decode: invalid value at 0x%04X\n", raw);

                //sprintf(result, "--.-");
                String.Format(result, "--.-");
                n = 0;
            }
            return(n);
        }