Bachelorthesis/Modbus-CAPL/include/CAPL/include/Common.cin
2014-06-20 10:45:41 +00:00

290 lines
7.3 KiB
Plaintext

/*@!Encoding:1252*/
// This file contains very common functions:
// - writeDbg() which writes messages only if the global debug level 'OutputDebugLevel' is high enough
// - bin_to_strhex() which converts byte[]s to a hex string
// - hbin_to_strhex() which converts only the lower nibble of the byte[]s to a hex string
// - dbin_to_strhex() which converts word[]s to a hex string
variables
{
enum DebugLvl {
Debug = 0x00,
AlgoDebug = 0x02,
ConnDebug = 0x04,
MbDebug = 0x07,
Info = 0x10,
AlgoInfo = 0x12,
ConnInfo = 0x14,
MbInfo = 0x17,
Warning = 0x20,
AlgoWarning = 0x22,
ConnWarning = 0x24,
MbWarning = 0x27,
Error = 0x30,
AlgoError = 0x32,
ConnError = 0x34,
MbError = 0x37,
Mute = 0xFF
};
char DebugLvlStr[4][8] = {"Debug", "Info", "Warning", "Error"};
enum DebugLvl OutputDebugLevel = Debug;
}
// This method creates a format string in 'msg' which can then be passed to writeLineEx().
// This format string contains the node name and the debug level
/// <writeDbg>
void writeDbgFormat(byte lVl, char msg[], char format[])
{
strncpy(msg, "<%NODE_NAME%> ", elCount(msg));
strncat(msg, DebugLvlStr[lVl], elCount(msg));
strncat(msg, ": ", elCount(msg));
strncat(msg, format, elCount(msg)-strlen(msg));
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[])
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], char string[])
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, string);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], char string1[], char string2[])
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, string1, string2);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num, char string[])
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num, string);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], char string[], long num)
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, string, num);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num1)
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num1);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num1, long num2, char string[])
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num1, num2, string);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num1, long num2)
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num1, num2);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num1, long num2, long num3)
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num1, num2, num3);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num1, long num2, long num3, long num4)
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num1, num2, num3, num4);
}
// This method prints the specified arguments to CAPL/Write Window if the debug level is high enough
/// <writeDbg>
void writeDbg(enum DebugLvl lvl, char format[], long num1, long num2, long num3, long num4, long num5, long num6)
{
char msg[500];
byte lVl;
if (lvl < OutputDebugLevel)
return;
lVl = (byte)lvl >> 4;
writeDbgFormat(lVl, msg, format);
writeLineEx(1, lVl, msg, num1, num2, num3, num4, num5, num6);
}
// This method converts the bytes in 'bin' to a readable string in hex format ("XX XX XX XX")
// You have to allocate 60 bytes for 'result'
void bin_to_strhex(byte bin[], char result[])
{
char hex_str[17] = "0123456789ABCDEF";
word i;
word binsz;
binsz = elCount(bin);
if (binsz > 20)
binsz = 20;
for (i = 0; i < binsz; i++)
{
result[i * 3 + 0] = hex_str[bin[i] >> 4 ];
result[i * 3 + 1] = hex_str[bin[i] & 0x0F];
result[i * 3 + 2] = ' ';
}
if (elCount(bin) > 20) // trailing "..."
{
result[56] = '.';
result[57] = '.';
result[58] = '.';
}
result[binsz * 3 - 1] = 0;
}
// This method converts the lower nibbles of the bytes in 'bin' to a readable string in hex format ("X X X X")
// You have to allocate 40 bytes for 'result'
void hbin_to_strhex(byte bin[], char result[])
{
char hex_str[17] = "0123456789ABCDEF";
word i;
word binsz;
binsz = elCount(bin);
if (binsz > 20)
binsz = 20;
for (i = 0; i < binsz; i++)
{
result[i * 2 + 0] = hex_str[bin[i] & 0x0F];
result[i * 2 + 1] = ' ';
}
if (elCount(bin) > 20) // trailing "..."
{
result[36] = '.';
result[37] = '.';
result[38] = '.';
}
result[binsz * 2 - 1] = 0;
}
// This method converts the words in 'bin' to a readable string in hex format ("XXXX XXXX XXXX XXXX")
// You have to allocate 100 bytes for 'result'
void dbin_to_strhex(word bin[], char result[])
{
char hex_str[17] = "0123456789ABCDEF";
word i;
word binsz;
byte offset;
binsz = elCount(bin);
if (binsz > 20)
binsz = 20;
for (i = 0; i < binsz; i++)
{
result[i * 5 + 0] = hex_str[(bin[i] >> 12) & 0x0F];
result[i * 5 + 1] = hex_str[(bin[i] >> 8) & 0x0F];
result[i * 5 + 2] = hex_str[(bin[i] >> 4) & 0x0F];
result[i * 5 + 3] = hex_str[(bin[i] ) & 0x0F];
result[i * 5 + 4] = ' ';
}
if (elCount(bin) > 20) // trailing "..."
{
result[96] = '.';
result[97] = '.';
result[98] = '.';
}
result[(byte)(binsz * 2.5) - 1] = 0;
}