nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
nmealib::nmea2000::Message2000 Class Reference

Represents a generic NMEA 2000 message encapsulating a CAN frame. More...

#include <nmea2000.h>

Inheritance diagram for nmealib::nmea2000::Message2000:
Inheritance graph
[legend]
Collaboration diagram for nmealib::nmea2000::Message2000:
Collaboration graph
[legend]

Public Member Functions

 Message2000 (const Message2000 &)=default
 
Message2000operator= (const Message2000 &)=default
 
 Message2000 (Message2000 &&) noexcept=default
 
Message2000operator= (Message2000 &&)=default
 
 ~Message2000 () override=default
 
std::unique_ptr< nmealib::Messageclone () const override
 Creates a polymorphic deep copy of this Message2000.
 
const std::vector< uint8_t > & getCanId () const noexcept
 Returns the 29-bit CAN identifier as 4 bytes (big-endian).
 
const std::vector< uint8_t > & getCanFrame () const noexcept
 Returns the raw CAN frame payload (0-223 bytes).
 
uint8_t getCanFrameLength () const noexcept
 Returns the number of bytes in the CAN frame payload.
 
uint8_t getPriority () const noexcept
 Returns the 3-bit message priority (0 = highest, 7 = lowest).
 
bool getPriority3 () const noexcept
 Returns individual priority bits (P3 is the MSB).
 
bool getPriority2 () const noexcept
 Bit 3 of canId_[0].
 
bool getPriority1 () const noexcept
 Bit 2 of canId_[0].
 
bool getHeaderReserved () const noexcept
 Returns the Reserved bit (R1).
 
bool getDataPage () const noexcept
 Returns the Data Page bit (DP).
 
uint8_t getPDUFormat () const noexcept
 Returns the PDU Format byte (PF).
 
uint8_t getPDUSpecific () const noexcept
 Returns the PDU Specific byte (PS).
 
uint8_t getSourceAddress () const noexcept
 Returns the source address of the transmitting device.
 
uint8_t getDestinationAddress () const noexcept
 Returns the destination address.
 
uint32_t getPgn () const noexcept
 Returns the PGN (Parameter Group Number) extracted from the CAN Id.
 
virtual std::string getStringContent (bool verbose) const noexcept
 Returns a human-readable string representation of the message.
 
std::string serialize () const override
 Serializes the message to "CANID:data" hex format.
 
virtual bool operator== (const Message2000 &other) const noexcept
 
bool validate () const override
 Returns true if the PGN fits in 18 bits and the frame is 0-223 bytes.
 
- Public Member Functions inherited from nmealib::Message
 Message ()
 
 Message (std::string raw, Type type=Type::Unknown, TimePoint ts=std::chrono::system_clock::now()) noexcept
 
virtual ~Message ()
 
 Message (const Message &)=default
 
Messageoperator= (const Message &)=default
 
 Message (Message &&) noexcept=default
 
Messageoperator= (Message &&) noexcept=default
 
Type getType () const noexcept
 Returns the message type.
 
const std::string & getRawData () const noexcept
 Returns the raw, unmodified data string as received.
 
TimePoint getTimestamp () const noexcept
 Returns the timestamp associated with this message.
 
bool operator== (const Message &other) const noexcept
 Compares two Message objects for equality based on their content.
 

Protected Member Functions

std::string toString (bool verbose) const noexcept
 Returns the base string common to all PGNs (used by getStringContent).
 
- Protected Member Functions inherited from nmealib::Message
void setType (Type t) noexcept
 Sets the message type.
 
void setRaw (std::string r) noexcept
 Sets the raw data string.
 
void setTimestamp (TimePoint ts) noexcept
 Sets the message timestamp.
 

Static Protected Member Functions

static std::unique_ptr< Message2000create (std::string raw, TimePoint ts=std::chrono::system_clock::now())
 Protected factory — parses "CANID:data" (and variant formats) into a Message2000.
 

Protected Attributes

std::vector< uint8_t > canId_
 29-bit CAN Id stored as 4 bytes (big-endian)
 
std::vector< uint8_t > canFrame_
 CAN frame payload (0-223 bytes)
 
- Protected Attributes inherited from nmealib::Message
std::string rawData_
 
Type type_ {Type::Unknown}
 
TimePoint timestamp_ {}
 

Friends

class Nmea2000Factory
 

Additional Inherited Members

- Public Types inherited from nmealib::Message
enum class  Type { Unknown , NMEA0183 , NMEA2000 }
 Defines the type of NMEA message, allowing for differentiation between various standards. More...
 
using TimePoint = std::chrono::system_clock::time_point
 
- Static Public Member Functions inherited from nmealib::Message
static std::string typeToString (Type t)
 Converts a Message::Type enum value to its string representation.
 

Detailed Description

Represents a generic NMEA 2000 message encapsulating a CAN frame.

An NMEA 2000 message is built on the CAN 2.0B protocol (29-bit extended identifier). The 29-bit CAN Id is stored as 4 bytes (big-endian, value right-aligned):

canId_[0]: [ 0 0 0 P3 P2 P1 R1 DP ] canId_[1]: [PF8 PF7 PF6 PF5 PF4 PF3 PF2 PF1] ← PDU Format canId_[2]: [PS8 PS7 PS6 PS5 PS4 PS3 PS2 PS1] ← PDU Specific canId_[3]: [SA8 SA7 SA6 SA5 SA4 SA3 SA2 SA1] ← Source Address

PGN extraction follows the ISO 11783 / NMEA 2000 rules:

RTR and DLC are CAN bus framing fields managed by hardware/drivers and are NOT part of the 29-bit CAN Id; they are not stored here.

Payload: up to 223 bytes (single-frame: 0-8 bytes, fast-packet: 0-223 bytes).

Definition at line 62 of file nmea2000.h.

Constructor & Destructor Documentation

◆ Message2000() [1/2]

nmealib::nmea2000::Message2000::Message2000 ( const Message2000 )
default

◆ Message2000() [2/2]

nmealib::nmea2000::Message2000::Message2000 ( Message2000 &&  )
defaultnoexcept

◆ ~Message2000()

nmealib::nmea2000::Message2000::~Message2000 ( )
overridedefault

Member Function Documentation

◆ clone()

std::unique_ptr< nmealib::Message > nmealib::nmea2000::Message2000::clone ( ) const
overridevirtual

◆ create()

std::unique_ptr< Message2000 > nmealib::nmea2000::Message2000::create ( std::string  raw,
TimePoint  ts = std::chrono::system_clock::now() 
)
staticprotected

Protected factory — parses "CANID:data" (and variant formats) into a Message2000.

Exceptions
InvalidCanFrameExceptionIf the format is unrecognized.
InvalidPgnExceptionIf the extracted PGN exceeds 18 bits.
FrameTooLongExceptionIf the payload exceeds 223 bytes.

Definition at line 109 of file nmea2000.cpp.

◆ getCanFrame()

const std::vector< uint8_t > & nmealib::nmea2000::Message2000::getCanFrame ( ) const
noexcept

Returns the raw CAN frame payload (0-223 bytes).

Definition at line 308 of file nmea2000.cpp.

◆ getCanFrameLength()

uint8_t nmealib::nmea2000::Message2000::getCanFrameLength ( ) const
noexcept

Returns the number of bytes in the CAN frame payload.

Definition at line 312 of file nmea2000.cpp.

◆ getCanId()

const std::vector< uint8_t > & nmealib::nmea2000::Message2000::getCanId ( ) const
noexcept

Returns the 29-bit CAN identifier as 4 bytes (big-endian).

Definition at line 304 of file nmea2000.cpp.

◆ getDataPage()

bool nmealib::nmea2000::Message2000::getDataPage ( ) const
noexcept

Returns the Data Page bit (DP).

Bit 0 of canId_[0]

Definition at line 269 of file nmea2000.cpp.

◆ getDestinationAddress()

uint8_t nmealib::nmea2000::Message2000::getDestinationAddress ( ) const
noexcept

Returns the destination address.

For PDU1 messages (PF < 0xF0) this is getPDUSpecific(). For PDU2 messages the destination is always 255 (global).

Definition at line 283 of file nmea2000.cpp.

◆ getHeaderReserved()

bool nmealib::nmea2000::Message2000::getHeaderReserved ( ) const
noexcept

Returns the Reserved bit (R1).

Always 0 in NMEA 2000; non-zero in J1939. Bit 1 of canId_[0]

Definition at line 266 of file nmea2000.cpp.

◆ getPDUFormat()

uint8_t nmealib::nmea2000::Message2000::getPDUFormat ( ) const
noexcept

Returns the PDU Format byte (PF).

Determines addressing mode: PF < 0xF0 → PDU1 (addressed): canId_[2] is the destination address. PF >= 0xF0 → PDU2 (broadcast): canId_[2] is the PGN group extension. canId_[1]

Definition at line 272 of file nmea2000.cpp.

◆ getPDUSpecific()

uint8_t nmealib::nmea2000::Message2000::getPDUSpecific ( ) const
noexcept

Returns the PDU Specific byte (PS).

Interpretation depends on PDU Format (see getPDUFormat()). canId_[2]

Definition at line 276 of file nmea2000.cpp.

◆ getPgn()

uint32_t nmealib::nmea2000::Message2000::getPgn ( ) const
noexcept

Returns the PGN (Parameter Group Number) extracted from the CAN Id.

The PGN fits in 18 bits (0x00000 - 0x3FFFF).

Definition at line 296 of file nmea2000.cpp.

◆ getPriority()

uint8_t nmealib::nmea2000::Message2000::getPriority ( ) const
noexcept

Returns the 3-bit message priority (0 = highest, 7 = lowest).

Definition at line 256 of file nmea2000.cpp.

◆ getPriority1()

bool nmealib::nmea2000::Message2000::getPriority1 ( ) const
noexcept

Bit 2 of canId_[0].

Definition at line 263 of file nmea2000.cpp.

◆ getPriority2()

bool nmealib::nmea2000::Message2000::getPriority2 ( ) const
noexcept

Bit 3 of canId_[0].

Definition at line 262 of file nmea2000.cpp.

◆ getPriority3()

bool nmealib::nmea2000::Message2000::getPriority3 ( ) const
noexcept

Returns individual priority bits (P3 is the MSB).

Bit 4 of canId_[0]

Definition at line 261 of file nmea2000.cpp.

◆ getSourceAddress()

uint8_t nmealib::nmea2000::Message2000::getSourceAddress ( ) const
noexcept

Returns the source address of the transmitting device.

0-253 = valid node address, 254 = anonymous, 255 = broadcast/global. canId_[3]

Definition at line 279 of file nmea2000.cpp.

◆ getStringContent()

std::string nmealib::nmea2000::Message2000::getStringContent ( bool  verbose) const
virtualnoexcept

Returns a human-readable string representation of the message.

Verbose: multi-line dump of all fields. Non-verbose: single-line summary.

Reimplemented in nmealib::nmea2000::PGN127245, nmealib::nmea2000::PGN127250, nmealib::nmea2000::PGN127251, nmealib::nmea2000::PGN127257, nmealib::nmea2000::PGN128001, nmealib::nmea2000::PGN128259, nmealib::nmea2000::PGN129025, nmealib::nmea2000::PGN129026, and nmealib::nmea2000::PGN130306.

Definition at line 320 of file nmea2000.cpp.

◆ operator=() [1/2]

Message2000 & nmealib::nmea2000::Message2000::operator= ( const Message2000 )
default

◆ operator=() [2/2]

Message2000 & nmealib::nmea2000::Message2000::operator= ( Message2000 &&  )
default

◆ operator==()

bool nmealib::nmea2000::Message2000::operator== ( const Message2000 other) const
virtualnoexcept

◆ serialize()

std::string nmealib::nmea2000::Message2000::serialize ( ) const
overridevirtual

Serializes the message to "CANID:data" hex format.

Implements nmealib::Message.

Definition at line 380 of file nmea2000.cpp.

◆ toString()

std::string nmealib::nmea2000::Message2000::toString ( bool  verbose) const
protectednoexcept

Returns the base string common to all PGNs (used by getStringContent).

Definition at line 337 of file nmea2000.cpp.

◆ validate()

bool nmealib::nmea2000::Message2000::validate ( ) const
overridevirtual

Returns true if the PGN fits in 18 bits and the frame is 0-223 bytes.

Implements nmealib::Message.

Definition at line 407 of file nmea2000.cpp.

Friends And Related Symbol Documentation

◆ Nmea2000Factory

friend class Nmea2000Factory
friend

Definition at line 236 of file nmea2000.h.

Member Data Documentation

◆ canFrame_

std::vector<uint8_t> nmealib::nmea2000::Message2000::canFrame_
protected

CAN frame payload (0-223 bytes)

Definition at line 201 of file nmea2000.h.

◆ canId_

std::vector<uint8_t> nmealib::nmea2000::Message2000::canId_
protected

29-bit CAN Id stored as 4 bytes (big-endian)

Definition at line 200 of file nmea2000.h.


The documentation for this class was generated from the following files: