WorkerGetBroadcast Method

Retrieves a Broadcast object from the periodic CAN messages list.

Definition

Namespace: Peak.Can.Basic
Assembly: PCANBasic.NET (in PCANBasic.NET.dll) Version: 4.9.0
C#
public Broadcast GetBroadcast(
	int index
)

Parameters

index  Int32
Represents the zero-based index of the periodic CAN message to be retrieved.

Return Value

Broadcast
A NullableT value with a valid periodic message, otherwise null

Remarks

Changes made to an object retrieved by this method are automatically updated in the Worker object's broadcast list.

Example

The following example shows how to retrieve a Broadcast object from the Worker object's broadcast list.

In case of failure, an error messages is written to the console output using English as output language.

C#
// Create the object using the default configuration
//
Worker myWorker = new Worker();

// Create the CAN message to broadcast
//
PcanMessage message = new PcanMessage(0x100, MessageType.Standard, 3, new byte[] { 1, 2, 3 }, false);

// Add the new broadcast using a CAN message and its interval
//
int index = myWorker.AddBroadcast(message, 100);
if (index < 0)
{
    Console.WriteLine("The broadcast couldn't be configured.");
}
else
{
    Console.WriteLine($"The broadcast object with index '{index}' was configured successfully.");

    try 
    { 
        // Activate the worker object
        //
        myWorker.Start();
        Console.WriteLine("The Worker object was activated successfully.");

        // Wait some time for the message to be sent several times
        //
        Console.WriteLine("Broadcast is working....");
        System.Threading.Thread.Sleep(5000);

        // Get the broadcast object to modify it directly
        //
        Broadcast broadcast = myWorker.GetBroadcast(index);

        if (broadcast != null)
        {
            // Update the CAN message being sent and its interval
            //
            broadcast.Message.ID = 0x200;
            broadcast.Message.DLC = 8;
            broadcast.Message.Data = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
            broadcast.Interval = 1000;

            // Wait some time for the modified message to be sent several times
            //                        
            Console.WriteLine("The broadcast object was modified.");
            System.Threading.Thread.Sleep(5000);
        }
        else
        {
            Console.WriteLine($"The broadcast object with index '{index}' could not be retrieved.");
        }         

        // Deactivate the worker object, when it is no longer needed
        //
        myWorker.Stop();
        Console.WriteLine("The Worker object was deactivated.");
    }
    catch(Exception ex)
    {
        Console.WriteLine($"An exception occurred by activating the Worker object. {ex.Message}");
    }
}

Exceptions

IndexOutOfRangeExceptionThe index parameter value is out of range. This must be in the range [0...MaximumBroadcastCount-1].

See Also