private void deleteFieldsToolStripMenuItem_Click(object sender, EventArgs e) { if (dataGridViewFields.Focused) { var fields = (N2kField[])dataGridViewFields.DataSource; // We set all selected items to null, and then rebuild the array of Fields for (int i = 0; i < dataGridViewFields.Rows.Count; i++) { if (dataGridViewFields.Rows[i].Selected) { fields[i] = null; } } //foreach (var r in dataGridViewFields.SelectedRows) { // MainForm.PGNDefns.PGNDefns[r.Index] = null; //} var newFields = new N2kField[fields.Length - dataGridViewFields.SelectedRows.Count]; int j = 0; for (int i = 0; i < fields.Length; i++) { if (fields[i] != null) { newFields[j++] = fields[i]; } } PGNDefnCol.PGNDefns[dataGridViewPGNs.SelectedRows[0].Index].Fields = newFields; UpdateGridDisplays(GridViewsUpdateType.RebindFields); // Delete a field } }
// Field operations public void AddField(N2kField f) { N2kField[] fields = Fields; int length = fields.Length; Array.Resize(ref fields, length + 1); Fields = fields; Fields[length] = f; RebuildFieldDict(); }
public PGNDefnUnknown() // This PGn definition is used whenever we receive an unrecognised PGN // It has a single field which does a binary dump of the data { PGN = 0; ByteLength = 0; Name = "Unknown PGN"; Fields = new N2kField[1] { new N2kBinaryField { BitLength = 0, BitOffset = 0, Description = "Unknown", Name = "Unknown" } }; }
// This allows the user to change the class of a field private void FieldClassTypeListComboBox_SelectionChangeCommitted(object sender, EventArgs e) { if (propertyGridField.SelectedObject != null) { N2kField f = ((N2kField[])dataGridViewFields.DataSource)[dataGridViewFields.SelectedRows[0].Index]; if (FieldClassTypeListComboBox.Text != f.GetType().Name) { UpdatePropertyValuesDict(ref FieldPropertyValues, propertyGridField.SelectedObject); ((N2kField[])dataGridViewFields.DataSource)[dataGridViewFields.SelectedRows[0].Index] = N2kField.CreateNewField((Type)FieldClassTypeListComboBox.SelectedItem); CopyTo(FieldPropertyValues, ((N2kField[])dataGridViewFields.DataSource)[dataGridViewFields.SelectedRows[0].Index]); UpdateGridDisplays(GridViewsUpdateType.RebindFields); AnyChanges = true; } } }
// Creator public PGNExplorerForm(string fileName) { InitializeComponent(); PGNDefnCol = PGNDefnCollection.LoadFromFile(fileName); // Creates our own copy FormTitleText = this.Text; if (fileName == "") { this.Text += " - <Internal>"; } else { this.Text += " - " + fileName; } // We have a combo-box thatallowsd the type (class) of a field to be changed; populate this with the list of all CAN Field Types FieldClassTypeListComboBox.Items.AddRange(N2kField.AllFieldTypes()); // We display the DisplayName field of these UpdateGridDisplays(GridViewsUpdateType.RebindPGNs); AnyChanges = false; savePGNDefinitionFileToolStripMenuItem.Enabled = (fileName != ""); }
public PGNDefn[] GetPGNInfos() // Get an array of PGNDefn records, effectively converting from KeesDefinitions // into our own PGNDefn array { PGNDefn[] result = new PGNDefn[KeesPGNDefns.Count()]; int j = 0; foreach (KeesPGNDefn kPGN in KeesPGNDefns) { N2kField[] f = new N2kField[kPGN.Fields != null ? kPGN.Fields.Count() : 0]; if (kPGN.Fields != null) { int i = 0; foreach (KeesField kField in kPGN.Fields) { double scale; if (!double.TryParse(kField.Resolution, NumberStyles.Number, CultureInfo.InvariantCulture, out scale)) { scale = 1.0; } /* // Old method for working out scale... * switch (kField.Resolution) * { * case "0.1": * scale = 0.1; * break; * case "0.01": * scale = 0.01; * break; * case "0.001": * scale = 0.001; * break; * case "0.004": * scale = 0.004; * break; * case "1e-006": * scale = 1e-6; * break; * case "0.0001": * scale = 0.001; * break; * default: * scale = 1; * break; * } */ if ((kPGN.PGN == 130842) && (kField.Order == 17) && (kField.Name == "Spare")) { // AJM: I think this is probably an unsigned integer based on Description="0=unavailable" f[i++] = new N2kUIntField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; } else if (CultureInfo.InvariantCulture.CompareInfo.IndexOf(kField.Name, "Instance", CompareOptions.IgnoreCase) >= 0) { f[i++] = new N2kInstanceField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; } else if ((kField.Name == "SID")) { f[i++] = new N2kUIntField { FormatAs = FormatEnum.SID, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; } else if ((kField.Name == "PGN")) { // We force all PGNBs to be UInt; Kees has one defined as an Integer in 59904, but no such 'Integer' defn in 59392 f[i++] = new N2kUIntField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; } else if ((kField.Name == "Speed") || (kField.Name == "SOG")) { //f[i++] = new SpeedField { DisplayName = kField.DisplayName, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = scale }; f[i++] = new N2kUDblField { FormatAs = FormatEnum.Speed, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = scale }; } else if ((kField.Name == "Depth") || (kField.Name == "SOG")) { // This is probably unsigned based on FFFFFFFF appearing to mean 'unavailable' f[i++] = new N2kUDblField { FormatAs = FormatEnum.Length, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = scale }; } else { switch (kField.Type) { case null: if ((kField.Resolution == null) || (kField.Resolution == "")) { f[i++] = new N2kUIntField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; } else { f[i++] = new N2kDblField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = scale }; } break; case "ASCII text": f[i++] = new N2kASCIIField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; break; case "Binary data": case "String with start/stop byte": // We need new type for this f[i++] = new N2kBinaryField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; break; case "Manufacturer code": f[i++] = new N2kBinaryField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; break; case "Date": //f[i++] = new DateField { DisplayName = kField.DisplayName, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; f[i++] = new N2kUIntField { FormatAs = FormatEnum.Date, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; break; case "Degrees": // Degrees changed to angle on Kees? case "Angle": if (((kField.Resolution == "0.0001 rad") || (kField.Resolution == "0.0001"))) { // We convert the units from Radians to Degrees if (kField.Signed) { f[i++] = new N2kDblField { FormatAs = FormatEnum.Heading, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = 0.0001 * 180 / Math.PI }; } else { f[i++] = new N2kUDblField { FormatAs = FormatEnum.Heading, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = 0.0001 * 180 / Math.PI }; } } else { Console.WriteLine("Unknown Kees Degrees field data"); } break; case "IEEE Float": f[i++] = new N2kDblField { FormatAs = FormatEnum.Number, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = 32, Description = kField.Description, Scale = scale }; break; case "Integer": f[i++] = new N2kIntField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; break; case "Latitude": if (kField.BitLength == 32) { // LatField f[i++] = new N2kDblField { FormatAs = FormatEnum.Latitude, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = 32, Description = kField.Description, Scale = 1e-7 }; } else if (kField.BitLength == 64) { // LatField f[i++] = new N2kDblField { FormatAs = FormatEnum.Latitude, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = 64, Description = kField.Description, Scale = 1e-16 }; } else { Console.WriteLine("Unknown Kees Latitude field data"); } break; case "Longitude": if (kField.BitLength == 32) { // LonField f[i++] = new N2kDblField { FormatAs = FormatEnum.Longitude, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = 32, Description = kField.Description, Scale = 1e-7 }; } else if (kField.BitLength == 64) { // LonField f[i++] = new N2kDblField { FormatAs = FormatEnum.Longitude, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = 64, Description = kField.Description, Scale = 1e-16 }; } else { Console.WriteLine("Unknown Kees Longitude field data"); } break; case "Lookup table": f[i++] = new N2kEnumField { Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, EnumPairArray = kField.EnumPairArray }; break; case "Temperature": if ((kField.Units == null) || (kField.Units == "K")) { // We convert this to Celcius // f[i++] = new TemperatureField { DisplayName = kField.DisplayName, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description }; f[i++] = new N2kUDblField { FormatAs = FormatEnum.Temperature, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = 0.01, Offset = -273.15 }; } else { Console.WriteLine("Unknown Kees Temperature field data"); } break; case "Time": // TimeField f[i++] = new N2kUDblField { FormatAs = FormatEnum.Time, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = 0.0001 }; break; case "Speed": f[i++] = new N2kUDblField { FormatAs = FormatEnum.Speed, Name = kField.Name, BitOffset = kField.BitOffset, BitLength = kField.BitLength, Description = kField.Description, Scale = scale }; break; default: Console.WriteLine("Unknown Kees Field"); break; } } } } uint ByteLength = 0; foreach (N2kField field in f) { ByteLength = (uint)Math.Max(ByteLength, (int)(((uint)((uint)field.BitOffset + (uint)field.BitLength + 7)) >> 3)); } if (kPGN.PGN == 129029) { ByteLength = 43; // Add 3 extra bytes missing in Kees data } result[j++] = new PGNDefn { PGN = kPGN.PGN, Name = kPGN.Description, Description = "", ByteLength = ByteLength, Fields = f }; } return(result); }