Documentation Center

  • Trial Software
  • Product Updates

Events and Callbacks

Event Types and Callback Properties

The event types and associated callback properties supported by TCP/IP and UDP objects are listed below.

TCP/IP and UDP Event Types and Callback Properties

Event Type

Associated Properties

Bytes available

BytesAvailableFcn

BytesAvailableFcnCount

BytesAvailableFcnMode

Datagram received

DatagramReceivedFcn (UDP objects only)

Error

ErrorFcn

Output empty

OutputEmptyFcn

Timer

TimerFcn

TimerPeriod

The datagram-received event is described below. For a description of the other event types, refer to Event Types and Callback Properties.

Datagram-Received Event

A datagram-received event is generated immediately after a complete datagram is received in the input buffer.

This event executes the callback function specified for the DatagramReceivedFcn property. It can be generated for both synchronous and asynchronous read operations.

Responding To Event Information

You can respond to event information in a callback function or in a record file. Event information stored in a callback function uses two fields: Type and Data. The Type field contains the event type, while the Data field contains event-specific information. As described in Creating and Executing Callback Functions, these two fields are associated with a structure that you define in the callback function header. Refer to Debugging: Recording Information to Disk to learn about storing event information in a record file.

The event types and the values for the Type and Data fields are given below.

TCP/IP and UDP Event Information

Event Type

Field

Field Value

Bytes available

Type

BytesAvailable

Data.AbsTime

day-month-year hour:minute:second

Datagram received

Type

DatagramReceived

Data.AbsTime

day-month-year hour:minute:second

Data.DatagramAddress

IP address string

Data.DatagramLength

Number of bytes received as double

Data.DatagramPort

Port number of sender as double

Error

Type

Error

Data.AbsTime

day-month-year hour:minute:second

Data.Message

An error string

Output empty

Type

OutputEmpty

Data.AbsTime

day-month-year hour:minute:second

Timer

Type

Timer

Data.AbsTime

day-month-year hour:minute:second

The Data field values are described below.

AbsTime Field

AbsTime is defined for all events, and indicates the absolute time the event occurred. The absolute time is returned using the MATLAB® Command window clock format.

day-month-year hour:minute:second

DatagramAddress Field

DatagramAddress is the IP address of the datagram sender.

DatagramLength Field

DatagramLength is the length of the datagram in bytes.

DatagramPort Field

DatagramPort is the sender's port number from which the datagram originated.

Message Field

Message is used by the error event to store the descriptive message that is generated when an error occurs.

Using Events and Callbacks

This example extends Communicating Between Two Hosts to include a datagram received callback. The callback function is instrcallback, which displays information to the command line indicating that a datagram has been received.

The following command configures the callback for the UDP object u2.

u2.DatagramReceivedFcn = @instrcallback;

When a datagram is received, the following message is displayed.

DatagramReceived event occurred at 10:26:20 for the object: 
UDP-doetom.dhpc.
25 bytes were received from address 192.168.1.12, port 8844.
Was this topic helpful?