Obtain nmea2000 PGN code of NMEA2000 C++ library by Timo

Hi, I need to get the pgn number from the messages read on an NMEA bus with the Timo NMEA2000 library used on a raspberry pi, but I can't understand which library commands to use. can you help me please?

I know it is an old thread, but to who ever find this in the furture:
With an Arduino MKR 1010 Wifi and a Arduno Can shield.
You could do something like this:

#include <Arduino.h>
#define USE_N2K_CAN 1
#define N2k_SPI_CS_PIN 3
#define N2k_CAN_INT_PIN 7
#define USE_MCP_CAN_CLOCK_SET 16
#include <NMEA2000_CAN.h>  
#include <N2kMessages.h>

typedef struct {
  unsigned long PGN;
  void (*Handler)(const tN2kMsg &N2kMsg); 
} tNMEA2000Handler;
int oiltempK=0;

Stream *OutputStream;

void HandleNMEA2000Msg(const tN2kMsg &N2kMsg);

void setup() {
  // Set Product information
  NMEA2000.SetProductInformation("02", // Manufacturer's Model serial code
                                 101, // Manufacturer's product code
                                 "Model ID",  // Manufacturer's Model ID
                                 "1.1.0.21 (2016-12-31)",  // Manufacturer's Software version code
                                 "1.1.0.0 (2016-12-31)" // Manufacturer's Model version
                                 );
  // Set device information
  NMEA2000.SetDeviceInformation(112255, // Unique number. Use e.g. Serial number.
                                130, // Device function=Temperature. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
                                75, // Device class=Sensor Communication Interface. See codes on  http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
                                2040 // Just choosen free from code list on http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf                               
                               );                        
  Serial.begin(115200);
  OutputStream=&Serial;
  Serial.print("Starting\n");
  NMEA2000.SetForwardStream(&Serial);
  NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); 
  NMEA2000.SetMode(tNMEA2000::N2km_ListenOnly,25);
  NMEA2000.EnableForward(true);
  NMEA2000.SetMsgHandler(HandleNMEA2000Msg);
  NMEA2000.Open();
}


void loop() {
  NMEA2000.ParseMessages();
}

void HandleNMEA2000Msg(const tN2kMsg &N2kMsg) {
  int source = N2kMsg.Data[1];
  OutputStream->print("PGN: "); 
  OutputStream->println(N2kMsg.PGN);
}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.