33#define StartOfText 0x02
35#define MsgTypeN2k 0x93
37#define MaxActisenseMsgBuf 400
42#if defined(__GNUC__) && defined (__BYTE_ORDER__)
43#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
44#define HOST_IS_BIG_ENDIAN
60tN2kMsg::tN2kMsg(
unsigned char _Source,
unsigned char _Priority,
unsigned long _PGN,
int _DataLen) {
61 Init(_Priority,_PGN,_Source,255);
75void tN2kMsg::Init(
unsigned char _Priority,
unsigned long _PGN,
unsigned char _Source,
unsigned char _Destination) {
81#if !defined(N2K_NO_ISO_MULTI_PACKET_SUPPORT)
224 char c = toupper((
int)*str);
225 *buf=(c >= 0x20 && c <= 0x5F) ? c :
'?';
229 if ( len>0 ) memset(buf,
'@',len);
235 int len=(str!=0?strlen(str):0);
256 return Data[Index++];
361 bool nullReached =
false;
363 if ((
size_t)Index+Length<=(
size_t)
DataLen) {
364 for (
size_t i=0; i<Length; i++) {
367 if (vb == 0x00 || vb ==
'@') {
385bool tN2kMsg::GetStr(
size_t StrBufSize,
char *StrBuf,
size_t Length,
unsigned char nulChar,
int &Index)
const {
387 bool nullReached =
false;
388 if ( StrBufSize==0 || StrBuf==0 ) {
393 if ((
size_t)Index+Length<=(
size_t)
DataLen) {
395 for (i=0; i<Length && i<StrBufSize-1; i++) {
398 if (vb == 0x00 || vb == nulChar ) {
409 for (;i<Length;i++)
GetByte(Index);
410 for (;i<StrBufSize;i++) StrBuf[i] =
'\0';
419 if ( Type!=0x01 ) { StrBufSize=0;
return false; }
421 GetStr(StrBufSize,StrBuf,Len,0xff,Index);
433 if ((
size_t)Index+Length<=(
size_t)
DataLen) {
435 memcpy(buf,
Data+Index,Length);
481 return (val << 8) | (val >> 8);
492 return ((val << 24)) |
493 ((val << 8) & 0xff0000UL) |
494 ((val >> 8) & 0xff00UL) |
505 return ((val << 56)) |
506 ((val << 40) & 0xff000000000000ULL) |
507 ((val << 24) & 0xff0000000000ULL) |
508 ((val << 8) & 0xff00000000ULL) |
509 ((val >> 8) & 0xff000000ULL) |
510 ((val >> 24) & 0xff0000ULL) |
511 ((val >> 40) & 0xff00ULL) |
522T
GetBuf(
size_t len,
int& index,
const unsigned char* buf) {
527 memcpy(&v, &buf[index], len);
530#if defined(HOST_IS_BIG_ENDIAN)
539void SetBuf(T v,
size_t len,
int& index,
unsigned char* buf) {
540#if defined(HOST_IS_BIG_ENDIAN)
546 memcpy(&buf[index], &v, len);
552 if (
sizeof(
double)==8 && !
N2kIsNA(v) ) {
555 SetBuf(iv, 8, index, buf);
569 SetBuf(iv, 4, index, buf);
572#define N2kInt8OR 0x7e
573#define N2kUInt8OR 0xfe
574#define N2kInt16OR 0x7ffe
575#define N2kUInt16OR 0xfffe
576#define N2kInt32OR 0x7ffffffe
577#define N2kUInt32OR 0xfffffffe
579#define N2kInt32Min -2147483648L
580#define N2kInt24OR 8388606L
581#define N2kInt24Min -8388608L
582#define N2kInt16Min -32768
583#define N2kInt8Min -128
589 if (
sizeof(
double)<8 ) {
590 double fp=precision*1e6;
600 SetBuf(vll, 8, index, buf);
605 double vd=
round((v/precision));
607 SetBuf<int32_t>(vi, 4, index, buf);
612 double vd=
round((v/precision));
614 SetBuf<uint32_t>(vi, 4, index, buf);
619 double vd=
round((v/precision));
621 SetBuf<int32_t>(vi, 3, index, buf);
626 return GetBuf<int16_t>(2, index, buf);
631 return GetBuf<uint16_t>(2, index, buf);
636 return GetBuf<uint32_t>(3, index, buf);
641 return GetBuf<uint32_t>(4, index, buf);
646 return GetBuf<uint64_t>(8, index, buf);
651 int8_t vl = GetBuf<int8_t>(1, index, buf);
652 if (vl==0x7f)
return def;
654 return vl * precision;
659 uint8_t vl = GetBuf<uint8_t>(1, index, buf);
660 if (vl==0xff)
return def;
662 return vl * precision;
667 int16_t vl = GetBuf<int16_t>(2, index, buf);
668 if (vl==0x7fff)
return def;
670 return vl * precision;
675 uint16_t vl = GetBuf<uint16_t>(2, index, buf);
676 if (vl==0xffff)
return def;
678 return vl * precision;
683 int64_t vl = GetBuf<int64_t>(8, index, buf);
684 if (vl==0x7fffffffffffffffLL)
return def;
686 return vl * precision;
691 int64_t vl = GetBuf<int64_t>(8, index, buf);
694 if (
sizeof(
double)==8 && !
N2kIsNA(vl) ) {
696 if ( isnan(ret) ) ret=def;
704float GetBufFloat(
int &index,
const unsigned char *buf,
float def) {
705 int32_t vl = GetBuf<int32_t>(4, index, buf);
709 if ( isnan(ret) ) ret=def;
718 int32_t vl = GetBuf<int32_t>(3, index, buf);
719 if (vl==0x007fffff)
return def;
721 return vl * precision;
726 int32_t vl = GetBuf<int32_t>(4, index, buf);
727 if (vl==0x7fffffff)
return def;
729 return vl * precision;
734 uint32_t vl = GetBuf<uint32_t>(4, index, buf);
735 if (vl==0xffffffff)
return def;
737 return vl * precision;
742 double vd=
round((v/precision));
744 SetBuf(vi, 2, index, buf);
749 double vd=
round((v/precision));
751 SetBuf(vi, 2, index, buf);
756 double vd=
round((v/precision));
758 SetBuf(vi, 1, index, buf);
763 double vd=
round((v/precision));
765 SetBuf(vi, 1, index, buf);
794void SetBufStr(
const char *str,
int len,
int &index,
unsigned char *buf,
bool UsePgm,
unsigned char fillChar) {
797 for (; i<len && str[i]!=0; i++, index++) {
801 for (; i<len && str[i]!=0; i++, index++) {
805 for (; i<len; i++, index++) {
814 for(
int i = 0; i<len; i++) {
815 if (i>0) { port->
print(
F(
",")); };
817 port->
print(pData[i], 16);
825 if (port==0 || !
IsValid())
return;
842 buf[idx++]=byteToAdd;
845 if (byteToAdd ==
Escape) {
854 unsigned long _PGN=
PGN;
855 unsigned long _MsgTime=
MsgTime;
861 if (port==0 || !
IsValid())
return;
864 ActisenseMsgBuf[msgIdx++]=
Escape;
885 CheckSum = (uint8_t)((byteSum == 0) ? 0 : (256 - byteSum));
886 ActisenseMsgBuf[msgIdx++]=CheckSum;
887 if (CheckSum==
Escape) ActisenseMsgBuf[msgIdx++]=CheckSum;
889 ActisenseMsgBuf[msgIdx++] =
Escape;
893 port->
write(ActisenseMsgBuf,msgIdx);
#define pgm_read_byte(var)
void AddByteEscapedToBuf(unsigned char byteToAdd, uint8_t &idx, unsigned char *buf, int &byteSum)
void SetBufDouble(double v, int &index, unsigned char *buf)
Writes a double value into a byte array buffer
uint16_t GetBuf2ByteUInt(int &index, const unsigned char *buf)
Extracts 2 bytes out of the given buffer and converts it to an integer value.
double GetBuf8ByteDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 8 bytes out of the given buffer and converts it to a double value.
int16_t GetBuf2ByteInt(int &index, const unsigned char *buf)
Extracts 2 bytes out of the given buffer and converts it to an integer value.
void SetBuf2ByteInt(int16_t v, int &index, unsigned char *buf)
Writes an integer value into a byte array buffer using 2 bytes To write a integer value into a certai...
double GetBuf1ByteDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts the specified numbers of bytes out of the given buffer and converts it to a double value.
void SetBuf2ByteUDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double unsigned value into a byte array buffer using 2 bytes.
void SetBufStr(const char *str, int len, int &index, unsigned char *buf, bool UsePgm, unsigned char fillChar)
Writes a string into a byte array buffer.
double GetBuf2ByteUDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 2 bytes out of the given buffer and converts it to a double value.
void SetBuf(T v, size_t len, int &index, unsigned char *buf)
double GetBuf2ByteDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 2 bytes out of the given buffer and converts it to a double value.
double GetBuf4ByteUDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 4 bytes out of the given buffer and converts it to a double value.
double GetBufDouble(int &index, const unsigned char *buf, double def)
Extracts bytes out of the given buffer and converts it to a double value.
void SetBuf3ByteDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double signed value into a byte array buffer using 3 bytes.
void SetBuf2ByteDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double signed value into a byte array buffer using 2 bytes.
void PrintBuf(N2kStream *port, unsigned char len, const unsigned char *pData, bool AddLF)
Print out a buffer (byte array)
#define MaxActisenseMsgBuf
float GetBufFloat(int &index, const unsigned char *buf, float def)
Extracts the specified numbers of bytes out of the given buffer and converts it to an float value.
double GetBuf1ByteUDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 1 byte out of the given buffer and converts it to a double value.
T GetBuf(size_t len, int &index, const unsigned char *buf)
void SetBufFloat(float v, int &index, unsigned char *buf)
Writes a float value into a byte array buffer.
uint64_t GetBuf8ByteUInt(int &index, const unsigned char *buf)
Extracts 8 bytes out of the given buffer and converts it to an integer value.
void SetBuf4ByteDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double signed value into a byte array buffer using 4 bytes.
void SetBufUInt64(uint64_t v, int &index, unsigned char *buf)
Writes an unsigned integer 64bit value into a byte array buffer using 8 bytes To write a integer valu...
void SetBuf4ByteUDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double unsigned value into a byte array buffer using 4 bytes.
void SetBuf3ByteInt(int32_t v, int &index, unsigned char *buf)
Writes an integer value into a byte array buffer using 3 bytes To write a integer value into a certai...
void SetBuf2ByteUInt(uint16_t v, int &index, unsigned char *buf)
Writes an unsigned integer value into a byte array buffer using 2 bytes.
void SetBuf8ByteDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double signed value into a byte array buffer using 8 bytes.
void SetBuf1ByteUDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double unsigned value into a byte array buffer using 1 byte.
double GetBuf4ByteDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 4 bytes out of the given buffer and converts it to a double value.
uint32_t GetBuf4ByteUInt(int &index, const unsigned char *buf)
Extracts 4 bytes out of the given buffer and converts it to an integer value.
void SetBuf4ByteUInt(uint32_t v, int &index, unsigned char *buf)
Writes an unsigned integer value into a byte array buffer u ing 4 bytes To write a integer value into...
void SetBuf1ByteDouble(double v, double precision, int &index, unsigned char *buf)
Writes a double signed value into a byte array buffer using 1 byte.
uint32_t GetBuf3ByteUInt(int &index, const unsigned char *buf)
Extracts 3 bytes out of the given buffer and converts it to an integer value.
double GetBuf3ByteDouble(double precision, int &index, const unsigned char *buf, double def)
Extracts 3 bytes out of the given buffer and converts it to a double value.
This File contains the class tN2kMsg and all necessary functions to handle a NMEA2000 Message.
const uint32_t N2kUInt32NA
Constant "Not Available" for a unsigned 32bit int value.
const int8_t N2kInt8NA
Constant "Not Available" for a signed 8bit value.
const int16_t N2kInt16NA
Constant "Not Available" for a signed 16bit int value.
const int64_t N2kInt64NA
Constant "Not Available" for a signed 64bit int value.
const int32_t N2kInt32NA
Constant "Not Available" for a signed 32bit int value.
const uint16_t N2kUInt16NA
Constant "Not Available" for a unsigned 16bit int value.
const uint8_t N2kUInt8NA
Constant "Not Available" for a unsigned 8bit int value.
bool N2kIsNA(double v)
Verify that the specified value is equal to "Not available".
The file contains function and classes for best timing performance.
Streaming Class to handle all Streams generated by the Library.
size_t print(const char *str)
Print string to stream.
virtual size_t write(const uint8_t *data, size_t size)=0
Write data to stream.
size_t println(const char *str)
Print string and newline to stream.
unsigned char Data[MaxDataLen]
Byte array which carries all the data of the NMEA2000 message.
void Add2ByteUDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 2 bytes.
void AddFloat(float v, float UndefVal=N2kFloatNA)
Add float value to the buffer This method adds a float value to the message buffer using SetBufFloat ...
void AddStr(const char *str, int len, bool UsePgm=false, unsigned char fillChar=0xff)
Add string value to the buffer The string will be added to the end (indicated by DataLen) of the byte...
void Add2ByteUInt(uint16_t v)
Add unsigned integer value to the buffer using 2 bytes The value will be added to the end (indicated ...
double Get3ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 3 bytes out of Data The fixed point integer mechanism is used.
double Get4ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 4 bytes out of Data The fixed point integer mechanism is used.
double Get1ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 1 bytes out of Data The fixed point integer mechanism is used.
bool GetVarStr(size_t &StrBufSize, char *StrBuf, int &Index) const
Get a string out of Data This method determines the length of the string by it self,...
void Add4ByteUDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 4 bytes.
void Add2ByteInt(int16_t v)
Add integer value to the buffer using 2 bytes The value will be added to the end (indicated by DataLe...
void Add4ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 4 bytes.
void Add3ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 3 bytes.
bool IsValid() const
Checks if the Message Meta Content is valid Basic check if the message object is valid,...
void AddByte(unsigned char v)
Add byte value to the buffer The byte will be added to the end (indicated by DataLen) of the byte arr...
void ResetData()
Fills the whole data buffer with 0xff.
void Add2ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 2 bytes.
void Add1ByteUDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 1 byte.
bool Set2ByteUInt(uint16_t v, int &Index)
Set a 2byte unsigned integer in Data.
void Add1ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 1 byte.
void Print(N2kStream *port, bool NoData=false) const
Print out the whole content of the N2kMsg Object.
void AddUInt64(uint64_t v)
Add unsigned integer value to the buffer using 8 bytes The value will be added to the end (indicated ...
void AddBuf(const void *buf, size_t bufLen)
Add byte array to the buffer.
uint16_t Get2ByteUInt(int &Index, uint16_t def=0xffff) const
Get an unsigned integer from 2 bytes out of Data.
double Get2ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 2 bytes out of Data The fixed point integer mechanism is used.
unsigned char Source
Source of the NMEA2000 message.
void Add8ByteDouble(double v, double precision, double UndefVal=N2kDoubleNA)
Add double value to the buffer using 8 bytes.
static const int MaxDataLen
Maximum number of bytes that can be stored in the data buffer With fast packet the first frame can ha...
float GetFloat(int &Index, float def=N2kFloatNA) const
Get a float out of Data.
double Get2ByteUDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 2 bytes out of Data The fixed point integer mechanism is used.
void AddVarStr(const char *str, bool UsePgm=false)
Add string value to the buffer This method determines the length of the string by it self using strle...
unsigned char Priority
Priority of the NMEA2000 message.
bool SetByte(uint8_t v, int &Index)
Set a byte in Data.
uint32_t Get3ByteUInt(int &Index, uint32_t def=0xffffffff) const
Get an unsigned integer from 3 bytes out of Data.
double Get1ByteUDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 1 bytes out of Data The fixed point integer mechanism is used.
void Add3ByteInt(int32_t v)
Add integer value to the buffer using 3 bytes The value will be added to the end (indicated by DataLe...
virtual void Clear()
Clears the content of the N2kMsg object The method sets the PGN, DataLen and MsgTime to zero.
unsigned long MsgTime
timestamp (ms since start [max 49days]) of the NMEA2000 message
void Add4ByteUInt(uint32_t v)
Add unsigned integer value to the buffer using 4 bytes The value will be added to the end (indicated ...
bool TPMessage
Message is a MultiPacket Message.
void SetPGN(unsigned long _PGN)
Set the Parameter Group Number of the message *.
unsigned char GetByte(int &Index) const
Get the value from a byte out of Data.
uint64_t GetUInt64(int &Index, uint64_t def=0xffffffffffffffffULL) const
Get an unsigned integer from 8 bytes out of Data.
int16_t Get2ByteInt(int &Index, int16_t def=0x7fff) const
Get an integer from 2 bytes out of Data.
bool GetBuf(void *buf, size_t Length, int &Index) const
Get a byte array out of Data.
bool GetStr(char *StrBuf, size_t Length, int &Index) const
Get a string out of Data.
void AddAISStr(const char *str, int len)
Add string value to the buffer after filtering characters as defined in ITU-R M.1371-1 The string wil...
double Get4ByteUDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 4 bytes out of Data The fixed point integer mechanism is used.
void Init(unsigned char _Priority, unsigned long _PGN, unsigned char _Source, unsigned char _Destination=0xff)
Initialisation of the N2kMsg object.
tN2kMsg(unsigned char _Source=15, unsigned char _Priority=6, unsigned long _PGN=0, int _DataLen=0)
Construct a new t N2k Msg object.
void SendInActisenseFormat(N2kStream *port) const
Print out the whole content of the N2kMsg Object using the Actisense Format.
double Get8ByteDouble(double precision, int &Index, double def=N2kDoubleNA) const
Get a double from 8 bytes out of Data The fixed point integer mechanism is used.
uint32_t Get4ByteUInt(int &Index, uint32_t def=0xffffffff) const
Get an unsigned integer from 4 bytes out of Data.
int DataLen
Number of bytes already stored in tN2kMsg::Data of this message.
unsigned long PGN
Parameter Group Number (PGN) of the NMEA2000 message.
unsigned char Destination
Destination of the NMEA2000 message.