ReceiveEvent
This parameter passes a thread synchronization event handle to the underlying API. This event is fired (its state is set to “signaled”) when CAN data is placed into the receive queue of a PCAN Channel.
Events are typically used when an application separates tasks into different threads. A thread, which waits for an event to occur, does not affect the normal
execution of an application.
The event is fired when its state is “unsignaled” and data is received and placed in the Channel's receive queue. After an application is notified about new data,
it must read the receive queue until it is empty, and finally reset the event (if using a manual reset event).
It is available since PCAN-Basic version 1.0.0.
Windows: This parameter is read/write. It can be configured and retrieved.
Linux: This parameter can only be read. It cannot be modified.
Attempting to set this parameter on Linux results in an error. Due to differences between the two platforms, the event object for use under Linux must
be created and configured at initialization time by the API. To use ReceiveEvent under Linux, the event handle must simply be retrieved
by calling GetValue
with this parameter.
This parameter can be enabled/disabled by using numeric value in the range of [0...MaxValue]),
that represents a pointer to a synchronization object.
Windows: The default state is disabled (0).
Linux: The handle to a valid synchronization object.
Get: The PCAN Channel must be initialized before using this parameter.
Set:
The following code shows how to configure a receive event for a USB Channel on Windows and Linux systems:
PcanChannel channelUsed = PcanChannel.Usb01;
System.Threading.EventWaitHandle receiveEvent = new System.Threading.EventWaitHandle(false, System.Threading.EventResetMode.AutoReset);
if (Api.Initialize(channelUsed, Bitrate.Pcan500) == PcanStatus.OK)
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
if (Api.SetValue(channelUsed, PcanParameter.ReceiveEvent, (uint)receiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32()) == PcanStatus.OK)
{
Console.WriteLine($"A receive event was configured for {channelUsed}.");
}
else
{
Console.WriteLine($"Error configuring a receive event on channel {channelUsed}.");
}
}
else
{
uint eventHandle;
if (Api.GetValue(channelUsed, PcanParameter.ReceiveEvent, out eventHandle) == PcanStatus.OK)
{
receiveEvent.SafeWaitHandle.Close();
receiveEvent.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(new IntPtr(eventHandle), false);
Console.WriteLine($"A receive event was configured for {channelUsed}.");
}
else
{
Console.WriteLine($"Error retrieving the receive event on channel {channelUsed}.");
}
}
Api.Uninitialize(channelUsed);
}
Dim channelUsed As PcanChannel = PcanChannel.Usb01
Dim receiveEvent As Threading.EventWaitHandle = New Threading.EventWaitHandle(False, Threading.EventResetMode.AutoReset)
If Api.Initialize(channelUsed, Bitrate.Pcan500) = PcanStatus.OK Then
If Runtime.InteropServices.RuntimeInformation.IsOSPlatform(Runtime.InteropServices.OSPlatform.Windows) Then
If Api.SetValue(channelUsed, PcanParameter.ReceiveEvent, CUInt(receiveEvent.SafeWaitHandle.DangerousGetHandle().ToInt32())) = PcanStatus.OK Then
Console.WriteLine($"A receive event was configured for {channelUsed}.")
Else
Console.WriteLine($"Error configuring a receive event on channel {channelUsed}.")
End If
Else
Dim eventHandle As UInteger
If Api.GetValue(channelUsed, PcanParameter.ReceiveEvent, eventHandle) = PcanStatus.OK Then
receiveEvent.SafeWaitHandle.Close()
receiveEvent.SafeWaitHandle = New Microsoft.Win32.SafeHandles.SafeWaitHandle(New IntPtr(eventHandle), False)
Console.WriteLine($"A receive event was configured for {channelUsed}.")
Else
Console.WriteLine($"Error retrieving the receive event on channel {channelUsed}.")
End If
End If
Api.Uninitialize(channelUsed)
End If
PcanChannel channelUsed = PcanChannel::Usb01;
System::Threading::EventWaitHandle^ receiveEvent = gcnew System::Threading::EventWaitHandle(false, System::Threading::EventResetMode::AutoReset);
if (Api::Initialize(channelUsed, Bitrate::Pcan500) == PcanStatus::OK)
{
if (System::Runtime::InteropServices::RuntimeInformation::IsOSPlatform(System::Runtime::InteropServices::OSPlatform::Windows))
{
if (Api::SetValue(channelUsed, PcanParameter::ReceiveEvent, (UInt32)receiveEvent->SafeWaitHandle->DangerousGetHandle().ToInt32()) == PcanStatus::OK)
{
Console::WriteLine("A receive event was configured for {0}.", channelUsed);
}
else
{
Console::WriteLine("Error configuring a receive event on channel {0}.", channelUsed);
}
}
else
{
UInt32 eventHandle;
if (Api::GetValue(channelUsed, PcanParameter::ReceiveEvent, eventHandle) == PcanStatus::OK)
{
receiveEvent->SafeWaitHandle->Close();
receiveEvent->SafeWaitHandle = gcnew Microsoft::Win32::SafeHandles::SafeWaitHandle(IntPtr::IntPtr((Int32)eventHandle), false);
Console::WriteLine("A receive event was configured for {0}.", channelUsed);
}
else
{
Console::WriteLine("Error retrieving the receive event on channel {0}.", channelUsed);
}
}
Api::Uninitialize(channelUsed);
}