To support debugging of problems, that can arise during CAN communication, PCAN-Basic can generate a log file, containing a protocol of all API function calls. There are two different ways to configure and activate this logging functionality:
Using the API
Using the Windows Registry
In order to configure the log functionality, the PCAN-Basic API provides 3 parameters that can be configured using the SetValue method. These parameters are:
PCAN_LOG_LOCATION, to set the location of the log file.
PCAN_LOG_CONFIGURE, to configure the content of the log file.
PCAN_LOG_STATUS, to enable/disable logging.
The following example shows how the PCAN-Basic is configured to log all possible information (LOG_FUNCTION_ALL), and to store the log file on the desktop of an user called "admin".
TPCANStatus result;
System.Text.StringBuilder strMsg;
strMsg = new System.Text.StringBuilder(256);
// Configures the data in the log file.
//
uint iBuffer = PCANBasic.LOG_FUNCTION_ALL;
result = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_CONFIGURE, ref iBuffer, sizeof(UInt32));
if (result != TPCANStatus.PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(result, 0, strMsg);
Console.WriteLine(strMsg.ToString());
}
// Configures the path of the log file.
//
string buffer = "C:\\users\\admin\\desktop";
result = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_LOCATION, buffer, (UInt32)buffer.Length);
if (result != TPCANStatus.PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(result, 0, strMsg);
Console.WriteLine(strMsg.ToString());
}
// Configures the status of the log file
//
iBuffer = PCANBasic.PCAN_PARAMETER_ON;
result = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_STATUS, ref iBuffer, sizeof(UInt32));
if (result != TPCANStatus.PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic.GetErrorText(result, 0, strMsg);
Console.WriteLine(strMsg.ToString());
}Dim result As TPCANStatus
Dim strMsg As System.Text.StringBuilder
strMsg = New System.Text.StringBuilder(256)
' Configures the data in the log file.
'
Dim iBuffer As UInt32 = PCANBasic.LOG_FUNCTION_ALL
result = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_CONFIGURE, iBuffer, CType(System.Runtime.InteropServices.Marshal.SizeOf(iBuffer), UInteger))
If result <> TPCANStatus.PCAN_ERROR_OK Then
' An error occurred, get a text describing the error And show it
'
PCANBasic.GetErrorText(result, 0, strMsg)
Console.WriteLine(strMsg.ToString())
End If
' Configures the path of the log file.
'
Dim Buffer As String = "C:\\users\\admin\\desktop"
result = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_LOCATION, Buffer, CType(Buffer.Length, UInteger))
If result <> TPCANStatus.PCAN_ERROR_OK Then
' An error occurred, get a text describing the error And show it
'
PCANBasic.GetErrorText(result, 0, strMsg)
Console.WriteLine(strMsg.ToString())
End If
' Configures the status of the log file
'
iBuffer = PCANBasic.PCAN_PARAMETER_ON
result = PCANBasic.SetValue(PCANBasic.PCAN_NONEBUS, TPCANParameter.PCAN_LOG_STATUS, iBuffer, CType(System.Runtime.InteropServices.Marshal.SizeOf(iBuffer), UInteger))
If result <> TPCANStatus.PCAN_ERROR_OK Then
' An error occurred, get a text describing the error And show it
'
PCANBasic.GetErrorText(result, 0, strMsg)
Console.WriteLine(strMsg.ToString())
End IfTPCANStatus result;
System::Text::StringBuilder^ strMsg;
strMsg = gcnew System::Text::StringBuilder(256);
// Configures the data in the log file.
//
UInt32 iBuffer = PCANBasic::LOG_FUNCTION_ALL;
result = PCANBasic::SetValue(PCANBasic::PCAN_NONEBUS, TPCANParameter::PCAN_LOG_CONFIGURE, iBuffer, sizeof(UInt32));
if (result != TPCANStatus::PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
}
// Configures the path of the log file.
//
String^ buffer = "C:\\users\\admin\\desktop";
result = PCANBasic::SetValue(PCANBasic::PCAN_NONEBUS, TPCANParameter::PCAN_LOG_LOCATION, buffer, buffer->Length);
if (result != TPCANStatus::PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
}
// Configures the status of the log file
//
iBuffer = PCANBasic::PCAN_PARAMETER_ON;
result = PCANBasic::SetValue(PCANBasic::PCAN_NONEBUS, TPCANParameter::PCAN_LOG_STATUS, iBuffer, sizeof(UInt32));
if (result != TPCANStatus::PCAN_ERROR_OK)
{
// An error occurred, get a text describing the error and show it
//
PCANBasic::GetErrorText(result, 0, strMsg);
Console::WriteLine(strMsg->ToString());
}In order to enable the log file generation, the following registry key must be created:
HKEY_CURRENT_USER\SOFTWARE\PEAK-System\PCAN-Basic\Log
The existence of this key is analogous to use the SetValue method to set the PCAN_LOG_STATUS parameter to "on". If this key is not present, then no log file is generated.
If no further configuration is made, then the default values for PCAN_LOG_LOCATION , and PCAN_LOG_CONFIGURE are used. In order to configure the location and content of the log file, two registry values are used:
Flags: This is a UInt32 value, that represents a logical OR operation between the values LOG_FUNCTION_* that are wanted to be included within the logging data. The value LOG_FUNCTION_ALL causes logging all possible information.
Path: This is a String value, that represents the path to a folder in the computer, where the log file will be created.
[HKEY_CURRENT_USER\SOFTWARE\PEAK-System\PCAN-Basic\Log]
"Flags"=dword:00000007
"Path"="C:\\Users\\admin\\desktop"
The registry key should be deleted (or renamed) after a debug session is done. If the key is leaved, all PCAN-Basic applications running under the same user account will remain writing data to their log files, generating in this way huge text files that consume hard-disk space unnecessarily.