Datatype

Hi everyone!

I found a code snippet I want to use, but it includes something I don´t understand.
It is about AIS (Automated Identification System) and there is a library I want to use: GitHub - KimBP/AIS: An Arduino library able to decode AIS messages
I´ve loaded the library AIS.h into my sketch.
In the example the payload ("13u?etPv2;0n:dDPwUM1U1Cb069D") to decode is given as a fix String it seems. This line sets the payload:

AIS ais_msg("13u?etPv2;0n:dDPwUM1U1Cb069D");

Later in the code a function is called:

showROT(ais_msg);

The function is:

void showROT(AIS& ais_msg) {
  long ROT = ais_msg.get_rot();
  if(ROT<0){
    Serial.print("Returned ROT: "); Serial.print(-1*sq(ROT/4.733)); Serial.println("°/min");    
  }
  else{
    Serial.print("Returned ROT: "); Serial.print(sq(ROT/4.733)); Serial.println("°/min");    
  }
}

This works fine with the fix payload. But how can I change the payload in "ais_msg"?

I hope you undertand what I mean...

Thanks for your help!

Please post the complete program so that what you are doing can be seen in context

The Code is given in the examples for the library. To make it short: How do I pass defferent payloads to the functions showMMSI(ais_msg); showSOG(ais_msg); ... ?

#include <Arduino.h>
#include <AIS.h>

/* Coming from util.h */
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
                   ((x)<< 8 & 0x00FF0000UL) | \
                   ((x)>> 8 & 0x0000FF00UL) | \
                   ((x)>>24 & 0x000000FFUL) )
#define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) )


AIS ais_msg("14eG;o@034o8sd<L9i:a;WF>062D");

void printDegrees(long min4)
{
//    Serial.print(min4); Serial.print(" (");Serial.print(min4,16);Serial.println(")");
    long intPart = min4 / 60L;
    long fracPart = intPart % 10000L;
    if (fracPart < 0)
      fracPart = -fracPart;
    char frac[6];
    sprintf(frac, "%04ld", fracPart);
    Serial.print(intPart/10000L);Serial.print(".");Serial.print(frac);
}

void showMMSI(AIS& ais_msg) {
  unsigned long mmsi = ais_msg.get_mmsi();
  Serial.print("Returned MMSI: ");
  Serial.print(mmsi);
  Serial.print(" ("); Serial.print(mmsi, 16); Serial.print(" )");
  Serial.println("");
}

void showSOG(AIS& ais_msg) {
    unsigned int SOG = ais_msg.get_SOG();
    Serial.print("Returned SOG: ");
    Serial.print( (SOG) / 10 ); Serial.print("."); Serial.print( (SOG) % 10 ); Serial.println(" nm"); 
}

void showCOG(AIS& ais_msg) {
    unsigned int COG = ais_msg.get_COG();
    Serial.print("Returned COG: ");
    Serial.print( (COG) / 10 ); Serial.print("."); Serial.print( (COG) % 10 ); Serial.println(" degrees"); 
}

void showLatitude(AIS& ais_msg) {
    long LAT = ais_msg.get_latitude();
    Serial.print("Returned LAT: "); printDegrees(LAT); Serial.println(" degrees"); 
}

void showLongitude(AIS& ais_msg) {
    long LONG = ais_msg.get_longitude();
    Serial.print("Returned LONG: "); printDegrees(LONG); Serial.println(" degrees"); 
}

void setup() {
  Serial.begin(115200);

//  AIS ais_msg("14eG;o@034o8sd<L9i:a;WF>062D");
  /* Console output (!AIVDM,1,1,,A,14eG;o@034o8sd<L9i:a;WF>062D,0*7D)
   * 
   * Returned MMSI: 316001245 (12D5CBDD )
   * Returned SOG: 19.6 nm
   * Returned COG: 235.0 degrees
   * Returned LAT: 49.2002 degrees
   * Returned LONG: -123.8777 degrees
   */
  //AIS ais_msg("177KQJ5000G?tO`K>RA1wUbN0TKH");
  /*  Console output: (!AIVDM,1,1,,B,177KQJ5000G?tO`K>RA1wUbN0TKH,0*5C)
   *   
   *  Returned MMSI: 477553000 (1C76E168 )
   *  Returned SOG: 0.0 nm
   *  Returned COG: 51.0 degrees
   *  Returned LAT: 47.5828 degrees
   *  Returned LONG: -122.3458 degrees
   */
//
//  showMMSI(ais_msg);
//  showSOG(ais_msg);
//  showCOG(ais_msg);
//  showLatitude(ais_msg);
//  showLongitude(ais_msg);
}

void loop() {

  // ais_msg should be dynamic in loop to I can read from serial and pass to functions showMMSI(aus_msg),...
  showMMSI(ais_msg);
  showSOG(ais_msg);
  showCOG(ais_msg);
  showLatitude(ais_msg);
  showLongitude(ais_msg);
  delay(1000);

}

Looking at the library's source code, I don't see a method for changing the internal message value once an AIS object is instantiated by the constructor. You can, of course, instantiate multiple objects of this type with different payload strings.

I think thats not an option. I read the AIS data live from a receiver and the payload will change dynamically. So probably this library is not what I was looking for.