NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
DeviceAnalyzer.ino
Go to the documentation of this file.
1/***********************************************************************/
12#include <Arduino.h>
13#define N2k_CAN_INT_PIN 21
14#include <NMEA2000_CAN.h> // This will automatically choose right CAN library and create suitable NMEA2000 object
15#include "N2kDeviceList.h"
16
19
20//*****************************************************************************
21template<typename T> void PrintLabelValWithConversionCheckUnDef(const char* label, T val, double (*ConvFunc)(double val)=0, bool AddLf=false ) {
22 OutputStream->print(label);
23 if (!N2kIsNA(val)) {
24 if (ConvFunc) { OutputStream->print(ConvFunc(val)); } else { OutputStream->print(val); }
25 } else OutputStream->print("not available");
26 if (AddLf) OutputStream->println();
27}
28
29//*****************************************************************************
30void setup() {
31 Serial.begin(115200);
32 OutputStream=&Serial;
33 Serial.println("Device analyzer is starting up...");
34 delay(3000);
37 // Set Product information
38 NMEA2000.SetProductInformation("00000003", // Manufacturer's Model serial code
39 100, // Manufacturer's product code
40 "N2k bus device analyzer", // Manufacturer's Model ID
41 "1.0.0.10 (2017-07-29)", // Manufacturer's Software version code
42 "1.0.0.0 (2017-07-12)" // Manufacturer's Model version
43 );
44
45 // Set device information
46 NMEA2000.SetDeviceInformation(2, // Unique number. Use e.g. Serial number.
47 130, // Device function=Display. 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
48 120, // Device class=Display. 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
49 2046 // Just choosen free from code list on https://web.archive.org/web/20190529161431/http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
50 );
51 // Uncomment 3 rows below to see, what device will send to bus
52
53 NMEA2000.SetForwardStream(0); //&Serial);
54 NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show in clear text. Leave uncommented for default Actisense format.
55 // NMEA2000.SetForwardOwnMessages();
56
60 NMEA2000.Open();
61 Serial.println("Device analyzer started.");
62 Serial.println(" - Analyzer will automatically print new list on list changes.");
63 Serial.println(" - Send 'u' to print latest list");
64}
65
66//*****************************************************************************
67void PrintUlongList(const char *prefix, const unsigned long * List) {
68 uint8_t i;
69 if ( List!=0 ) {
70 Serial.print(prefix);
71 for (i=0; List[i]!=0; i++) {
72 if (i>0) Serial.print(", ");
73 Serial.print(List[i]);
74 }
75 Serial.println();
76 }
77}
78
79//*****************************************************************************
80void PrintText(const char *Text, bool AddLineFeed=true) {
81 if ( Text!=0 ) Serial.print(Text);
82 if ( AddLineFeed ) Serial.println();
83}
84
85//*****************************************************************************
86void PrintDevice(const tNMEA2000::tDevice *pDevice) {
87 if ( pDevice == 0 ) return;
88
89 Serial.println("----------------------------------------------------------------------");
90 Serial.println(pDevice->GetModelID());
91 Serial.print(" Source: "); Serial.println(pDevice->GetSource());
92 Serial.print(" Manufacturer code: "); Serial.println(pDevice->GetManufacturerCode());
93 Serial.print(" Unique number: "); Serial.println(pDevice->GetUniqueNumber());
94 Serial.print(" Software version: "); Serial.println(pDevice->GetSwCode());
95 Serial.print(" Model version: "); Serial.println(pDevice->GetModelVersion());
96 Serial.print(" Manufacturer Information: "); PrintText(pDevice->GetManufacturerInformation());
97 Serial.print(" Installation description1: "); PrintText(pDevice->GetInstallationDescription1());
98 Serial.print(" Installation description2: "); PrintText(pDevice->GetInstallationDescription2());
99 PrintUlongList(" Transmit PGNs :",pDevice->GetTransmitPGNs());
100 PrintUlongList(" Receive PGNs :",pDevice->GetReceivePGNs());
101 Serial.println();
102}
103
104#define START_DELAY_IN_S 8
105//*****************************************************************************
106void ListDevices(bool force = false) {
107 static bool StartDelayDone=false;
108 static int StartDelayCount=0;
109 static unsigned long NextStartDelay=0;
110 if ( !StartDelayDone ) { // We let system first collect data to avoid printing all changes
111 if ( millis()>NextStartDelay ) {
112 if ( StartDelayCount==0 ) {
113 Serial.print("Reading device information from bus ");
114 NextStartDelay=millis();
115 }
116 Serial.print(".");
117 NextStartDelay+=1000;
118 StartDelayCount++;
119 if ( StartDelayCount>START_DELAY_IN_S ) {
120 StartDelayDone=true;
121 Serial.println();
122 }
123 }
124 return;
125 }
126 if ( !force && !pN2kDeviceList->ReadResetIsListUpdated() ) return;
127
128 Serial.println();
129 Serial.println("**********************************************************************");
130 for (uint8_t i = 0; i < N2kMaxBusDevices; i++) PrintDevice(pN2kDeviceList->FindDeviceBySource(i));
131}
132
133//*****************************************************************************
135 if (Serial.available()) {
136 char chr = Serial.read();
137 switch ( chr ) {
138 case 'u': ListDevices(true); break;
139 }
140 }
141}
142
143//*****************************************************************************
144void loop() {
146 ListDevices();
147 CheckCommand();
148}
void setup()
void CheckCommand()
void PrintDevice(const tNMEA2000::tDevice *pDevice)
#define START_DELAY_IN_S
void PrintUlongList(const char *prefix, const unsigned long *List)
void ListDevices(bool force=false)
void PrintLabelValWithConversionCheckUnDef(const char *label, T val, double(*ConvFunc)(double val)=0, bool AddLf=false)
tN2kDeviceList * pN2kDeviceList
void PrintText(const char *Text, bool AddLineFeed=true)
Stream * OutputStream
void loop()
uint32_t millis()
This File contains a.
#define N2kMaxBusDevices
Maximum allowed number of devices on the CAN BUS bus system is 254.
Definition: N2kDeviceList.h:38
bool N2kIsNA(double v)
Verify that the specified value is equal to "Not available".
Definition: N2kMsg.h:80
Automatic library selection according to the selected board.
tNMEA2000 & NMEA2000
Definition: NMEA2000_CAN.h:350
Helper class to keep track of all devices on the bus.
Definition: N2kDeviceList.h:72
bool ReadResetIsListUpdated()
Check if device list has updated.
const tNMEA2000::tDevice * FindDeviceBySource(uint8_t Source) const
Return device by it's bus source address.
This class represents a N2k device.
Definition: NMEA2000.h:481
uint8_t GetSource() const
Returns the Source Address of this device.
Definition: NMEA2000.h:505
virtual const unsigned long * GetTransmitPGNs() const
Get the list of transmitted PGNs from this device.
Definition: NMEA2000.h:607
virtual const unsigned long * GetReceivePGNs() const
Get the list of received PGNs from this device.
Definition: NMEA2000.h:609
virtual const char * GetManufacturerInformation() const
Get the manufacturer information from the configuration information of this device.
Definition: NMEA2000.h:598
virtual const char * GetModelVersion() const =0
Get the model version from the product information of this device.
uint32_t GetUniqueNumber() const
Get the unique Number from the Device Information.
Definition: NMEA2000.h:525
uint16_t GetManufacturerCode() const
Get the Manufacturer Code from the Device Information.
Definition: NMEA2000.h:530
virtual const char * GetSwCode() const =0
Get the Software version code from the product information of this device.
virtual const char * GetModelID() const =0
Get the model ID from the product information of this device.
virtual const char * GetInstallationDescription2() const
Get the installation description 2 from the configuration information of this device.
Definition: NMEA2000.h:604
virtual const char * GetInstallationDescription1() const
Get the installation description 1 from the configuration information of this device.
Definition: NMEA2000.h:601
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.
Definition: NMEA2000.cpp:745
void EnableForward(bool v=true)
Enable message forwarding to stream.
Definition: NMEA2000.h:2900
void ParseMessages()
Parse all incoming Messages.
Definition: NMEA2000.cpp:2577
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.
Definition: NMEA2000.cpp:1093
void SetForwardStream(N2kStream *_stream)
Set the Forward Stream object.
Definition: NMEA2000.h:2603
void SetN2kCANMsgBufSize(const uint8_t _MaxN2kCANMsgs)
Set incoming CAN message (tNMEA2000::N2kCANMsgBuf) buffer size.
Definition: NMEA2000.h:1816
bool Open()
Open the CAN device.
Definition: NMEA2000.cpp:1200
@ N2km_ListenAndNode
Definition: NMEA2000.h:699
@ fwdt_Text
Definition: NMEA2000.h:671
virtual void SetN2kCANReceiveFrameBufSize(const uint16_t _MaxCANReceiveFrames)
Set CAN receive frame buffer size.
Definition: NMEA2000.h:1870
void SetMode(tN2kMode _N2kMode, uint8_t _N2kSource=15)
Set the library mode and start source address.
Definition: NMEA2000.cpp:1177
void SetForwardType(tForwardType fwdType)
Set the Forward Streaming Type.
Definition: NMEA2000.h:2591