public void VesselHeadingMessageLoadsProperties() { N2kMessageEncoded encodedMessage = new N2kMessageEncoded { PGN = 127250, Priority = 2, Destination = 255, Source = 58, Milliseconds = 45, Payload = new byte[] {0xFF, 0x00, 0x00, 0xFF, 0x7F, 0xFF, 0x7F, 0xFD} }; VesselHeading vesselHeading = new VesselHeading(encodedMessage); Assert.AreEqual(255, vesselHeading.SID); Assert.AreEqual(0, vesselHeading.Heading); Assert.AreEqual(187.74108073051684, vesselHeading.Deviation, double.Epsilon); Assert.AreEqual(187.74108073051684, vesselHeading.Variation, double.Epsilon); Assert.AreEqual(HeadingType.True, vesselHeading.HeadingReference); }
public void VesselHeadingMessageLoadsProperties() { N2kMessageEncoded encodedMessage = new N2kMessageEncoded { PGN = 127250, Priority = 2, Destination = 255, Source = 58, Milliseconds = 45, Payload = new byte[] { 0xFF, 0x00, 0x00, 0xFF, 0x7F, 0xFF, 0x7F, 0xFD } }; VesselHeading vesselHeading = new VesselHeading(encodedMessage); Assert.AreEqual(255, vesselHeading.SID); Assert.AreEqual(0, vesselHeading.Heading); Assert.AreEqual(187.74108073051684, vesselHeading.Deviation, double.Epsilon); Assert.AreEqual(187.74108073051684, vesselHeading.Variation, double.Epsilon); Assert.AreEqual(HeadingType.True, vesselHeading.HeadingReference); }
void OnDeviceMessageReceived(object sender, MessageReceivedEventArgs e) { // The message has already been converted from Can Id to ISO-11783 by the Actisense device. if (e.MessageType == MessageType.NMEA2000) { var n2kMessage = new N2kMessageEncoded { // leave parenthesis to prevent unwanted bitshifting Priority = e.Data[(int)ActisenseN2kMessageFormat.Priority], PGN = (e.Data[(int)ActisenseN2kMessageFormat.PGNLSB] + (e.Data[(int)ActisenseN2kMessageFormat.PGNMiddle] << 8) + (e.Data[(int)ActisenseN2kMessageFormat.PGNMSB] << 16)), Destination = e.Data[(int)ActisenseN2kMessageFormat.Destination], Source = e.Data[(int)ActisenseN2kMessageFormat.Source], Milliseconds = (e.Data[(int)ActisenseN2kMessageFormat.TimeStampLSB] + (e.Data[(int)ActisenseN2kMessageFormat.TimeStamp2] << 8) + (e.Data[(int)ActisenseN2kMessageFormat.TimeStamp3] << 16) + (e.Data[(int)ActisenseN2kMessageFormat.TimeStampMSB] << 32)) }; int payloadLength = e.Data[(int)ActisenseN2kMessageFormat.PayloadLength]; byte[] payload = new byte[payloadLength]; // could check there are enough bytes... if (e.Data.Length - (int)ActisenseN2kMessageFormat.PayloadStart < payloadLength) { payloadLength = e.Data.Length - (int)ActisenseN2kMessageFormat.PayloadStart; } Array.Copy(e.Data, (int)ActisenseN2kMessageFormat.PayloadStart, payload, 0, payloadLength); n2kMessage.Payload = payload; var handler = N2kMessageParsed; if (handler != null) { handler(this, new N2kMessageEventArgs(n2kMessage)); } } }
public void BasicMessageParse() { byte[] fakeData = new byte[] { // header information 0x02, 0x12, 0xF1, 0x01, 0xFF, 0x3A, 0x2D, 0x00, 0x00, 0x00, 0x08, // payload 0xFF, 0x00, 0x00, 0xFF, 0x7F, 0xFF, 0x7F, 0xFD }; byte[] expectedPayload = new byte[] { 0xFF, 0x00, 0x00, 0xFF, 0x7F, 0xFF, 0x7F, 0xFD }; FakeDevice fakeDevice = new FakeDevice(); ActisenseMessageParser parser = new ActisenseMessageParser(fakeDevice); var messagesParsed = new List <N2kMessageEventArgs>(); parser.N2kMessageParsed += ((sender, e) => messagesParsed.Add(e)); Assert.AreEqual(0, messagesParsed.Count); fakeDevice.RaiseMessageReceivedEvent(fakeData); Assert.AreEqual(1, messagesParsed.Count); N2kMessageEncoded result = messagesParsed[0].Message; Assert.AreEqual(2, result.Priority); Assert.AreEqual(127250, result.PGN); Assert.AreEqual(255, result.Destination); Assert.AreEqual(58, result.Source); Assert.AreEqual(45, result.Milliseconds); Assert.AreEqual(8, result.PayloadLength); Assert.IsTrue(expectedPayload.SequenceEqual(result.Payload), "arrays should be identical"); }
public N2kMessageEventArgs(N2kMessageEncoded message) { Message = message; }