NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
tNMEA2000 Class Referenceabstract

tNMEA2000 device class definition. More...

#include <NMEA2000.h>

Collaboration diagram for tNMEA2000:

Classes

struct  tCANSendFrame
 Structure holds all the data needed for a valid CAN-Message. More...
 
struct  tConfigurationInformation
 Structure that holds the Configuration Information of this device. More...
 
class  tDevice
 This class represents a N2k device. More...
 
class  tDeviceInformation
 Class that holds all the device informations and several helper functions to that. More...
 
class  tInternalDevice
 This class represents an internal device. More...
 
class  tMsgHandler
 Message handler class. More...
 
struct  tProductInformation
 Structure that holds all the product information. More...
 

Public Types

enum  tDebugMode { dm_None , dm_ClearText , dm_Actisense }
 For debugging we have some cases for SendMsg. More...
 
enum  tForwardType { fwdt_Actisense , fwdt_Text }
 Type how to forward messages in listen mode. More...
 
enum  tN2kMode {
  N2km_ListenOnly , N2km_NodeOnly , N2km_ListenAndNode , N2km_SendOnly ,
  N2km_ListenAndSend
}
 System mode defines how the device will behave on the NMEA2000 bus. More...
 

Public Member Functions

 tNMEA2000 ()
 Construct a new NMEA2000 object. More...
 
void AddGroupFunctionHandler (tN2kGroupFunctionHandler *pGroupFunctionHandler)
 Add a message handler for incoming Group Function messages. More...
 
void AttachMsgHandler (tMsgHandler *_MsgHandler)
 Attach a message handler for incoming N2kMessages. More...
 
void DetachMsgHandler (tMsgHandler *_MsgHandler)
 Detach a message handler for incoming N2kMessages. More...
 
void EnableForward (bool v=true)
 Enable message forwarding to stream. More...
 
void ExtendFastPacketMessages (const unsigned long *_FastPacketMessages)
 Set the list of known Extended Fast Packet Messages. More...
 
void ExtendReceiveMessages (const unsigned long *_ReceiveMessages, int iDev=0)
 Extend the list of Received Messages. More...
 
void ExtendSingleFrameMessages (const unsigned long *_SingleFrameMessages)
 Set the list of known Extended Single Frame Messages. More...
 
void ExtendTransmitMessages (const unsigned long *_TransmitMessages, int iDev=0)
 Extend the list of Transmitted Messages. More...
 
int FindSourceDeviceIndex (unsigned char Source) const
 Finds a device on Devices by its source address. More...
 
unsigned char GetCertificationLevel (int iDev=0) const
 Get the Certification Level of the device. More...
 
const tDeviceInformation GetDeviceInformation (int iDev=0)
 Get the Device Information. More...
 
N2kStreamGetForwardStream () const
 Read current Forward Stream object. More...
 
uint32_t GetHeartbeatInterval (int iDev=0)
 Get the Heartbeat Interval of a device. More...
 
uint32_t GetHeartbeatOffset (int iDev=0)
 Get the Heartbeat Offset of a device. More...
 
void GetInstallationDescription1 (char *buf, size_t max_len)
 Get the Install Description 1 of this device. More...
 
void GetInstallationDescription2 (char *buf, size_t max_len)
 Get the Install Description 2 of this device. More...
 
unsigned char GetLoadEquivalency (int iDev=0) const
 Get the Load Equivalency of this device. More...
 
void GetManufacturerInformation (char *buf, size_t max_len)
 Get the Manufacturer Information of this device. More...
 
void GetModelID (char *buf, size_t max_len, int iDev=0) const
 Get the ModelID of the device. More...
 
void GetModelSerialCode (char *buf, size_t max_len, int iDev=0) const
 Get the Model Serial of the device. More...
 
void GetModelVersion (char *buf, size_t max_len, int iDev=0) const
 Get the Model Version of the device. More...
 
unsigned char GetN2kSource (int DeviceIndex=0) const
 Read address for current device. More...
 
unsigned short GetN2kVersion (int iDev=0) const
 Get the N2k standard version of the device. More...
 
unsigned short GetProductCode (int iDev=0) const
 Get the Product Code of the device. More...
 
const tNMEA2000::tProductInformationGetProductInformation (int iDev, bool &IsProgMem) const
 Get the Product Information of the device. More...
 
void GetSwCode (char *buf, size_t max_len, int iDev=0) const
 Get the Sw Code of the device. More...
 
bool IsOpen () const
 Test is NMEA2000 open and running. More...
 
bool IsTxPGN (unsigned long PGN, int iDev=0)
 Check if this message is a Transmit message of this device. More...
 
bool Open ()
 Open the CAN device. More...
 
void ParseMessages ()
 Parse all incoming Messages. More...
 
bool ReadResetAddressChanged ()
 Check if this device has changed its address. More...
 
bool ReadResetDeviceInformationChanged ()
 Check if this device has changed its DeviceInstances or SystemInstance. More...
 
bool ReadResetInstallationDescriptionChanged ()
 Check if this device has changed its Install Description. More...
 
void RemoveGroupFunctionHandler (tN2kGroupFunctionHandler *pGroupFunctionHandler)
 Remove a message handler for incoming Group Function messages. More...
 
void Restart ()
 Restart the device. More...
 
bool SendConfigurationInformation (int DeviceIndex=0)
 Send a Config Information message. More...
 
bool SendConfigurationInformation (unsigned char Destination, int DeviceIndex, bool UseTP)
 Send a Config Information message. More...
 
void SendHeartbeat (bool force=false)
 Send Heartbeat for all devices. More...
 
void SendHeartbeat (int iDev)
 Send heartbeat for specific device. More...
 
void SendIsoAddressClaim (unsigned char Destination=0xff, int DeviceIndex=0, unsigned long FromNow=0)
 Send an IsoAddressClaim message. More...
 
bool SendMsg (const tN2kMsg &N2kMsg, int DeviceIndex=0)
 Send message to the NMEA2000 bus. More...
 
bool SendProductInformation (int DeviceIndex=0)
 Send a Product Information message. More...
 
bool SendProductInformation (unsigned char Destination, int DeviceIndex, bool UseTP)
 Send a Product Information message. More...
 
void SendRxPGNList (unsigned char Destination, int DeviceIndex, bool UseTP=false)
 Send a list with all supported Receive messages. More...
 
void SendTxPGNList (unsigned char Destination, int DeviceIndex, bool UseTP=false)
 Send a list with all supported Transmit messages. More...
 
void SetConfigurationInformation (const char *ManufacturerInformation, const char *InstallationDescription1=0, const char *InstallationDescription2=0)
 Set the Configuration Information of this device. More...
 
void SetDebugMode (tDebugMode _dbMode)
 Set the Debug Mode of the system. More...
 
void SetDeviceCount (const uint8_t _DeviceCount)
 Set the count of devices library shows on bus. More...
 
void SetDeviceInformation (unsigned long _UniqueNumber, unsigned char _DeviceFunction=0xff, unsigned char _DeviceClass=0xff, uint16_t _ManufacturerCode=0xffff, unsigned char _IndustryGroup=4, int iDev=0)
 Set the Device Information. See also NAME. More...
 
void SetDeviceInformationInstances (uint8_t _DeviceInstanceLower=0xff, uint8_t _DeviceInstanceUpper=0xff, uint8_t _SystemInstance=0xff, int iDev=0)
 Set the Device Information Instances. More...
 
void SetFastPacketMessages (const unsigned long *_FastPacketMessages)
 Set the list of known Fast Packet Messages. More...
 
void SetForwardOnlyKnownMessages (bool v=true)
 Enable Only Known Messages for forwarding. More...
 
void SetForwardOwnMessages (bool v=true)
 Enable Own Messages for forwarding. More...
 
void SetForwardStream (N2kStream *_stream)
 Set the Forward Stream object. More...
 
void SetForwardSystemMessages (bool v=true)
 Enable System Messages for forwarding. More...
 
void SetForwardType (tForwardType fwdType)
 Set the Forward Streaming Type. More...
 
void SetHandleOnlyKnownMessages (bool v=true)
 Set the Handle Only Known Messages. More...
 
void SetHeartbeatInterval (unsigned long interval, bool SetAsDefault=true, int iDev=-1) __attribute__((deprecated))
 Deprecated. Use function SetHeartbeatIntervalAndOffset. More...
 
void SetHeartbeatIntervalAndOffset (uint32_t interval, uint32_t offset=0, int iDev=-1)
 Set the Heartbeat Interval and Offset for a device. More...
 
void SetInstallationDescription1 (const char *InstallationDescription1)
 Set the Installation Description 1 of this device. More...
 
void SetInstallationDescription2 (const char *InstallationDescription2)
 Set the Installation Description 2 of this device. More...
 
void SetISORqstHandler (bool(*ISORequestHandler)(unsigned long RequestedPGN, unsigned char Requester, int DeviceIndex))
 Set the message handler for incoming ISO Requests. More...
 
void SetMode (tN2kMode _N2kMode, uint8_t _N2kSource=15)
 Set the library mode and start source address. More...
 
void SetMsgHandler (void(*_MsgHandler)(const tN2kMsg &N2kMsg))
 Set the message handler for incoming NMEA2000 messages. More...
 
void SetN2kCANMsgBufSize (const uint8_t _MaxN2kCANMsgs)
 Set incoming CAN message (tNMEA2000::N2kCANMsgBuf) buffer size. More...
 
virtual void SetN2kCANReceiveFrameBufSize (const uint16_t _MaxCANReceiveFrames)
 Set CAN receive frame buffer size. More...
 
virtual void SetN2kCANSendFrameBufSize (const uint16_t _MaxCANSendFrames)
 Set CAN send frame buffer size. More...
 
void SetN2kSource (unsigned char _iAddr, int _iDev=0)
 Set source for the given device. More...
 
void SetOnOpen (void(*_OnOpen)())
 Set OnOpen callback function. More...
 
void SetProductInformation (const char *_ModelSerialCode, unsigned short _ProductCode=0xffff, const char *_ModelID=0, const char *_SwCode=0, const char *_ModelVersion=0, unsigned char _LoadEquivalency=0xff, unsigned short _N2kVersion=0xffff, unsigned char _CertificationLevel=0xff, int iDev=0)
 Set the Product Information of this device. More...
 
void SetProductInformation (const tProductInformation *_ProductInformation, int iDev=0)
 Set the Product Information of this device. More...
 
void SetProgmemConfigurationInformation (const char *ManufacturerInformation, const char *InstallationDescription1=0, const char *InstallationDescription2=0)
 Set the Configuration Information located on PROGMEM. More...
 
void SetSingleFrameMessages (const unsigned long *_SingleFrameMessages)
 Set the list of known Single Frame Messages. More...
 

Static Public Member Functions

static void ClearCharBuf (size_t MaxLen, char *buf)
 Clears a char array buffer with 0s. More...
 
static void ClearSetCharBuf (const char *str, size_t MaxLen, char *buf)
 Setting up a clean Char Buffer. More...
 
static bool IsBroadcast (unsigned char Source)
 Checks if the given Address is a broadcast address. More...
 
static bool IsProprietaryMessage (unsigned long PGN)
 Check if the given PGN is proprietary. More...
 
template<typename T >
static T N2kMax (T a, T b)
 Delivers Max out of A an B. More...
 
template<typename T >
static T N2kMin (T a, T b)
 Delivers Min out of A an B. More...
 
static void SetCharBuf (const char *str, size_t MaxLen, char *buf)
 Setting up a Char Buffer. More...
 

Protected Types

enum  tOpenState { os_None , os_OpenCAN , os_WaitOpen , os_Open }
 Library open state. More...
 

Protected Member Functions

virtual bool CANGetFrame (unsigned long &id, unsigned char &len, unsigned char *buf)=0
 Abstract class for reading frame from driver class. More...
 
virtual bool CANOpen ()=0
 Abstract class for initializing and opening CAN interface. More...
 
virtual bool CANSendFrame (unsigned long id, unsigned char len, const unsigned char *buf, bool wait_sent=true)=0
 Abstract class for sending a CAN Frame. More...
 
bool CheckKnownMessage (unsigned long PGN, bool &SystemMessage, bool &FastPacket)
 Check if this Message is known to the system. More...
 
void CopyProgmemConfigurationInformationToLocal ()
 Copy Configuration Information to local memory. More...
 
void EndSendTPMessage (int iDev)
 Ends sending of ISO-TP message. More...
 
void FindFreeCANMsgIndex (unsigned long PGN, unsigned char Source, unsigned char Destination, bool TPMsg, uint8_t &MsgIndex)
 Find index for free space for a message on N2kCANMsgBuf. More...
 
bool ForwardEnabled () const
 Is message forwarding enabled. More...
 
void ForwardMessage (const tN2kCANMsg &N2kCanMsg)
 Forwards a N2k message. More...
 
void ForwardMessage (const tN2kMsg &N2kMsg)
 Forwards a N2k message. More...
 
bool ForwardOnlyKnownMessages () const
 Is forwarding enabled for known messages only. More...
 
bool ForwardOwnMessages () const
 Is forwarding enabled for own messages. More...
 
bool ForwardSystemMessages () const
 Is forwarding enabled for system messages. More...
 
size_t GetFastPacketTxPGNCount (int iDev)
 Get the Fast Packet Tx PGN Count. More...
 
void GetNextAddress (int DeviceIndex, bool RestartAtEnd=false)
 Get the next free address for the device. More...
 
tCANSendFrameGetNextFreeCANSendFrame ()
 Get the Next Free CAN Frame from CANSendFrameBuf. More...
 
int GetSequenceCounter (unsigned long PGN, int iDev)
 Get the Sequence Counter for the PGN. More...
 
void HandleCommandedAddress (const tN2kMsg &N2kMsg)
 Handles if we get commanded to set a new address. More...
 
void HandleCommandedAddress (uint64_t CommandedName, unsigned char NewAddress, int iDev)
 Handles if we get commanded to set a new address. More...
 
void HandleGroupFunction (const tN2kMsg &N2kMsg)
 Handles a Group Function. More...
 
void HandleISOAddressClaim (const tN2kMsg &N2kMsg)
 Handles an IsoAddressClaim. More...
 
void HandleISORequest (const tN2kMsg &N2kMsg)
 Handles an ISO Request. More...
 
bool HandleOnlyKnownMessages () const
 Is handle only known messages enabled. More...
 
bool HandleReceivedMessage (unsigned char Destination)
 Should received message be handled depending on the destination of the received message. More...
 
bool HandleReceivedSystemMessage (int MsgIndex)
 Handles a received system message. More...
 
bool HasAllTPDTSent (int iDev)
 Check if all data bytes of the multi packet message has been send successful. More...
 
virtual void InitCANFrameBuffers ()
 Initialize CAN Frame buffers. More...
 
void InitDevices ()
 Initialize all devices. More...
 
bool IsActiveNode ()
 Returns if this node is active on the bus. More...
 
bool IsAddressClaimStarted (int iDev)
 Checks if the IsoAddressClaim is already started. More...
 
bool IsFastPacket (const tN2kMsg &N2kMsg)
 Check if this PNG is a fast packet message. More...
 
bool IsFastPacketPGN (unsigned long PGN)
 Check if this PNG is a fast packet message. More...
 
bool IsInitialized ()
 Determines if the CAN BUS is already initialized. More...
 
bool IsMySource (unsigned char Source)
 Checks if the source belongs to a device on Devices. More...
 
bool IsReadyToSend () const
 Checks if the device is ready to start address claiming. More...
 
bool IsValidDevice (int iDev) const
 Checks if the device index on Devices is valid. More...
 
void RespondGroupFunction (const tN2kMsg &N2kMsg, tN2kGroupFunctionCode GroupFunctionCode, unsigned long PGNForGroupFunction, int iDev)
 Respond to an Group Function. More...
 
void RespondISORequest (const tN2kMsg &N2kMsg, unsigned long RequestedPGN, int iDev)
 Respond to an ISO request. More...
 
void RunMessageHandlers (const tN2kMsg &N2kMsg)
 Run all message handlers. More...
 
bool SendFrame (unsigned long id, unsigned char len, const unsigned char *buf, bool wait_sent=true)
 Sends a single CAN frame. More...
 
bool SendFrames ()
 Sends pending all frames. More...
 
void SendPendingInformation ()
 Send ISO AddressClaim, Product Information and Config Information. More...
 
void SendPendingTPMessage (int iDev)
 Send pending ISO-TP Messages. More...
 
void SendTPCM_Abort (unsigned long PGN, unsigned char Destination, int iDev, unsigned char AbortCode)
 Send ISO Transport Protocol message Abort. More...
 
bool SendTPCM_BAM (int iDev)
 Send ISO Transport Protocol message BAM. More...
 
void SendTPCM_CTS (unsigned long PGN, unsigned char Destination, int iDev, unsigned char nPackets, unsigned char NextPacketNumber)
 Send ISO Transport Protocol message CTS. More...
 
void SendTPCM_EndAck (unsigned long PGN, unsigned char Destination, int iDev, uint16_t nBytes, unsigned char nPackets)
 Send ISO Transport Protocol message End Acknowledge. More...
 
bool SendTPCM_RTS (int iDev)
 Send ISO Transport Protocol message RTS. More...
 
bool SendTPDT (int iDev)
 Send ISO Transport Protocol data packet. More...
 
uint8_t SetN2kCANBufMsg (unsigned long canId, unsigned char len, unsigned char *buf)
 Function handles received CAN frame and adds it to tN2kCANMsg. More...
 
void StartAddressClaim ()
 Starting the ISO Address Claim for all devices. More...
 
void StartAddressClaim (int iDev)
 Starting the ISO Address Claim for a device. More...
 
bool StartSendTPMessage (const tN2kMsg &msg, int iDev)
 Start sending an ISO-TP message. More...
 
bool TestHandleTPMessage (unsigned long PGN, unsigned char Source, unsigned char Destination, unsigned char len, unsigned char *buf, uint8_t &MsgIndex)
 ISO Transport Protocol handlers for multi packet support. More...
 

Protected Attributes

bool AddressChanged
 Flag that the address has changed. More...
 
tCANSendFrameCANSendFrameBuf
 Buffer for library send out CAN frames. More...
 
uint16_t CANSendFrameBufferRead
 Next write index for the library CAN send frame buffer. More...
 
uint16_t CANSendFrameBufferWrite
 Next read index for the library CAN send frame buffer. More...
 
tConfigurationInformation ConfigurationInformation
 Configuration Information of the device. More...
 
tDebugMode dbMode
 Attribute that holds the actual Debug Mode (default = md_none) More...
 
int DeviceCount
 Number of devices. More...
 
bool DeviceInformationChanged
 Flag that the device information has changed. More...
 
tInternalDeviceDevices
 Pointer to a buffer for all internal devices. More...
 
const unsigned long * FastPacketMessages [N2kMessageGroups]
 
unsigned int ForwardMode
 Actual message forward operation mode (default = all messages - also system and own) More...
 
N2kStreamForwardStream
 Actual stream to be used for forward messaging. More...
 
tForwardType ForwardType
 Actual message forward type (default = fwdt_Actisense) More...
 
bool InstallationDescriptionChanged
 Flag the Installation description has changed. More...
 
bool(* ISORqstHandler )(unsigned long RequestedPGN, unsigned char Requester, int DeviceIndex)
 Handler callbacks for 'ISORequest' messages. More...
 
char * LocalConfigurationInformationData
 Pointer to a buffer for local Configuration Information. More...
 
uint16_t MaxCANReceiveFrames
 Max number received CAN messages that can go to the buffer. More...
 
uint16_t MaxCANSendFrames
 Size of CANSendFrameBuf or before initialization requested total frame buffering size. More...
 
uint8_t MaxN2kCANMsgs
 Size of N2kCANMsgBuf receiving message buffer. More...
 
void(* MsgHandler )(const tN2kMsg &N2kMsg)
 Handler callbacks for normal messages. More...
 
tMsgHandlerMsgHandlers
 Pointer to a buffer for Message Handlers. More...
 
tN2kCANMsgN2kCANMsgBuf
 Buffer for receiving messages. More...
 
tN2kMode N2kMode
 Actual operation mode of this device (default = N2km_ListenOnly) More...
 
void(* OnOpen )()
 Callback function, which will be called when library start bus communication. More...
 
tN2kScheduler OpenScheduler
 
tOpenState OpenState
 
tN2kGroupFunctionHandlerpGroupFunctionHandlers
 Pointer to Buffer for GRoup Function Handlers. More...
 
const unsigned long * SingleFrameMessages [N2kMessageGroups]
 

Static Protected Attributes

static const int FwdModeBit_EnableForward = BIT(0)
 Forward mode bit: -> If set, forward is enabled. More...
 
static const int FwdModeBit_OnlyKnownMessages = BIT(2)
 Forward mode bit: -> Only known messages will be forwarded. System messages will be forwarded according its own bit. More...
 
static const int FwdModeBit_OwnMessages = BIT(3)
 Forward mode bit: -> Forward also all messages, what this device will send. More...
 
static const int FwdModeBit_SystemMessages = BIT(1)
 Forward mode bit: -> System messages will be forwarded. More...
 
static const int HandleModeBit_OnlyKnownMessages = BIT(4)
 Forward mode bit: -> Only known messages will be handled. More...
 

Detailed Description

tNMEA2000 device class definition.

With tNMEA2000 class you can easily communicate with NMEA2000 bus. Library can be used for any kind of NMEA2000 bus device needs from bus traffic listener to complex MFD system.

As default library simply reads all messages from bus and forwards them to defined forward stream (e.g., Serial) in Actisense format. Using N2km_ListenAndNode mode, one can make NMEA2000 compatible devices and even NMEA2000 certified devices by implementing all NMEA2000 certification requirements. Simple example is bus device, which provides some sensor like temperature, battery or engine information to the bus to be shown or MFD.

Class can be used just for reading bus data. More common is to use it as bus device (also called node). For bus device mode you provide required basic information about device to class, which then takes care of informing other devices on the bus in standard way. For other message handling class provides callback methods and method to send messages.

Each device on NMEA2000 bus will have own source address on range 0-251. Class takes care of address claiming (also called dynamic addressing) without any developer interaction. Class also provides multi device support, if that is necessary.

Definition at line 137 of file NMEA2000.h.

Member Enumeration Documentation

◆ tDebugMode

For debugging we have some cases for SendMsg.

Enumerator
dm_None 

Directs data to CAN bus

dm_ClearText 

Directs sent data to serial in clear text format

dm_Actisense 

Directs sent data to serial as Actisense format.

Definition at line 718 of file NMEA2000.h.

◆ tForwardType

Type how to forward messages in listen mode.

Enumerator
fwdt_Actisense 

Forwards messages to output port in Actisense format. Note that some Navigation sw uses this.

fwdt_Text 

Forwards messages to output port in clear text. This is e.g. for debugging.

Definition at line 664 of file NMEA2000.h.

◆ tN2kMode

System mode defines how the device will behave on the NMEA2000 bus.

See also
Enumerator
N2km_ListenOnly 

Default mode. Listen bus and if message forwarding has been enabled forwards messages to defined stream in Actisense format. You can not send any data to the bus. See example DataDisplay.ino.

N2km_NodeOnly 

In this mode device acts as active bus device like display, RPM or temperature monitor. Remember to set right device information first. Messages will not be forwarded. Look example TemperatureMonitor.ino.

N2km_ListenAndNode 

Mode is as N2km_NodeOnly plus you can control message forwarding.

N2km_SendOnly 

Only for message sending. Device will not inform itself to the bus. Messages will not be forwarded to the stream.
By setting message handler, you can still read messages and handle them by yourself.

N2km_ListenAndSend 

Listen bus and forwards messages to given forward stream in Actisense format. Messages can be send. Device will not inform itself to the bus. This is mode is usable e.g., with NMEA Simulator http://www.kave.fi/Apps/.

Definition at line 683 of file NMEA2000.h.

◆ tOpenState

enum tNMEA2000::tOpenState
protected

Library open state.

Open state is internal library open state used to avoid any blocking call.

Enumerator
os_None 

State none.

Open has not yet been called. Buffers are still uninitialized. Trying to open CAN driver.

os_OpenCAN 

State Open CAN.

CAN driver open succeed. Wait CAN driver stabilize.

os_WaitOpen 

State Wait Open.

Open and running.

os_Open 

State Open.

Definition at line 751 of file NMEA2000.h.

Constructor & Destructor Documentation

◆ tNMEA2000()

tNMEA2000::tNMEA2000 ( )

Construct a new NMEA2000 object.

Initialize all the attributes of the class

Definition at line 660 of file NMEA2000.cpp.

Member Function Documentation

◆ AddGroupFunctionHandler()

void tNMEA2000::AddGroupFunctionHandler ( tN2kGroupFunctionHandler pGroupFunctionHandler)

Add a message handler for incoming Group Function messages.

Parameters
pGroupFunctionHandlerMessage handler for group functions, see tN2kGroupFunctionHandler

Definition at line 2708 of file NMEA2000.cpp.

◆ AttachMsgHandler()

void tNMEA2000::AttachMsgHandler ( tMsgHandler _MsgHandler)

Attach a message handler for incoming N2kMessages.

tNMEA2000::SetMsgHandler allows you to define only one handler to your system. If you like to do it by using classes, I prefer to use AttachMsgHandler. In this way you can e.g. define own class for each PGN and attach/detach them within your program.

Example NMEA2000ToNMEA0183 uses AttachMsgHandler. Due to logic it still has single class and so handles all PGNs.

See also
Parameters
_MsgHandlerMessage handler

Definition at line 2643 of file NMEA2000.cpp.

◆ CANGetFrame()

virtual bool tNMEA2000::CANGetFrame ( unsigned long &  id,
unsigned char &  len,
unsigned char *  buf 
)
protectedpure virtual

Abstract class for reading frame from driver class.

Driver writer must override this function for specific CAN interface. Currently there are driver classes like NMEA2000_Teensyx, NMEA2000_teensy, NMEA2000_esp32, NMEA2000_due, NMEA2000_mcp, NMEA2000_avr, NMEA2000_mbed and NMEA2000_socketCAN.

See also
Hardware depended libraries
Return values
trueNew frame read from buffer.
falseNothing read.

◆ CANOpen()

virtual bool tNMEA2000::CANOpen ( )
protectedpure virtual

Abstract class for initializing and opening CAN interface.

Driver writer must override this function for specific CAN interface. Currently there are driver classes like NMEA2000_Teensyx, NMEA2000_teensy, NMEA2000_esp32, NMEA2000_due, NMEA2000_mcp, NMEA2000_avr, NMEA2000_mbed and NMEA2000_socketCAN.

See also
Hardware depended libraries
Return values
trueInitialize and open success
falseInitialize or open failed.

◆ CANSendFrame()

virtual bool tNMEA2000::CANSendFrame ( unsigned long  id,
unsigned char  len,
const unsigned char *  buf,
bool  wait_sent = true 
)
protectedpure virtual

Abstract class for sending a CAN Frame.

Driver writer must override this function for specific CAN interface. Currently there are driver classes like NMEA2000_Teensyx, NMEA2000_teensy, NMEA2000_esp32, NMEA2000_due, NMEA2000_mcp, NMEA2000_avr, NMEA2000_mbed and NMEA2000_socketCAN.

See also
Hardware depended libraries
Parameters
idID of the CAN frame
lenlength of payload for the message
bufbuffer with the payload
wait_sentHas the message to wait before sending
Return values
trueSuccess
falsethere is no space in the queue

◆ CheckKnownMessage()

bool tNMEA2000::CheckKnownMessage ( unsigned long  PGN,
bool &  SystemMessage,
bool &  FastPacket 
)
protected

Check if this Message is known to the system.

Determines whether this message is known to the system, either by being a default, mandatory or system message or this specific message which is listed in SingleFrameMessages \ FastPacketMessages.

See also
IsDefaultSingleFrameMessage, IsMandatoryFastPacketMessage, IsDefaultFastPacketMessage, IsSingleFrameSystemMessage, IsFastPacketSystemMessage,
Parameters
PGNPGN to be checked
SystemMessageFlag system message
FastPacketFlag fast packet
Return values
true
false

Definition at line 1579 of file NMEA2000.cpp.

◆ ClearCharBuf()

void tNMEA2000::ClearCharBuf ( size_t  MaxLen,
char *  buf 
)
static

Clears a char array buffer with 0s.

Parameters
MaxLenMax length of the buffer
bufPointer to the buffer

Definition at line 637 of file NMEA2000.cpp.

◆ ClearSetCharBuf()

void tNMEA2000::ClearSetCharBuf ( const char *  str,
size_t  MaxLen,
char *  buf 
)
static

Setting up a clean Char Buffer.

This functions clears an existing buffer using ClearCharBuf, copies the a string to the char buffer and terminates it with 0.

Parameters
strString
MaxLenMax length of the buffer
bufPointer to the buffer

Definition at line 654 of file NMEA2000.cpp.

◆ CopyProgmemConfigurationInformationToLocal()

void tNMEA2000::CopyProgmemConfigurationInformationToLocal ( )
protected

Copy Configuration Information to local memory.

See also

Definition at line 1022 of file NMEA2000.cpp.

◆ DetachMsgHandler()

void tNMEA2000::DetachMsgHandler ( tMsgHandler _MsgHandler)

Detach a message handler for incoming N2kMessages.

With DetachMsgHandler you can remove your handler from the handler stack. This is useful, if you do not want to handle some messages anymore.

See also
Parameters
_MsgHandlerMessage handler

Definition at line 2668 of file NMEA2000.cpp.

◆ EnableForward()

void tNMEA2000::EnableForward ( bool  v = true)
inline

Enable message forwarding to stream.

Set true as default. With this you can control if bus messages will be forwarded to forward stream.

See also
Message forwarding.
Parameters
vEnable, default = true

Definition at line 2900 of file NMEA2000.h.

◆ EndSendTPMessage()

void tNMEA2000::EndSendTPMessage ( int  iDev)
protected

Ends sending of ISO-TP message.

Parameters
iDevindex of the device on Devices

Definition at line 1949 of file NMEA2000.cpp.

◆ ExtendFastPacketMessages()

void tNMEA2000::ExtendFastPacketMessages ( const unsigned long *  _FastPacketMessages)

Set the list of known Extended Fast Packet Messages.

Call these if you wish to extent list of fast packet messages. Pointers must be in PROGMEM

As a default library has a list of known messages. With this function user can extent default list of fast packet messages.

Note
If an incoming fast packet message is not known, it will be treated as single frame message. So if you want to handle unknown fast packet message, you need to duplicate frame collection logic from library to your code. So it is easier to have fast packet messages listed on library, if you want to handle them.
Currently subsequent calls will override previously set list.
See also
Parameters
_FastPacketMessagesBuffer holding extended fast packet messages

Definition at line 1158 of file NMEA2000.cpp.

◆ ExtendReceiveMessages()

void tNMEA2000::ExtendReceiveMessages ( const unsigned long *  _ReceiveMessages,
int  iDev = 0 
)

Extend the list of Received Messages.

Define list of extra PGNs, what your system will handle. System messages will be automatically included.
Pointers must be in PROGMEM.

const unsigned ReceivedMessages[] PROGMEM={130310L,130311L,130312L,0};
NMEA2000.ExtendReceiveMessages(ReceivedMessages);
#define PROGMEM
Definition: N2kDef.h:45
tNMEA2000 & NMEA2000
Definition: NMEA2000_CAN.h:350
void ExtendReceiveMessages(const unsigned long *_ReceiveMessages, int iDev=0)
Extend the list of Received Messages.
Definition: NMEA2000.cpp:1170

Library responds automatically to PGN 126464 request about transmit or receive messages. With this function you extend library list of messages your device own logic listens.

Note
Extending receive messages is required for NMEA2000 certification.

This has only effect for device modes tNMEA2000::N2km_NodeOnly and tNMEA2000::N2km_ListenAndNode.

Parameters
_ReceiveMessagesBuffer holding list of extra PGNs device will handle.
iDevindex of the device on Devices

Definition at line 1170 of file NMEA2000.cpp.

◆ ExtendSingleFrameMessages()

void tNMEA2000::ExtendSingleFrameMessages ( const unsigned long *  _SingleFrameMessages)

Set the list of known Extended Single Frame Messages.

Call these if you wish to add own list of supported message packets. Pointers must be in PROGMEM

As a default library has a list of known messages. With this function user can add own list of known single frame messages.

Known single frame message lists has only effect if you limit handling with tNMEA2000::HandleOnlyKnownMessages or use message forwarding and limit it with tNMEA2000::SetForwardOnlyKnownMessages

Note
Currently subsequent calls will override previously set list.
See also
Parameters
_SingleFrameMessagesBuffer holding single frame messages

Definition at line 1153 of file NMEA2000.cpp.

◆ ExtendTransmitMessages()

void tNMEA2000::ExtendTransmitMessages ( const unsigned long *  _TransmitMessages,
int  iDev = 0 
)

Extend the list of Transmitted Messages.

Define list of extra PGNs, what your system will transmit. System messages will be automatically included. Pointers must be in PROGMEM

const unsigned TransmitMessages[] PROGMEM={130310L,130311L,130312L,0};
const unsigned long TransmitMessages[]
void ExtendTransmitMessages(const unsigned long *_TransmitMessages, int iDev=0)
Extend the list of Transmitted Messages.
Definition: NMEA2000.cpp:1163

Library responds automatically to PGN 126464 request about transmit or receive messages. With this function you extend library list of messages your device own logic sends.

Note
Extending transmit messages is required for NMEA2000 certification, may be also critical since some devices refuses to handle PGNs from devices, which does not list them on transmit messages.

This has only effect for device modes tNMEA2000::N2km_NodeOnly and tNMEA2000::N2km_ListenAndNode.

Parameters
_TransmitMessagesBuffer holding list of extra PGNs device will transmit.
iDevindex of the device on Devices

Definition at line 1163 of file NMEA2000.cpp.

◆ FindFreeCANMsgIndex()

void tNMEA2000::FindFreeCANMsgIndex ( unsigned long  PGN,
unsigned char  Source,
unsigned char  Destination,
bool  TPMsg,
uint8_t &  MsgIndex 
)
protected

Find index for free space for a message on N2kCANMsgBuf.

This functions searches for free space at N2kCANMsgBuf. If there is free space in buffer or there is a message with the same, source, PGN and destination at the buffer the corresponding Index is returned. If this is not the case, the Index of the oldest message inside the buffer is returned.

Parameters
PGNPNG for the message
SourceSource address of the message
DestinationDestination of the message
TPMsgMessage is Multi Packet message
MsgIndexIndex

Definition at line 1634 of file NMEA2000.cpp.

◆ FindSourceDeviceIndex()

int tNMEA2000::FindSourceDeviceIndex ( unsigned char  Source) const

Finds a device on Devices by its source address.

Parameters
SourceSource address of the device
Returns
int DeviceIndex, not found = -1

Definition at line 2062 of file NMEA2000.cpp.

◆ ForwardEnabled()

bool tNMEA2000::ForwardEnabled ( ) const
inlineprotected

Is message forwarding enabled.

See also

Checks if forwarding i enabled and the node is not N2km_SendOnly

Return values
true
false

Definition at line 1520 of file NMEA2000.h.

◆ ForwardMessage() [1/2]

void tNMEA2000::ForwardMessage ( const tN2kCANMsg N2kCanMsg)
protected

Forwards a N2k message.

This function forwards a n2k message according to the forward type ForwardType( tForwardType) to the correct ForwardStream.

Parameters
N2kCanMsgN2k CAN message object, see tN2kCANMsg

Definition at line 2092 of file NMEA2000.cpp.

◆ ForwardMessage() [2/2]

void tNMEA2000::ForwardMessage ( const tN2kMsg N2kMsg)
protected

Forwards a N2k message.

This function forwards a n2k message according to the forward type ForwardType( tForwardType) to the correct ForwardStream.

Parameters
N2kMsgN2k message object

Definition at line 2078 of file NMEA2000.cpp.

◆ ForwardOnlyKnownMessages()

bool tNMEA2000::ForwardOnlyKnownMessages ( ) const
inlineprotected

Is forwarding enabled for known messages only.

See also
ForwardMode
Return values
true
false

Definition at line 1536 of file NMEA2000.h.

◆ ForwardOwnMessages()

bool tNMEA2000::ForwardOwnMessages ( ) const
inlineprotected

Is forwarding enabled for own messages.

See also
ForwardMode
Return values
true
false

Definition at line 1544 of file NMEA2000.h.

◆ ForwardSystemMessages()

bool tNMEA2000::ForwardSystemMessages ( ) const
inlineprotected

Is forwarding enabled for system messages.

See also
ForwardMode
Return values
true
false

Definition at line 1528 of file NMEA2000.h.

◆ GetCertificationLevel()

unsigned char tNMEA2000::GetCertificationLevel ( int  iDev = 0) const

Get the Certification Level of the device.

Parameters
iDevindex of the device on Devices
Returns
unsigned char

Definition at line 994 of file NMEA2000.cpp.

◆ GetDeviceInformation()

const tDeviceInformation tNMEA2000::GetDeviceInformation ( int  iDev = 0)
inline

Get the Device Information.

With this function you can read current device information. Normally device information contains what you have set during initializing with tNMEA2000::SetDeviceInformation and tNMEA2000::SetDeviceInformationInstances functions.

Note
Device information instances can be changed by the NMEA 2000 group function by e.g. using system configuration device. So you should time to time check if they have changed and save changed data to e.g. EEPROM for use on startup.
See also
Parameters
iDevindex of the device on Devices
Returns
const tDeviceInformation

Definition at line 2386 of file NMEA2000.h.

◆ GetFastPacketTxPGNCount()

size_t tNMEA2000::GetFastPacketTxPGNCount ( int  iDev)
protected

Get the Fast Packet Tx PGN Count.

Parameters
iDevindex of the device on Devices
Returns
size_t

Definition at line 813 of file NMEA2000.cpp.

◆ GetForwardStream()

N2kStream * tNMEA2000::GetForwardStream ( ) const
inline

Read current Forward Stream object.

Returns
current Forward Stream object or 0 if not set.

Definition at line 2610 of file NMEA2000.h.

◆ GetHeartbeatInterval()

uint32_t tNMEA2000::GetHeartbeatInterval ( int  iDev = 0)
inline

Get the Heartbeat Interval of a device.

Heartbeat interval may be changed by e.g. MFD by group function. I have not yet found should changed value be saved for next startup or not.

Parameters
iDevindex of the device on Devices
Return values
uint_32Device heartbeat interval in ms

Definition at line 2513 of file NMEA2000.h.

◆ GetHeartbeatOffset()

uint32_t tNMEA2000::GetHeartbeatOffset ( int  iDev = 0)
inline

Get the Heartbeat Offset of a device.

Heartbeat Offset may be changed by e.g. MFD by group function. I have not yet found should changed value be saved for next startup or not.

Parameters
iDevindex of the device on Devices
Return values
uint_32Device heartbeat Offset in ms

Definition at line 2524 of file NMEA2000.h.

◆ GetInstallationDescription1()

void tNMEA2000::GetInstallationDescription1 ( char *  buf,
size_t  max_len 
)

Get the Install Description 1 of this device.

Parameters
bufBuffer in memory for the description
max_lenMax size of the buffer

Definition at line 1056 of file NMEA2000.cpp.

◆ GetInstallationDescription2()

void tNMEA2000::GetInstallationDescription2 ( char *  buf,
size_t  max_len 
)

Get the Install Description 2 of this device.

Parameters
bufBuffer in memory for the description
max_lenMax size of the buffer

Definition at line 1065 of file NMEA2000.cpp.

◆ GetLoadEquivalency()

unsigned char tNMEA2000::GetLoadEquivalency ( int  iDev = 0) const

Get the Load Equivalency of this device.

A Load Equivalence Number express the amount of current that is drawn from an NMEA 2000 network. 1 equals to 50mA. If a device draws 151mA of current from the network, then its LEN is 4

Parameters
iDevindex of the device on Devices
Returns
unsigned char

Definition at line 1008 of file NMEA2000.cpp.

◆ GetManufacturerInformation()

void tNMEA2000::GetManufacturerInformation ( char *  buf,
size_t  max_len 
)

Get the Manufacturer Information of this device.

Parameters
bufBuffer in memory for the description
max_lenMax size of the buffer

Definition at line 1074 of file NMEA2000.cpp.

◆ GetModelID()

void tNMEA2000::GetModelID ( char *  buf,
size_t  max_len,
int  iDev = 0 
) const

Get the ModelID of the device.

Parameters
bufBuffer to hold the information
max_lenMaximum size of the buffer
iDevindex of the device on Devices

Definition at line 930 of file NMEA2000.cpp.

◆ GetModelSerialCode()

void tNMEA2000::GetModelSerialCode ( char *  buf,
size_t  max_len,
int  iDev = 0 
) const

Get the Model Serial of the device.

Parameters
bufBuffer to hold the information
max_lenMaximum size of the buffer
iDevindex of the device on Devices

Definition at line 978 of file NMEA2000.cpp.

◆ GetModelVersion()

void tNMEA2000::GetModelVersion ( char *  buf,
size_t  max_len,
int  iDev = 0 
) const

Get the Model Version of the device.

Parameters
bufBuffer to hold the information
max_lenMaximum size of the buffer
iDevindex of the device on Devices

Definition at line 962 of file NMEA2000.cpp.

◆ GetN2kSource()

unsigned char tNMEA2000::GetN2kSource ( int  DeviceIndex = 0) const
inline

Read address for current device.

With this function you can get you device current source address on the N2k bus.

See also
Parameters
DeviceIndexindex of the device on Devices
Returns
unsigned char -> Address of the device

Definition at line 2827 of file NMEA2000.h.

◆ GetN2kVersion()

unsigned short tNMEA2000::GetN2kVersion ( int  iDev = 0) const

Get the N2k standard version of the device.

Parameters
iDevindex of the device on Devices
Returns
unsigned short

Definition at line 902 of file NMEA2000.cpp.

◆ GetNextAddress()

void tNMEA2000::GetNextAddress ( int  DeviceIndex,
bool  RestartAtEnd = false 
)
protected

Get the next free address for the device.

Parameters
DeviceIndexindex of the device on Devices
RestartAtEndRestart the search from the beginning if the search for the free source has reached N2kNullCanBusAddress

Definition at line 2512 of file NMEA2000.cpp.

◆ GetNextFreeCANSendFrame()

tNMEA2000::tCANSendFrame * tNMEA2000::GetNextFreeCANSendFrame ( )
protected

Get the Next Free CAN Frame from CANSendFrameBuf.

Returns
tCANSendFrame*

Definition at line 1417 of file NMEA2000.cpp.

◆ GetProductCode()

unsigned short tNMEA2000::GetProductCode ( int  iDev = 0) const

Get the Product Code of the device.

Parameters
iDevindex of the device on Devices
Returns
unsigned short

Definition at line 916 of file NMEA2000.cpp.

◆ GetProductInformation()

const tNMEA2000::tProductInformation * tNMEA2000::GetProductInformation ( int  iDev,
bool &  IsProgMem 
) const

Get the Product Information of the device.

Parameters
iDevindex of the device on Devices
IsProgMemProgram memory has been used for the data
Returns
const tNMEA2000::tProductInformation*

Definition at line 891 of file NMEA2000.cpp.

◆ GetSequenceCounter()

int tNMEA2000::GetSequenceCounter ( unsigned long  PGN,
int  iDev 
)
protected

Get the Sequence Counter for the PGN.

Parameters
PGNPGN
iDevindex of the device on Devices
Returns
int Sequence Counter, non valid iDEV = 0

Definition at line 831 of file NMEA2000.cpp.

◆ GetSwCode()

void tNMEA2000::GetSwCode ( char *  buf,
size_t  max_len,
int  iDev = 0 
) const

Get the Sw Code of the device.

Parameters
bufBuffer to hold the information
max_lenMaximum size of the buffer
iDevindex of the device on Devices

Definition at line 946 of file NMEA2000.cpp.

◆ HandleCommandedAddress() [1/2]

void tNMEA2000::HandleCommandedAddress ( const tN2kMsg N2kMsg)
protected

Handles if we get commanded to set a new address.

Parameters
N2kMsgReference to a N2kMsg Object

Definition at line 2467 of file NMEA2000.cpp.

◆ HandleCommandedAddress() [2/2]

void tNMEA2000::HandleCommandedAddress ( uint64_t  CommandedName,
unsigned char  NewAddress,
int  iDev 
)
protected

Handles if we get commanded to set a new address.

Parameters
CommandedNameDevice name that have been commanded to set new address
NewAddressnew address for the device
iDevindex of the device on Devices

Definition at line 2455 of file NMEA2000.cpp.

◆ HandleGroupFunction()

void tNMEA2000::HandleGroupFunction ( const tN2kMsg N2kMsg)
protected

Handles a Group Function.

Document https://web.archive.org/web/20170609033039/http://www.nmea.org/Assets/20140109%20nmea-2000-corrigendum-tc201401031%20pgn%20126208.pdf defines that systems should respond to NMEA Request/Command/Acknowledge group function PGN 126208. On the document it is not clear can request be send as broadcast, so we handle it, if we can.

Parameters
N2kMsgReference to a N2kMsg Object

Definition at line 2375 of file NMEA2000.cpp.

◆ HandleISOAddressClaim()

void tNMEA2000::HandleISOAddressClaim ( const tN2kMsg N2kMsg)
protected

Handles an IsoAddressClaim.

Parameters
N2kMsgReference to a N2kMsg Object

Definition at line 2430 of file NMEA2000.cpp.

◆ HandleISORequest()

void tNMEA2000::HandleISORequest ( const tN2kMsg N2kMsg)
protected

Handles an ISO Request.

The function determines if the request is for us (Broadcast message or device in our list) and responds to the request.

Parameters
N2kMsgReference to a N2kMsg Object

Definition at line 2333 of file NMEA2000.cpp.

◆ HandleOnlyKnownMessages()

bool tNMEA2000::HandleOnlyKnownMessages ( ) const
inlineprotected

Is handle only known messages enabled.

Return values
true
false

Definition at line 1552 of file NMEA2000.h.

◆ HandleReceivedMessage()

bool tNMEA2000::HandleReceivedMessage ( unsigned char  Destination)
inlineprotected

Should received message be handled depending on the destination of the received message.

Note
By default this message always returns true, because we handle messages to any destination.
Parameters
DestinationDestination of the received message
Return values
true
false

Definition at line 1572 of file NMEA2000.h.

◆ HandleReceivedSystemMessage()

bool tNMEA2000::HandleReceivedSystemMessage ( int  MsgIndex)
protected

Handles a received system message.

If the node is not N2km_SendOnly or N2km_ListenAndSend this function chooses the correct handler for the given system message.

See also
Parameters
MsgIndexMessage Index on N2kCANMsgBuf
Return values
truemessage was handled
false

Definition at line 2543 of file NMEA2000.cpp.

◆ HasAllTPDTSent()

bool tNMEA2000::HasAllTPDTSent ( int  iDev)
protected

Check if all data bytes of the multi packet message has been send successful.

Parameters
iDevindex of the device on Devices
Return values
true
false

Definition at line 1781 of file NMEA2000.cpp.

◆ InitCANFrameBuffers()

void tNMEA2000::InitCANFrameBuffers ( )
protectedvirtual

Initialize CAN Frame buffers.

This will be called on tNMEA2000::Open() before any other initialization.

Driver writer must inherit this for low level driver class and create driver internal send and receive buffers according requested MaxCANSendFrames and MaxCANReceiveFrames sizes. Driver class can handle completely send and receive buffers or after buffers has been initialized set MaxCANSendFrames for size of library buffer and call tNMEA2000::InitCANFrameBuffers();

Inherited function can also override requested MaxCANSendFrames and MaxCANReceiveFrames in case they are set e.g., too small or too large.

See e.g., tNMEA2000_Teensyx::InitCANFrameBuffers() on NMEA2000_teensyx.cpp.

See also

Definition at line 1188 of file NMEA2000.cpp.

◆ InitDevices()

void tNMEA2000::InitDevices ( )
protected

Initialize all devices.

This function initializes the array Devices with the size of DeviceCount and sets default device information.

Default Device Info is:

  • Unique ID = Device Index +1 ==> 21 bit resolution, max 2097151. Each device from same manufacturer should have unique number.
  • Device Function = 130 ==> PC Gateway
  • Device Class = 25 ==> Inter/Intranetwork Device.
  • Manufacturer Code = 2046 ==> Maximum 2046
  • Industry Group Code = 4 ==> Marine
See also

Definition at line 710 of file NMEA2000.cpp.

◆ IsActiveNode()

bool tNMEA2000::IsActiveNode ( )
inlineprotected

Returns if this node is active on the bus.

Return values
trueif ( N2km_NodeOnly or N2km_ListenAndNode)
false

Definition at line 1584 of file NMEA2000.h.

◆ IsAddressClaimStarted()

bool tNMEA2000::IsAddressClaimStarted ( int  iDev)
protected

Checks if the IsoAddressClaim is already started.

Parameters
iDevindex of the device on Devices
Return values
true
false

Definition at line 2414 of file NMEA2000.cpp.

◆ IsBroadcast()

static bool tNMEA2000::IsBroadcast ( unsigned char  Source)
inlinestatic

Checks if the given Address is a broadcast address.

Parameters
SourceSource address
Return values
trueSource is broadcast address
falseSource is not broadcast address

Definition at line 2990 of file NMEA2000.h.

◆ IsFastPacket()

bool tNMEA2000::IsFastPacket ( const tN2kMsg N2kMsg)
protected

Check if this PNG is a fast packet message.

Determines if this given PGN belongs to a fast packet message by checking if a corresponding message is listed on FastPacketMessages[]

Parameters
N2kMsgReference to a N2kMsg Object
Return values
true
false

Definition at line 1572 of file NMEA2000.cpp.

◆ IsFastPacketPGN()

bool tNMEA2000::IsFastPacketPGN ( unsigned long  PGN)
protected

Check if this PNG is a fast packet message.

Determines if this given PGN belongs to a fast packet message by checking if a corresponding message is listed on FastPacketMessages[]

Parameters
PGNPGN to be checked
Return values
true
false

Definition at line 1552 of file NMEA2000.cpp.

◆ IsInitialized()

bool tNMEA2000::IsInitialized ( )
inlineprotected

Determines if the CAN BUS is already initialized.

Return values
true
false

Definition at line 1234 of file NMEA2000.h.

◆ IsMySource()

bool tNMEA2000::IsMySource ( unsigned char  Source)
protected

Checks if the source belongs to a device on Devices.

Parameters
SourceSource address
Return values
true
false

Definition at line 2073 of file NMEA2000.cpp.

◆ IsOpen()

bool tNMEA2000::IsOpen ( ) const
inline

Test is NMEA2000 open and running.

Return values
true
false

Definition at line 2634 of file NMEA2000.h.

◆ IsProprietaryMessage()

bool tNMEA2000::IsProprietaryMessage ( unsigned long  PGN)
static

Check if the given PGN is proprietary.

PGNs that are proprietary by definition: 61184, 65280 - 65535, 126720, 130816 - 131071

Parameters
PGNPGN to be checked
Return values
true-> for PGNs:
  • 126720L
  • 130816L ... 131071L
  • 65280L ... 65535L
false

Definition at line 583 of file NMEA2000.cpp.

◆ IsReadyToSend()

bool tNMEA2000::IsReadyToSend ( ) const
inlineprotected

Checks if the device is ready to start address claiming.

Return values
true
false

Definition at line 1601 of file NMEA2000.h.

◆ IsTxPGN()

bool tNMEA2000::IsTxPGN ( unsigned long  PGN,
int  iDev = 0 
)

Check if this message is a Transmit message of this device.

Parameters
PGNPGN to be checked
iDevindex of the device on Devices
Return values
true
false

Definition at line 875 of file NMEA2000.cpp.

◆ IsValidDevice()

bool tNMEA2000::IsValidDevice ( int  iDev) const
inlineprotected

Checks if the device index on Devices is valid.

Parameters
iDevindex of the device on Devices
Return values
true
false

Definition at line 1593 of file NMEA2000.h.

◆ N2kMax()

template<typename T >
static T tNMEA2000::N2kMax ( a,
b 
)
inlinestatic

Delivers Max out of A an B.

As max and min are not available on all systems, so use own definition.

Template Parameters
TDatatype
Parameters
aValue A
bValue B
Returns
T

Definition at line 193 of file NMEA2000.h.

◆ N2kMin()

template<typename T >
static T tNMEA2000::N2kMin ( a,
b 
)
inlinestatic

Delivers Min out of A an B.

As max and min are not available on all systems, so use own definition.

Template Parameters
TDatatype
Parameters
aValue A
bValue B
Returns
T

Definition at line 205 of file NMEA2000.h.

◆ Open()

bool tNMEA2000::Open ( )

Open the CAN device.

You can call this on Setup(). It will be called anyway automatically by first call of tNMEA2000::ParseMessages(). When system is finally opened, OnOpen callback will be executed. See tNMEA2000::SetOnOpen

Note
After tNMEA2000::Open() you should start loop tNMEA2000::ParseMessages() without delays. If you e.g., call first tNMEA2000::Open() and then start to read configuration from slow flash, address claiming will not work properly.
Return values
true
false

Definition at line 1200 of file NMEA2000.cpp.

◆ ParseMessages()

void tNMEA2000::ParseMessages ( )

Parse all incoming Messages.

You have to call this periodically on loop() to handle N2k messages, otherwise tNMEA2000 object will not work at all.

Note
Do not use delay() on your loop(). Also take care that any library you use does not use delay() or any blocking function internally.

With NMEA2000 library you should use only nonblocking calls/libraries in your loop. Any library you use, should work asynchronously and not cause long delays on loop. Randomly caused 10-40 ms delays are acceptable. E.g., simple ADC conversion may block loop for 300 ms, which you will then see by other device dropping your device from the list.

See also
Note
Even if you only send e.g. temperature to the bus, you should call this so the node will automatically inform about itself to others.

See example TemperatureMonitor.ino.

Definition at line 2577 of file NMEA2000.cpp.

◆ ReadResetAddressChanged()

bool tNMEA2000::ReadResetAddressChanged ( )

Check if this device has changed its address.

With this function you can check has your device address you initiated with SetMode been changed after last call.

Note
For certified NMEA 2000 devices it is mandatory save changed address to e.g. EEPROM, for use in next startup.

When you call this, tNMEA2000::AddressChanged will be reset. Anyway, if system for some reason needs to change its address again, tNMEA2000::AddressChanged will be set. So you can e.g. in every 10 min check has address changed and if it has, save it.

See also
Return values
trueSome device source address changed. Read new values and save them.
falseNothing changed, no further actions required.

Definition at line 2496 of file NMEA2000.cpp.

◆ ReadResetDeviceInformationChanged()

bool tNMEA2000::ReadResetDeviceInformationChanged ( )

Check if this device has changed its DeviceInstances or SystemInstance.

With this function you can check has your device device instances or system instances changed.

Note
For certified NMEA 2000 devices it is mandatory save changed info to e.g. EEPROM, for initialize them in next startup.
See also
Return values
trueSome device information changed. Read new values and save them.
falseNothing changed, no further actions required.

Definition at line 2504 of file NMEA2000.cpp.

◆ ReadResetInstallationDescriptionChanged()

bool tNMEA2000::ReadResetInstallationDescriptionChanged ( )

Check if this device has changed its Install Description.

See also
Return values
trueInstallation description changed. Read new values and save them.
falseNothing changed, no further actions required.

Definition at line 1083 of file NMEA2000.cpp.

◆ RemoveGroupFunctionHandler()

void tNMEA2000::RemoveGroupFunctionHandler ( tN2kGroupFunctionHandler pGroupFunctionHandler)

Remove a message handler for incoming Group Function messages.

Parameters
pGroupFunctionHandlerMessage handler for group functions, see tN2kGroupFunctionHandler

Definition at line 2690 of file NMEA2000.cpp.

◆ RespondGroupFunction()

void tNMEA2000::RespondGroupFunction ( const tN2kMsg N2kMsg,
tN2kGroupFunctionCode  GroupFunctionCode,
unsigned long  PGNForGroupFunction,
int  iDev 
)
protected

Respond to an Group Function.

Document https://web.archive.org/web/20170609033039/http://www.nmea.org/Assets/20140109%20nmea-2000-corrigendum-tc201401031%20pgn%20126208.pdf defines that systems should respond to NMEA Request/Command/Acknowledge group function PGN 126208. Here we first call callback and if that will not handle function, we use default handler.

Parameters
N2kMsgReference to a N2kMsg Object
GroupFunctionCode
PGNForGroupFunction
iDevindex of the device on Devices

Definition at line 2353 of file NMEA2000.cpp.

◆ RespondISORequest()

void tNMEA2000::RespondISORequest ( const tN2kMsg N2kMsg,
unsigned long  RequestedPGN,
int  iDev 
)
protected

Respond to an ISO request.

If there is now IsoAdressClaim procedure started for this device, we respond for the requested PGN with sending IsoAddressClaim, Tx/Rx PGN lists, Product- / Config information or an user defined ISORqstHandler.

If non of this fits the RequestedPGN, we directly respond to the requester NAK. ( SetN2kPGNISOAcknowledgement)

Parameters
N2kMsgReference to a N2kMsg Object
RequestedPGNRequested PGN
iDevindex of the device on Devices

Definition at line 2297 of file NMEA2000.cpp.

◆ Restart()

void tNMEA2000::Restart ( )

Restart the device.

This is preliminary function for e.g. battery powered or devices, which may go to sleep or of the bus in any way. Function is under development.

Note
At the moment this functions just restarts the IsoAddressClaim

Definition at line 1264 of file NMEA2000.cpp.

◆ RunMessageHandlers()

void tNMEA2000::RunMessageHandlers ( const tN2kMsg N2kMsg)
protected

Run all message handlers.

Parameters
N2kMsgReference to a N2kMsg Object

Definition at line 2620 of file NMEA2000.cpp.

◆ SendConfigurationInformation() [1/2]

bool tNMEA2000::SendConfigurationInformation ( int  DeviceIndex = 0)

Send a Config Information message.

This is automatically used by class. You only need to use this, if you want to write your own behavior for providing config information.

Parameters
DeviceIndexindex of the device on Devices
Return values
trueSuccess
false

Definition at line 2260 of file NMEA2000.cpp.

◆ SendConfigurationInformation() [2/2]

bool tNMEA2000::SendConfigurationInformation ( unsigned char  Destination,
int  DeviceIndex,
bool  UseTP 
)

Send a Config Information message.

This is automatically used by class. You only need to use this, if you want to write your own behavior for providing config information.

Parameters
DestinationDestination address
DeviceIndexindex of the device on Devices
UseTPuse multi packet message
Return values
trueSuccess
false

Definition at line 2264 of file NMEA2000.cpp.

◆ SendFrame()

bool tNMEA2000::SendFrame ( unsigned long  id,
unsigned char  len,
const unsigned char *  buf,
bool  wait_sent = true 
)
protected

Sends a single CAN frame.

This function sends a CAN Message to the buffer, if we can not sent frame immediately via CANSendFrame()

Parameters
id{type}
len{type}
buf{type}
wait_sent{type}
Return values
truesuccess
falsefailed

Definition at line 1335 of file NMEA2000.cpp.

◆ SendFrames()

bool tNMEA2000::SendFrames ( )
protected

Sends pending all frames.

Return values
trueSuccess
falseMessage could not be sent.

Definition at line 1318 of file NMEA2000.cpp.

◆ SendHeartbeat() [1/2]

void tNMEA2000::SendHeartbeat ( bool  force = false)

Send Heartbeat for all devices.

Library will automatically send heartbeat, if interval is >0. You can also manually send it any time or force sent, if interval=0;

Parameters
forceTrue will send Heartbeat immediately, default = false

Definition at line 1396 of file NMEA2000.cpp.

◆ SendHeartbeat() [2/2]

void tNMEA2000::SendHeartbeat ( int  iDev)

Send heartbeat for specific device.

Library will automatically send heartbeat, if interval is >0. You can also manually send it any time or force sent, if interval=0;

Parameters
iDevindex of the device on Devices

Definition at line 1388 of file NMEA2000.cpp.

◆ SendIsoAddressClaim()

void tNMEA2000::SendIsoAddressClaim ( unsigned char  Destination = 0xff,
int  DeviceIndex = 0,
unsigned long  FromNow = 0 
)

Send an IsoAddressClaim message.

This is automatically used by class. You only need to use this, if you want to write your own behavior for address claiming.

Parameters
DestinationDestination address for the message
DeviceIndexindex of the device on Devices
FromNowoptional time delay from now in ms

Definition at line 2097 of file NMEA2000.cpp.

◆ SendMsg()

bool tNMEA2000::SendMsg ( const tN2kMsg N2kMsg,
int  DeviceIndex = 0 
)

Send message to the NMEA2000 bus.

Call this to send message to NMEA2000 bus. Before calling SendMsg() you have to prepare tN2kMsg type of message e.g. by using PGN specific function in N2kMessages.h.

Note
tNMEA2000 object is as default in tNMEA2000::N2km_ListenOnly mode. So if you want to send messages, you have to set right mode in setup with tNMEA2000::SetMode().

The function returns true, if the message was sent or buffered successfully, otherwise it return false. SendMsg may fail, if there is not room for message frames on sending buffer, device is not yet open or not ready to send.

If you sent single frame message time to time, it will normally go directly to CAN controller sent "mailbox", where controller sends it as soon as it gets time from bus. Sending fastpacket message will always buffer at least other than first frame. Internally sent buffer is devided to driver buffer and library buffer. Driver buffer will empty by interrupt and library buffer on call to SendMsg or ParseMessages, which moves frames to driver buffer. To have reliable sending, you need a sending buffer, which is large enough for your device needs. See tNMEA2000::SetN2kCANSendFrameBufSize.

See example TemperatureMonitor.ino.

See also
Most common hardware errors
Parameters
N2kMsgReference to a N2kMsg Object
DeviceIndexindex of the device on Devices
Setting DeviceIndex to -1 forces library to use source address of N2kMsg instead of device source address. This is useful with e.g., passthrough gateway devices.
Return values
trueMessage sent or buffered successfully.
falseOpen has not finished. Address claiming has not finished. There is no more room on send buffer, which may be caused by too small send buffer or CAN controller can not sent messages bus.

Definition at line 1450 of file NMEA2000.cpp.

◆ SendPendingInformation()

void tNMEA2000::SendPendingInformation ( )
protected

Send ISO AddressClaim, Product Information and Config Information.

Currently Product Information and Configuration Information will we pended on ISO request. This is because specially for broadcasted response it may take a while, when higher priority devices sends their response.

See also

Definition at line 1431 of file NMEA2000.cpp.

◆ SendPendingTPMessage()

void tNMEA2000::SendPendingTPMessage ( int  iDev)
protected

Send pending ISO-TP Messages.

Parameters
iDevindex of the device on Devices

Definition at line 1956 of file NMEA2000.cpp.

◆ SendProductInformation() [1/2]

bool tNMEA2000::SendProductInformation ( int  DeviceIndex = 0)

Send a Product Information message.

This is automatically used by class. You only need to use this, if you want to write your own behavior for providing product information.

Parameters
DeviceIndexindex of the device on Devices
Return values
trueSuccess
false

Definition at line 2218 of file NMEA2000.cpp.

◆ SendProductInformation() [2/2]

bool tNMEA2000::SendProductInformation ( unsigned char  Destination,
int  DeviceIndex,
bool  UseTP 
)

Send a Product Information message.

This is automatically used by class. You only need to use this, if you want to write your own behavior for providing product information.

Parameters
DestinationDestination address
DeviceIndexindex of the device on Devices
UseTPuse multi packet message
Return values
trueSuccess
false

Definition at line 2222 of file NMEA2000.cpp.

◆ SendRxPGNList()

void tNMEA2000::SendRxPGNList ( unsigned char  Destination,
int  DeviceIndex,
bool  UseTP = false 
)

Send a list with all supported Receive messages.

This function sends a PGN 126464 message consisting of all messages supported by this device for reception.

Parameters
DestinationDestination address
DeviceIndexindex of the device on Devices
UseTPuse multi packet message, default = false

Definition at line 2161 of file NMEA2000.cpp.

◆ SendTPCM_Abort()

void tNMEA2000::SendTPCM_Abort ( unsigned long  PGN,
unsigned char  Destination,
int  iDev,
unsigned char  AbortCode 
)
protected

Send ISO Transport Protocol message Abort.

Parameters
PGNPGN
DestinationDestination address
iDevindex of the device on Devices
AbortCodeAbort Code

Definition at line 1742 of file NMEA2000.cpp.

◆ SendTPCM_BAM()

bool tNMEA2000::SendTPCM_BAM ( int  iDev)
protected

Send ISO Transport Protocol message BAM.

This is used for Broadcast messages

Parameters
iDevindex of the device on Devices
Return values
true
false

Definition at line 1668 of file NMEA2000.cpp.

◆ SendTPCM_CTS()

void tNMEA2000::SendTPCM_CTS ( unsigned long  PGN,
unsigned char  Destination,
int  iDev,
unsigned char  nPackets,
unsigned char  NextPacketNumber 
)
protected

Send ISO Transport Protocol message CTS.

Parameters
PGNPGN
DestinationDestination address
iDevindex of the device on Devices
nPacketsNumber of packets
NextPacketNumberNumber of the next packet

Definition at line 1707 of file NMEA2000.cpp.

◆ SendTPCM_EndAck()

void tNMEA2000::SendTPCM_EndAck ( unsigned long  PGN,
unsigned char  Destination,
int  iDev,
uint16_t  nBytes,
unsigned char  nPackets 
)
protected

Send ISO Transport Protocol message End Acknowledge.

Parameters
PGNPGN
DestinationDestination address
iDevindex of the device on Devices
nBytesNumber of bytes
nPacketsNumber of packets

Definition at line 1725 of file NMEA2000.cpp.

◆ SendTPCM_RTS()

bool tNMEA2000::SendTPCM_RTS ( int  iDev)
protected

Send ISO Transport Protocol message RTS.

Parameters
iDevindex of the device on Devices
Return values
true
false

Definition at line 1687 of file NMEA2000.cpp.

◆ SendTPDT()

bool tNMEA2000::SendTPDT ( int  iDev)
protected

Send ISO Transport Protocol data packet.

Note
Caller should take care of not calling this after all has been done. Use HasAllTPDTSent for checking.
Parameters
iDevindex of the device on Devices
Return values
trueMessage was send successful
false

Definition at line 1762 of file NMEA2000.cpp.

◆ SendTxPGNList()

void tNMEA2000::SendTxPGNList ( unsigned char  Destination,
int  DeviceIndex,
bool  UseTP = false 
)

Send a list with all supported Transmit messages.

This function sends a PGN 126464 message consisting of all messages supported by this device for transmission.

Parameters
DestinationDestination address
DeviceIndexindex of the device on Devices
UseTPuse multi packet message, default = false

Definition at line 2128 of file NMEA2000.cpp.

◆ SetCharBuf()

void tNMEA2000::SetCharBuf ( const char *  str,
size_t  MaxLen,
char *  buf 
)
static

Setting up a Char Buffer.

This functions copies the a to the char buffer and terminates it with 0.

Parameters
strString
MaxLenMax length of the buffer
bufPointer to the buffer

Definition at line 644 of file NMEA2000.cpp.

◆ SetConfigurationInformation()

void tNMEA2000::SetConfigurationInformation ( const char *  ManufacturerInformation,
const char *  InstallationDescription1 = 0,
const char *  InstallationDescription2 = 0 
)

Set the Configuration Information of this device.

With this function you can set configuration information for your device.

Configuration information contains some extra information about device installation and manufacturer. Some MFD shows it, some does not. NMEA Reader can show configuration information. InstallationDescription1 and InstallationDescription2 can be changed as default during runtime by NMEA 2000 group function commands. That can be done e.g. with NMEA Reader.

Note
You can disable configuration information by calling SetProgmemConfigurationInformation(0);
See also
Parameters
ManufacturerInformationBuffer for Manufacturer information. Use e.g., company name and web address.
InstallationDescription1Buffer for Installation Description 1. Installation Description 1 and 2 may be used by device installer e.g., for location of physical device to help to find it. To handle externally updated Installation Description you should listen changes of them. See tNMEA2000::ReadResetInstallationDescriptionChanged
InstallationDescription2Buffer for Installation Description 2.

Definition at line 764 of file NMEA2000.cpp.

◆ SetDebugMode()

void tNMEA2000::SetDebugMode ( tDebugMode  _dbMode)

Set the Debug Mode of the system.

If you do not have physical N2k bus connection and you like to test your board without even CAN controller, you can use this function.

See also
Parameters
_dbModeDebug mode, see tNMEA2000::tDebugMode

Definition at line 1547 of file NMEA2000.cpp.

◆ SetDeviceCount()

void tNMEA2000::SetDeviceCount ( const uint8_t  _DeviceCount)

Set the count of devices library shows on bus.

With this function you can enable multi device support. As default there is only one device.

Note
To enable multi device support, you need to call this before any other tNMEA2000 class function.
See also
Multi device support
Parameters
_DeviceCountMaximum number of devices that can be hold in Devices

Definition at line 704 of file NMEA2000.cpp.

◆ SetDeviceInformation()

void tNMEA2000::SetDeviceInformation ( unsigned long  _UniqueNumber,
unsigned char  _DeviceFunction = 0xff,
unsigned char  _DeviceClass = 0xff,
uint16_t  _ManufacturerCode = 0xffff,
unsigned char  _IndustryGroup = 4,
int  iDev = 0 
)

Set the Device Information. See also NAME.

If you use device modes tNMEA2000::N2km_NodeOnly or tNMEA2000::N2km_ListenAndNode, it is critical that you set this information.

Device information will be used to choose right address for your device (also called node) on the bus. Each device must have an own address. Library will do this automatically, so it is enough that you call this function on setup to define your device.

For keeping defaults use 0xffff/0xff for int/char values and null ptr for pointers.

Note
You should set information so that it is unique over the world! Well if you are making device only for your own yacht N2k bus, it is enough to be unique there. So e.g. if you have two temperature monitors made by this library, you have to set at least first parameter UniqueNumber different for both of them.

I just decided to use number one below maximum for ManufacturerCode as Open Source devices - this is not any number given by NMEA.

Parameters
_UniqueNumberDefault=1. 21 bit resolution, max 2097151. Each device from same manufacturer should have unique number.
_DeviceFunctionDefault=130, PC Gateway. See codes on https://web.archive.org/web/20190531120557/https://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
_DeviceClassDefault=25, Inter/Intranetwork Device. See codes on https://web.archive.org/web/20190531120557/https://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
_ManufacturerCodeDefault=2046. Maximum 2046. See the list of codes on https://web.archive.org/web/20190529161431/http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
_IndustryGroupDefault=4, Marine.
iDevindex of the device on Devices

Definition at line 1093 of file NMEA2000.cpp.

◆ SetDeviceInformationInstances()

void tNMEA2000::SetDeviceInformationInstances ( uint8_t  _DeviceInstanceLower = 0xff,
uint8_t  _DeviceInstanceUpper = 0xff,
uint8_t  _SystemInstance = 0xff,
int  iDev = 0 
)

Set the Device Information Instances.

With this function you can set device instance lower, device instance upper and system instance values.

For certified devices there is requirement that device instances can be externally changed. Library handles that as default, but developer has to take care that changed instances will be saved and restored on devices start.

See also
Parameters
_DeviceInstanceLower0xff means no change
_DeviceInstanceUpper
_SystemInstance
iDevindex of the device on Devices

Definition at line 1110 of file NMEA2000.cpp.

◆ SetFastPacketMessages()

void tNMEA2000::SetFastPacketMessages ( const unsigned long *  _FastPacketMessages)

Set the list of known Fast Packet Messages.

Call these if you wish to override the default message packets supported. Pointers must be in PROGMEM

As a default library has a list of known messages. With this function user can override default list of fast packet messages. Build in default list contains all fastpackets found from open information.

Note
If an incoming fast packet message is not known, it will be treated as single frame message. So if you want to handle unknown fast packet message, you need to duplicate frame collection logic from library to your code. So it is easier to have fast packet messages listed on library, if you want to handle them.
See also
Parameters
_FastPacketMessagesBuffer holding fast packet messages

Definition at line 1148 of file NMEA2000.cpp.

◆ SetForwardOnlyKnownMessages()

void tNMEA2000::SetForwardOnlyKnownMessages ( bool  v = true)
inline

Enable Only Known Messages for forwarding.

Set false as default. With this you can control if unknown messages will be forwarded to forward stream. If you set this true, all unknown message will not be forwarded to the stream.

Note
This does not effect for own messages. Known messages are listed on library.
See also
Parameters
vEnable, default = false

Definition at line 2936 of file NMEA2000.h.

◆ SetForwardOwnMessages()

void tNMEA2000::SetForwardOwnMessages ( bool  v = true)
inline

Enable Own Messages for forwarding.

Set true as default. With this you can control if messages your device sends to bus will be forwarded to forward stream.

Parameters
vEnable, default = true

Definition at line 2948 of file NMEA2000.h.

◆ SetForwardStream()

void tNMEA2000::SetForwardStream ( N2kStream _stream)
inline

Set the Forward Stream object.

As default, forward stream has been set to null. For e.g. Arduino Due you can set it to SerialUSB, so you can use Serial for other things. You can of coarse use any stream available on your device.
See example ActisenseListenerSender.ino.

Parameters
_streamStream to be used for message forwarding

Definition at line 2603 of file NMEA2000.h.

◆ SetForwardSystemMessages()

void tNMEA2000::SetForwardSystemMessages ( bool  v = true)
inline

Enable System Messages for forwarding.

Set true as default. With this you can control if system messages like address claiming, device information will be forwarded to forward stream. If you set this false, system messages will not be forwarded to the stream.

Parameters
vEnable, default = true

Definition at line 2914 of file NMEA2000.h.

◆ SetForwardType()

void tNMEA2000::SetForwardType ( tForwardType  fwdType)
inline

Set the Forward Streaming Type.

With this function user can set how messages will be forwarded to the stream. Possible values are:

  • tNMEA2000::fwdt_Actisense (default) forwards messages is Actisense format. Some navigation softwares can read this format.
  • tNMEA2000::fwdt_Text forwards messages to output port in clear text. I see this useful only for testing with normal serial monitors.
Parameters
fwdTypeFormat type see tNMEA2000::tForwardType, default = fwdt_Actisense

Definition at line 2591 of file NMEA2000.h.

◆ SetHandleOnlyKnownMessages()

void tNMEA2000::SetHandleOnlyKnownMessages ( bool  v = true)
inline

Set the Handle Only Known Messages.

Set false as default. With this you can control if unknown messages will be handled at all. Known messages are listed on library.

See also
Parameters
vEnable, default = false

Definition at line 2966 of file NMEA2000.h.

◆ SetHeartbeatInterval()

void tNMEA2000::SetHeartbeatInterval ( unsigned long  interval,
bool  SetAsDefault = true,
int  iDev = -1 
)

Deprecated. Use function SetHeartbeatIntervalAndOffset.

Definition at line 1383 of file NMEA2000.cpp.

◆ SetHeartbeatIntervalAndOffset()

void tNMEA2000::SetHeartbeatIntervalAndOffset ( uint32_t  interval,
uint32_t  offset = 0,
int  iDev = -1 
)

Set the Heartbeat Interval and Offset for a device.

Library will automatically start heartbeat with default interval 60 s and offset 10 s.

According to document NMEA Heartbeat Corrigendum all NMEA devices shall transmit heartbeat PGN 126993. With this function you can set transmission interval in ms (range 1000-655320ms , default 60000). Set interval 0 to disable heartbeat

Function allows to set interval over 60 s or 0 to disable sending for test purposes.

Parameters
intervalHeartbeat Interval in ms. 0xffffffff=keep current, 0xfffffffe=restore default
offsetHeartbeat Offset in ms. 0xffffffff=keep current, 0xfffffffe=restore default
iDevIndex of the device on Devices or -1 to set for all.

Definition at line 1356 of file NMEA2000.cpp.

◆ SetInstallationDescription1()

void tNMEA2000::SetInstallationDescription1 ( const char *  InstallationDescription1)

Set the Installation Description 1 of this device.

This is automatically used by class. You only need to use this, if you want to write your own behavior.

Parameters
InstallationDescription1Description

Definition at line 1037 of file NMEA2000.cpp.

◆ SetInstallationDescription2()

void tNMEA2000::SetInstallationDescription2 ( const char *  InstallationDescription2)

Set the Installation Description 2 of this device.

This is automatically used by class. You only need to use this, if you want to write your own behavior.

Parameters
InstallationDescription2Description

Definition at line 1047 of file NMEA2000.cpp.

◆ SetISORqstHandler()

void tNMEA2000::SetISORqstHandler ( bool(*)(unsigned long RequestedPGN, unsigned char Requester, int DeviceIndex)  ISORequestHandler)

Set the message handler for incoming ISO Requests.

Devices on N2k bus may request PGN from your device. Certified devices should respond to requests to any transmit PGN listed. Write your own request handler and set it with this function.

The handler will be called by tNMEA2000::ParseMessages, if there is ISO request.

Note
When you send request message with tNMEA2000::SendMsg and it fails, it is your responsibility to take care of sending response again later. If your sending buffer is large enough, it is very uncommon that tNMEA2000::SendMsg fails.
Parameters
ISORequestHandlerMessage handler

Definition at line 2684 of file NMEA2000.cpp.

◆ SetMode()

void tNMEA2000::SetMode ( tN2kMode  _N2kMode,
uint8_t  _N2kSource = 15 
)

Set the library mode and start source address.

With SetMode you can define how your node acts on N2k bus and set start source address. Source address setting has effect only on modes N2km_NodeOnly and N2km_ListenAndNode.

NMEA2000 standard requires that once your device has been connected to the bus, it uses last used address on next start. Due to address claiming, your device may change its source address, when you add new devices to the bus. So you should save last used address to e.g. EEPROM and on setup use it as parameter for SetMode. You can check if your address you set originally by SetMode has changed by using function tNMEA2000::ReadResetAddressChanged() and you can read current address by function tNMEA2000::GetN2kSource().

See also
Parameters
_N2kModeMode for this node, see tN2kMode
_N2kSourceSource address for this node

Definition at line 1177 of file NMEA2000.cpp.

◆ SetMsgHandler()

void tNMEA2000::SetMsgHandler ( void(*)(const tN2kMsg &N2kMsg)  _MsgHandler)

Set the message handler for incoming NMEA2000 messages.

If you want to do something with messages read from N2k bus, easiest is to set message handler, which will be then called by ParseMessages for each new message.

If you have LCD display on your device and you want to show e.g., fluid level on it, create message handler, parse fluid level messages and draw value to display.

See example DataDisplay.ino or DataDisplay2.ino

Parameters
_MsgHandlerOld style - callback function pointer

Definition at line 2638 of file NMEA2000.cpp.

◆ SetN2kCANBufMsg()

uint8_t tNMEA2000::SetN2kCANBufMsg ( unsigned long  canId,
unsigned char  len,
unsigned char *  buf 
)
protected

Function handles received CAN frame and adds it to tN2kCANMsg.

This function returns Index to a ready tN2kCANMsg on buffer N2kCANMsgBuf (max MaxN2kCANMsgs), if we skipped the frame or message is not ready (fast packet or ISO Multi-Packet)

Parameters
canIdID of CAN message
lenlength of payload
bufbuffer for payload of message
Returns
uint8_t -> Index of the CAN message on N2kCANMsgBuf

Definition at line 1977 of file NMEA2000.cpp.

◆ SetN2kCANMsgBufSize()

void tNMEA2000::SetN2kCANMsgBufSize ( const uint8_t  _MaxN2kCANMsgs)
inline

Set incoming CAN message (tNMEA2000::N2kCANMsgBuf) buffer size.

With this function you can set size of buffer, where system builds incoming messages. The default size is 5 messages. Some messages are just single frame messages and they will be read in and handled immediately on call to ParseMessages. NMEA2000 fast packet message contains several frames sent sequentially. Anyway there can be other message frames between fast packet message frames, so these messages will be buffered and saved until all frames has been received. NMEA2000 requires that device should be able to read 2 concurrent fast packet and 2 ISO TP message. Since also single frame message requires one buffer slot buffer default size has been set to 5.

If buffer size is too small, there is risk that all fast packet messages will not be handled. Even your own logic does not listen any fast packet messages, internal logic listens group functions PGN 126208, which may contain important requests library should respond. If library does not respond to all required requests, there is risk that other devices drops your device information.

Since ISO TP messages are rarely used, with buffer size 5 library can handle 4 concurrent fast packet messages. Due to priorities this is enough in most cases. If bus has lot of devices sending fast packets and you have enough memory on MCU, you can increase buffer size. Buffer size 10 should be enough even on heavy traffic.

Function has to be called before communication opens. See tNMEA2000::Open().

Do not mix this with tNMEA2000::SetN2kCANReceiveFrameBufSize(), which has different meaning.

Parameters
_MaxN2kCANMsgsNumber of CAN messages that can be stored in tNMEA2000::N2kCANMsgBuf

Definition at line 1816 of file NMEA2000.h.

◆ SetN2kCANReceiveFrameBufSize()

virtual void tNMEA2000::SetN2kCANReceiveFrameBufSize ( const uint16_t  _MaxCANReceiveFrames)
inlinevirtual

Set CAN receive frame buffer size.

Some CAN drivers allows interrupted receive frame buffering. You can set receive buffer size with this function. If you use this function, call it once before tNMEA2000::Open();

Driver may override your setting, if you set too small or too large buffer size. This is driver dependent behaviour.

See also
Parameters
_MaxCANReceiveFrames{type}

Definition at line 1870 of file NMEA2000.h.

◆ SetN2kCANSendFrameBufSize()

virtual void tNMEA2000::SetN2kCANSendFrameBufSize ( const uint16_t  _MaxCANSendFrames)
inlinevirtual

Set CAN send frame buffer size.

With this function you can set size of buffer, where system saves frames of messages to be sent. Given buffer size will be devided to driver buffer and library buffer. Driver buffer will empty by driver interrupt and library buffer by call to SendMsg or ParseMessages, which moves frames to driver buffer. If driver can handle large buffer, library buffer will be set to minimum.

When sending long messages like ProductInformation or GNSS data, there may not be enough buffers for successfully send data. This depends of your hw and device source. Device source has effect due to priority of getting sending time. If your data is critical, use buffer size, which is large enough (default 40 frames).

E.g. Product information takes totally 134 bytes. This needs 20 frames. If you also send GNSS 47 bytes=7 frames. If you want to be sure that both will be sent on any situation, you need at least 27 frame buffer size.

If you use this function, call it once before tNMEA2000::Open() and before any device related function like tNMEA2000::SetProductInformation. If you call it later, function has no effect.

Driver may override your setting, if you set too small or too large buffer size. This is driver dependent behaviour.

See also
Parameters
_MaxCANSendFramesMaximum number of CAN frames that can be buffered in tNMEA2000::CANSendFrameBuf

Definition at line 1853 of file NMEA2000.h.

◆ SetN2kSource()

void tNMEA2000::SetN2kSource ( unsigned char  _iAddr,
int  _iDev = 0 
)

Set source for the given device.

With this function you can set you device source address on the N2k bus. This is meant to be use for multi device on basic configuration to restore source address changed by address claiming.

This function has to be called after tNMEA2000::SetMode() and before tNMEA2000::Open()

See also
Parameters
_iAddrAddress of the device
_iDevindex of the device on Devices

Definition at line 2488 of file NMEA2000.cpp.

◆ SetOnOpen()

void tNMEA2000::SetOnOpen ( void(*)()  _OnOpen)

Set OnOpen callback function.

OnOpen will be called, when communication really opens and starts initial address claiming. You can use this to init your message sending to synchronize them with e.g., heartbeat.

Note
In future OnOpen may be called several times, if communication will be reopened by Restart or driver error. Developer must take care that possible memory initializations will be handled properly in case OnOpen is called several times.

Definition at line 2633 of file NMEA2000.cpp.

◆ SetProductInformation() [1/2]

void tNMEA2000::SetProductInformation ( const char *  _ModelSerialCode,
unsigned short  _ProductCode = 0xffff,
const char *  _ModelID = 0,
const char *  _SwCode = 0,
const char *  _ModelVersion = 0,
unsigned char  _LoadEquivalency = 0xff,
unsigned short  _N2kVersion = 0xffff,
unsigned char  _CertificationLevel = 0xff,
int  iDev = 0 
)

Set the Product Information of this device.

With this function you define how your device will show up for other devices on NMEA2000 bus.

Define your product information. Defaults will be set on initialization. For keeping defaults use 0xffff/0xff for int/char values and null ptr for pointers. LoadEquivalency is multiplication of 50 mA, what your device will take power from NMEA2000 bus. E.g. for Arduino only it can be 1 (=50mA). If your device does not take power from bus, set this to 0.

If you use device modes tNMEA2000::N2km_ListenOnly, tNMEA2000::N2km_ListenAndSend or N2km_SendOnly, function does not have effect.

Note
Serial code has length of 32 so just long enough to carry GUID.
Parameters
_ModelSerialCodeDefault="00000001". Max 32 chars. Manufacturer's Model serial code
_ProductCodeDefault=666. Manufacturer's product code. For certified devices this number will be given by NMEA organization.
_ModelIDDefault="Arduino N2k->PC". Max 33 chars. Manufacturer's Model ID
_SwCodeDefault="1.0.0.0". Max 40 chars. Manufacturer's software version code
_ModelVersionDefault="1.0.0". Max 24 chars. Manufacturer's Model version
_LoadEquivalencyDefault=1. x * 50 mA
_N2kVersionDefault=2101
_CertificationLevelDefault=1
iDevindex of the device on Devices

Definition at line 745 of file NMEA2000.cpp.

◆ SetProductInformation() [2/2]

void tNMEA2000::SetProductInformation ( const tProductInformation _ProductInformation,
int  iDev = 0 
)

Set the Product Information of this device.

Call this if you want to save RAM and you have defined tProductInformation to PROGMEM as in example BatteryMonitor.ino

Note
I have not yet found a way to test is pointer in PROGMEM or not, so this does not work, if you define tProductInformation to RAM.
Parameters
_ProductInformationProduct information, see tProductInformation
iDevindex of the device on Devices

Definition at line 736 of file NMEA2000.cpp.

◆ SetProgmemConfigurationInformation()

void tNMEA2000::SetProgmemConfigurationInformation ( const char *  ManufacturerInformation,
const char *  InstallationDescription1 = 0,
const char *  InstallationDescription2 = 0 
)

Set the Configuration Information located on PROGMEM.

With this function you can set configuration information, which will be saved on device program memory. See example BatteryMonitor.ino.

This function is useful only on MCUs with very small RAM. By using PROGMEM, installation description can not be changed by group function.

As default system has build in configuration information on progmem. If you do not want to have configuration information at all, you can disable it by calling SetConfigurationInformation(0);

Parameters
ManufacturerInformationBuffer for Manufacturer information
InstallationDescription1Buffer for Installation Description 1
InstallationDescription2Buffer for Installation Description 2

Definition at line 802 of file NMEA2000.cpp.

◆ SetSingleFrameMessages()

void tNMEA2000::SetSingleFrameMessages ( const unsigned long *  _SingleFrameMessages)

Set the list of known Single Frame Messages.

Call these if you wish to override the default message packets supported. Pointers must be in PROGMEM

const unsigned SingleFrameMessages[] PROGMEM={......L,0};
const unsigned long * SingleFrameMessages[N2kMessageGroups]
Definition: NMEA2000.h:985

As a default library has a list of known messages. With this function user can override default list of single frame messages.

Known single frame message lists has only effect if you limit handling with tNMEA2000::HandleOnlyKnownMessages or use message forwarding and limit it with tNMEA2000::SetForwardOnlyKnownMessages

See also
Parameters
_SingleFrameMessagesBuffer holding single frame messages

Definition at line 1143 of file NMEA2000.cpp.

◆ StartAddressClaim() [1/2]

void tNMEA2000::StartAddressClaim ( )
protected

Starting the ISO Address Claim for all devices.

Definition at line 2406 of file NMEA2000.cpp.

◆ StartAddressClaim() [2/2]

void tNMEA2000::StartAddressClaim ( int  iDev)
protected

Starting the ISO Address Claim for a device.

Parameters
iDevindex of the device on Devices

Definition at line 2393 of file NMEA2000.cpp.

◆ StartSendTPMessage()

bool tNMEA2000::StartSendTPMessage ( const tN2kMsg msg,
int  iDev 
)
protected

Start sending an ISO-TP message.

Parameters
msgReference to a N2kMsg Object
iDevindex of the device on Devices
Return values
true
false

Definition at line 1926 of file NMEA2000.cpp.

◆ TestHandleTPMessage()

bool tNMEA2000::TestHandleTPMessage ( unsigned long  PGN,
unsigned char  Source,
unsigned char  Destination,
unsigned char  len,
unsigned char *  buf,
uint8_t &  MsgIndex 
)
protected

ISO Transport Protocol handlers for multi packet support.

Parameters
PGNPGN
SourceSource address
DestinationDestination address
lenlength of the data payload
bufpointer to a byte buffer for the
MsgIndexMsgIndex for N2kCANMsgBuf
Return values
true
false

Definition at line 1786 of file NMEA2000.cpp.

Member Data Documentation

◆ AddressChanged

bool tNMEA2000::AddressChanged
protected

Flag that the address has changed.

Definition at line 970 of file NMEA2000.h.

◆ CANSendFrameBuf

tCANSendFrame* tNMEA2000::CANSendFrameBuf
protected

Buffer for library send out CAN frames.

CANSendFrameBuf is library internal buffer for frames waiting for sending. If CanSendFrame() can not send or buffer frame to driver buffer, frame will be stored to this buffer and its sending will be tried again on next SendMsg() or ParseMessages() call.

Inherited driver class can split size of MaxCANSendFrames to driver buffer and library buffer. Inherited class can even disable library buffer.

InitCANFrameBuffers().

Definition at line 1035 of file NMEA2000.h.

◆ CANSendFrameBufferRead

uint16_t tNMEA2000::CANSendFrameBufferRead
protected

Next write index for the library CAN send frame buffer.

Definition at line 1055 of file NMEA2000.h.

◆ CANSendFrameBufferWrite

uint16_t tNMEA2000::CANSendFrameBufferWrite
protected

Next read index for the library CAN send frame buffer.

Definition at line 1052 of file NMEA2000.h.

◆ ConfigurationInformation

tConfigurationInformation tNMEA2000::ConfigurationInformation
protected

Configuration Information of the device.

Definition at line 983 of file NMEA2000.h.

◆ dbMode

tDebugMode tNMEA2000::dbMode
protected

Attribute that holds the actual Debug Mode (default = md_none)

Definition at line 950 of file NMEA2000.h.

◆ DeviceCount

int tNMEA2000::DeviceCount
protected

Number of devices.

Definition at line 977 of file NMEA2000.h.

◆ DeviceInformationChanged

bool tNMEA2000::DeviceInformationChanged
protected

Flag that the device information has changed.

Definition at line 972 of file NMEA2000.h.

◆ Devices

tInternalDevice* tNMEA2000::Devices
protected

Pointer to a buffer for all internal devices.

Definition at line 975 of file NMEA2000.h.

◆ FastPacketMessages

const unsigned long* tNMEA2000::FastPacketMessages[N2kMessageGroups]
protected

Definition at line 986 of file NMEA2000.h.

◆ ForwardMode

unsigned int tNMEA2000::ForwardMode
protected

Actual message forward operation mode (default = all messages - also system and own)

Definition at line 959 of file NMEA2000.h.

◆ ForwardStream

N2kStream* tNMEA2000::ForwardStream
protected

Actual stream to be used for forward messaging.

Definition at line 961 of file NMEA2000.h.

◆ ForwardType

tForwardType tNMEA2000::ForwardType
protected

Actual message forward type (default = fwdt_Actisense)

Definition at line 956 of file NMEA2000.h.

◆ FwdModeBit_EnableForward

const int tNMEA2000::FwdModeBit_EnableForward = BIT(0)
staticprotected

Forward mode bit: -> If set, forward is enabled.

Definition at line 936 of file NMEA2000.h.

◆ FwdModeBit_OnlyKnownMessages

const int tNMEA2000::FwdModeBit_OnlyKnownMessages = BIT(2)
staticprotected

Forward mode bit: -> Only known messages will be forwarded. System messages will be forwarded according its own bit.

Definition at line 941 of file NMEA2000.h.

◆ FwdModeBit_OwnMessages

const int tNMEA2000::FwdModeBit_OwnMessages = BIT(3)
staticprotected

Forward mode bit: -> Forward also all messages, what this device will send.

Definition at line 944 of file NMEA2000.h.

◆ FwdModeBit_SystemMessages

const int tNMEA2000::FwdModeBit_SystemMessages = BIT(1)
staticprotected

Forward mode bit: -> System messages will be forwarded.

Definition at line 938 of file NMEA2000.h.

◆ HandleModeBit_OnlyKnownMessages

const int tNMEA2000::HandleModeBit_OnlyKnownMessages = BIT(4)
staticprotected

Forward mode bit: -> Only known messages will be handled.

Definition at line 946 of file NMEA2000.h.

◆ InstallationDescriptionChanged

bool tNMEA2000::InstallationDescriptionChanged
protected

Flag the Installation description has changed.

Definition at line 1746 of file NMEA2000.h.

◆ ISORqstHandler

bool(* tNMEA2000::ISORqstHandler) (unsigned long RequestedPGN, unsigned char Requester, int DeviceIndex)
protected

Handler callbacks for 'ISORequest' messages.

Definition at line 1079 of file NMEA2000.h.

◆ LocalConfigurationInformationData

char* tNMEA2000::LocalConfigurationInformationData
protected

Pointer to a buffer for local Configuration Information.

Definition at line 981 of file NMEA2000.h.

◆ MaxCANReceiveFrames

uint16_t tNMEA2000::MaxCANReceiveFrames
protected

Max number received CAN messages that can go to the buffer.

See also

Definition at line 1061 of file NMEA2000.h.

◆ MaxCANSendFrames

uint16_t tNMEA2000::MaxCANSendFrames
protected

Size of CANSendFrameBuf or before initialization requested total frame buffering size.

Member has two function. On setup program can request tNMEA2000::SetN2kCANSendFrameBufSize, which will be saved to this member. Inherited tNMEA2000::InitCANFrameBuffers can split size to driver buffer and library buffer.

See also

Definition at line 1049 of file NMEA2000.h.

◆ MaxN2kCANMsgs

uint8_t tNMEA2000::MaxN2kCANMsgs
protected

Size of N2kCANMsgBuf receiving message buffer.

See also

Definition at line 1021 of file NMEA2000.h.

◆ MsgHandler

void(* tNMEA2000::MsgHandler) (const tN2kMsg &N2kMsg)
protected

Handler callbacks for normal messages.

Definition at line 1077 of file NMEA2000.h.

◆ MsgHandlers

tMsgHandler* tNMEA2000::MsgHandlers
protected

Pointer to a buffer for Message Handlers.

Definition at line 963 of file NMEA2000.h.

◆ N2kCANMsgBuf

tN2kCANMsg* tNMEA2000::N2kCANMsgBuf
protected

Buffer for receiving messages.

See also

Definition at line 1015 of file NMEA2000.h.

◆ N2kMode

tN2kMode tNMEA2000::N2kMode
protected

Actual operation mode of this device (default = N2km_ListenOnly)

Definition at line 954 of file NMEA2000.h.

◆ OnOpen

void(* tNMEA2000::OnOpen) ()
protected

Callback function, which will be called when library start bus communication.

OnOpen Will be called when library starts bus communication. At open library first starts address claiming and after it has been accepted, other communication can start. OnOpen can be used e.g., for message timing synchronization so that every time device starts messages will have same sent offset. See also SetOnOpen().

Note
In future OnOpen may be called several times, if communication will be reopened by Restart or driver error. Developer must take care that possible memory initializations will be handled properly in case OnOpen is called several times.

Definition at line 1074 of file NMEA2000.h.

◆ OpenScheduler

tN2kScheduler tNMEA2000::OpenScheduler
protected

Open the Scheduler

Definition at line 966 of file NMEA2000.h.

◆ OpenState

tOpenState tNMEA2000::OpenState
protected

State of the ....

Definition at line 968 of file NMEA2000.h.

◆ pGroupFunctionHandlers

tN2kGroupFunctionHandler* tNMEA2000::pGroupFunctionHandlers
protected

Pointer to Buffer for GRoup Function Handlers.

Definition at line 1083 of file NMEA2000.h.

◆ SingleFrameMessages

const unsigned long* tNMEA2000::SingleFrameMessages[N2kMessageGroups]
protected

Definition at line 985 of file NMEA2000.h.


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