コード例 #1
0
        private bool SetProductInformation(N2kFrame n2kFrame)
        {
            bool changed = false;

            lock (Lock)
            {
                if (!EqualBytesLongUnrolled(PiData, n2kFrame.Data))
                {
                    // We pass here field description and use NMEA2000 definition
                    N2kVersion         = BitConverter.ToUInt16(n2kFrame.Data, 0);
                    ProductCode        = BitConverter.ToUInt16(n2kFrame.Data, 2);
                    ModelID            = N2kASCIIField.ReadString(n2kFrame.Data, 4, 32);
                    SwCode             = N2kASCIIField.ReadString(n2kFrame.Data, 36, 32);
                    ModelVersion       = N2kASCIIField.ReadString(n2kFrame.Data, 68, 32);
                    ModelSerialCode    = N2kASCIIField.ReadString(n2kFrame.Data, 100, 32);
                    CertificationLevel = n2kFrame.Data[132];
                    LoadEquivalency    = n2kFrame.Data[133];
                    PiData             = n2kFrame.Data.ToArray();

                    changed = true;
                }
            }

            return(changed);
        }
コード例 #2
0
        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);
        }