Files

183 lines
9.8 KiB
Markdown
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- DataPRO/TiltMIF/CRC16.cs
- DataPRO/TiltMIF/Tilt_MIF.cs
generated_at: "2026-04-17T15:57:08.792769+00:00"
model: "zai-org/GLM-5-FP8"
schema_version: 1
sha256: "40eac8f03f30fae0"
---
# TiltMIF Module Documentation
## 1. Purpose
The `TiltMIF` namespace provides functionality for reading, writing, and manipulating Manufacturing Information Files (MIF) for tilt sensor devices. The `Tilt_MIF` class serves as a data container and serialization handler for device calibration data, sensor coefficients, and configuration parameters. It supports binary serialization with CRC-16 validation and XML export for human-readable storage. The `CRC16` static class provides checksum computation using the CRC-16-IBM polynomial (reversed as 0xA001).
---
## 2. Public Interface
### CRC16 Class (Static)
| Member | Signature | Description |
|--------|-----------|-------------|
| `ComputeChecksum` | `public static ushort ComputeChecksum(byte[] bytes)` | Computes a CRC-16 checksum over the input byte array using a pre-computed lookup table. |
### Tilt_MIF Class
#### Constructors
| Signature | Description |
|-----------|-------------|
| `public Tilt_MIF()` | Default constructor. |
| `public Tilt_MIF(bool hasConfig)` | Constructor that sets whether the instance includes configuration data. |
| `public Tilt_MIF(byte[] mifBytes)` | Deserializes a MIF from a byte array. Validates CRC-16 checksums and sets `ReadError` or `ReadError_Config` flags on failure. Accepts arrays of length 128 (MIF only) or 256 (MIF + config). |
#### Metadata Properties
| Property | Type | Description |
|----------|------|-------------|
| `TimeStamp` | `DateTime` | Timestamp for XML export. |
| `SystemName` | `string` | Machine name for XML export. |
| `UserName` | `string` | User name for XML export. |
| `BuildVersion` | `string` | Assembly version string. |
#### MIF Data Properties
| Property | Type | Default/Notes |
|----------|------|---------------|
| `MIFVersion` | `uint` | MIF format version. |
| `SerialNumber` | `string` | 10-character serial number. Getter trims null characters. |
| `HardwareConfiguration` | `ushort` | Hardware configuration flags. |
| `TemperatureCF` | `float` | Temperature coefficient. |
| `TemperatureOffset` | `short` | Temperature offset. |
| `TiltSensorCF1`, `TiltSensorCF2`, `TiltSensorCF3` | `float` | Default 7.5f. |
| `TiltSensorOffset1`, `TiltSensorOffset2`, `TiltSensorOffset3` | `short` | — |
| `TiltSensorRange1`, `TiltSensorRange2`, `TiltSensorRange3` | `float` | Default 360. |
| `TiltCalibrationCF1` through `TiltCalibrationCF18` | `float` | 18 calibration coefficients. |
| `MIF_CRC16` | `ushort` | CRC read from file. |
| `ComputedCRC16` | `ushort` | CRC computed during read/write. |
| `ReadError` | `bool` | Set when MIF CRC validation fails. |
#### Config Data Properties
| Property | Type | Default/Notes |
|----------|------|---------------|
| `SystemID` | `string` | 16-character system identifier. |
| `Location` | `string` | 16-character location string. |
| `Tolerance` | `float` | Default 2. |
| `ArmChecklist` | `bool` | Configuration bit 0. |
| `InvertAxis1`, `InvertAxis2`, `InvertAxis3` | `bool` | Configuration bits 1-3. |
| `AxisToIgnore` | `byte` | Bits 0-1 of second config byte. |
| `AxisLabel1`, `AxisLabel2`, `AxisLabel3` | `Axis` | Enum values (X=0, Y=1, Z=2). |
| `MountOffsetAxis1`, `MountOffsetAxis2`, `MountOffsetAxis3` | `float` | — |
| `TargetAngleAxis1`, `TargetAngleAxis2`, `TargetAngleAxis3` | `float` | — |
| `PreEventADCAxis1`, `PreEventADCAxis2`, `PreEventADCAxis3` | `short` | — |
| `PostEventADCAxis1`, `PostEventADCAxis2`, `PostEventADCAxis3` | `short` | — |
| `Config_CRC16` | `ushort` | CRC read from config section. |
| `ComputedCRC16_Config` | `ushort` | CRC computed during read/write. |
| `ReadError_Config` | `bool` | Set when config CRC validation fails. |
#### Enums
| Enum | Values |
|------|--------|
| `MIFAttributes` | `MIFVersion`, `SerialNumber`, `HardwareConfiguration`, `TemperatureCF`, `TemperatureOffset`, `TiltSensorCF1-3`, `TiltSensorOffset1-3`, `TiltSensorRange1-3`, `TiltCalibrationCF1-18` (32 total) |
| `ConfigAttributes` | `SystemID`, `Location`, `Tolerance`, `ArmChecklist`, `AxisToIgnore`, `AxisLabel1-3`, `InvertAxis1-3`, `MountOffsetAxis1-3`, `TargetAngleAxis1-3`, `PreEventADCAxis1-3`, `PostEventADCAxis1-3` (23 total) |
| `Axis` | `X = 0`, `Y = 1`, `Z = 2` |
#### MIF Attribute Methods
| Method | Signature | Description |
|--------|-----------|-------------|
| `GetTiltAttributes` | `public List<MIFAttribute> GetTiltAttributes()` | Returns all MIF attributes as a list of `MIFAttribute` objects (contains `MIFAttributes` enum and string value). |
| `GetTiltAttributeDictionary` | `public Dictionary<string, object> GetTiltAttributeDictionary()` | Returns all MIF attributes as a dictionary keyed by attribute name. |
| `GetMIFAttribute` | `public object GetMIFAttribute(MIFAttributes attribute)` | Returns the value of a specific MIF attribute. Returns `null` for unrecognized attributes. |
| `SetMIFAttribute` | `public void SetMIFAttribute(MIFAttributes attribute, string newValue)` | Sets a MIF attribute value by parsing the string. Silently ignores unrecognized attributes. |
#### Config Attribute Methods
| Method | Signature | Description |
|--------|-----------|-------------|
| `GetTiltConfigAttributes` | `public List<ConfigAttribute> GetTiltConfigAttributes()` | Returns all config attributes as a list of `ConfigAttribute` objects. |
| `GetTiltConfigAttributeDictionary` | `public Dictionary<string, object> GetTiltConfigAttributeDictionary()` | Returns all config attributes as a dictionary keyed by attribute name. |
| `GetConfigAttribute` | `public object GetConfigAttribute(ConfigAttributes attribute)` | Returns the value of a specific config attribute. Returns `null` for unrecognized attributes. |
| `SetConfigAttribute` | `public void SetConfigAttribute(ConfigAttributes attribute, string newValue)` | Sets a config attribute value by parsing the string. Silently ignores unrecognized attributes. |
#### Calibration Array Methods
| Method | Signature | Description |
|--------|-----------|-------------|
| `GetTiltCalibrations` | `public double[] GetTiltCalibrations()` | Returns all 18 tilt calibration coefficients as a `double[]`. |
| `SetTiltCalibrations` | `public void SetTiltCalibrations(float[] calibrations)` | Sets calibration coefficients from a `float[]`. Throws `InvalidDataException` if length is not 18. |
| `SetTiltCalibrations` | `public void SetTiltCalibrations(double[] calibrations)` | Sets calibration coefficients from a `double[]` (converted to float). Throws if length is not 18. |
#### Serialization Methods
| Method | Signature | Description |
|--------|-----------|-------------|
| `GetBytes` | `public byte[] GetBytes()` | Serializes the MIF (and config if `_hasConfig` is true) to a byte array. Computes and appends CRC-16 values. |
| `WriteXML` | `public void WriteXML(FileStream file)` | Serializes the instance to XML. Sets `TimeStamp`, `SystemName`, `UserName`, and `BuildVersion` before serialization. Closes the stream in a finally block. |
| `GetVersionString` | `public string GetVersionString()` | Returns the executing assembly version in format `major.minor.build`. Returns "N/A" on exception. |
#### Constants
| Constant | Value | Description |
|----------|-------|-------------|
| `MIF_BYTE_LENGTH` | 128 | Fixed byte length for MIF section. |
| `MIF_CONFIG_BYTE_LENGTH` | 256 | Total byte length for MIF + config. |
| `TILTCAL_COUNT` | 18 (private) | Number of tilt calibration coefficients. |
---
## 3. Invariants
1. **Fixed byte lengths**: MIF data is always 128 bytes; MIF with config is always 256 bytes.
2. **Fixed string field sizes**: `SerialNumber` is exactly 10 characters; `SystemID` and `Location` are exactly 16 characters. Longer input strings will be truncated; shorter strings will be padded with null characters.
3. **CRC validation on read**: The `Tilt_MIF(byte[])` constructor validates CRC-16 checksums. If `MIF_CRC16 != ComputedCRC16` or `MIF_CRC16 == 0`, `ReadError` is set to `true` and no further parsing occurs.
4. **Config CRC validation**: If config data is present, `ReadError_Config` is set when `Config_CRC16 != ComputedCRC16_Config`, but config parsing continues if the MIF section was valid.
5. **Calibration array length**: `SetTiltCalibrations` requires exactly 18 elements; otherwise, `InvalidDataException` is thrown.
6. **CRC-16 polynomial**: Uses polynomial `0xA001` (CRC-16-IBM reversed) with a 256-entry lookup table initialized in the static constructor.
---
## 4. Dependencies
### This module depends on:
- `System` (DateTime, BitConverter, Environment, Version, Reflection)
- `System.Collections.Generic` (List, Dictionary)
- `System.IO` (MemoryStream, BinaryReader, FileStream, InvalidDataException)
- `System.Linq` (Enumerable extensions)
- `System.Text` (Encoding.UTF8)
- `System.Xml.Serialization` (XmlSerializer)
### What depends on this module:
- **Unknown from source alone** — no consumers are shown in the provided files.
---
## 5. Gotchas
### Critical Bug: `SetConfigAttribute` PostEventADC Assignments
In `SetConfigAttribute`, the cases for `ConfigAttributes.PostEventADCAxis2` and `ConfigAttributes.PostEventADCAxis3` incorrectly assign to `PostEventADCAxis1`:
```csharp
case ConfigAttributes.PostEventADCAxis2:
PostEventADCAxis1 = short.Parse(newValue); // BUG: should be PostEventADCAxis2
break;
case ConfigAttributes.PostEventADCAxis3:
PostEventADCAxis1 = short.Parse(newValue); // BUG: should be PostEventADCAxis3
break;
```
### `SetTwoBits` Method Ignores `ref byte inByte` Parameter
The private method `SetTwoBits(int bitInByte, byte fistTwoBits, ref byte inByte)` always modifies `_configBits[1]` directly, ignoring the `ref byte inByte` parameter:
```csharp
private void SetTwoBits(int bitInByte, byte fistTwoBits, ref byte inByte)
{
SetBit(bitInByte, (fistTwoBits & 0x01) == 0x01, ref _configBits