Extended Socket example: UDP

UDP is connection-less, so this example demonstrates that a Socket Connect frame is not required to begin communication and that multiple peers can be used with a single socket.

Send a Socket Create frame

Field Value
Frame type

0x40 (Socket Create)

Frame ID 0x01
Protocol 0x00 (UDP)

UDP Socket Create frame data:

7E 00 03 40 01 00 BE

Receive a Socket Create response

Field Value
Frame type 0xC0 (Socket Create Response)
Frame ID 0x01
Socket ID 0x00
Status 0x00 (Success)

Socket Create Response received from XBee:

7E 00 04 C0 01 00 00 3E

Bind local source addres

The bind/listen operation is necessary prior to transmit in order to assign a known source address to all data sent from this socket.

Field Value
Frame type 0x46 (Socket Bind/Listen)
Frame ID 0x01
Socket ID 0x00
Source Port 0x12 0x34

Socket Bind/Listen frame data:

7E 00 05 46 01 00 12 34 72

Receive Bind/Listen Response

The XBee generates a response indicating the status of the request to bind the requested port.

Field Value
Frame type

0xC6 (Socket Bind/Listen Response)

Frame ID 0x01
Socket ID 0x00
Status 0x00 (Success)

Socket Bind/Listen Response received from XBee:

7E 00 04 C6 01 00 00 38

Send to Digi echo server

Digi hosts a server at 52.43.121.77 port 10001 which echos all UDP traffic sent to it.

Field Value
Frame type

0x45 (Socket SendTo)

Frame ID 0x01
Socket ID 0x00
Destination Address 0x34 0x2B 0x79 0x4D (52.43.121.77)
Destination Port 0x27 0x11 (decimal 10001)
Transmit Options 0x00
Payload echo this

Socket SendTo frame data:

7E 00 13 45 01 00 34 2B 79 4D 27 11 00 65 63 68 6F 20 74 68 69 73 E5

Receive TX Status

Extended sockets use the existing TX Status frame (0x89) to report acceptance of the data for transmit.

Field Value
Frame type

0x89 (TX Status)

Frame ID 0x01
Status 0x00 (Success)

TX Status received from XBee:

7E 00 03 89 01 00 75

Receive echoed data

When the response from the server is sent back, the XBee provides it using a Socket Receive From frame.

Field Value
Frame type

0xCE (Socket Receive From)

Frame ID 0x00
Socket ID 0x00
Source address 0x34 0x2B 0x79 0x4D (52.43.121.77)
Source Port 0x27 0x11 (decimal 10001)
Status 0x00 (Success)
Payload echo this

Socket ReceiveFrom received from XBee, containing echoed data:

7E 00 13 CE 00 00 34 2B 79 4D 27 11 00 65 63 68 6F 20 74 68 69 73 5D

Send to Digi time server

Digi hosts a server at 54.43.121.77 port 10002 which will reply with the time when it receives a packet.

Field Value
Frame type

0x45 (Socket SendTo)

Frame ID 0x01
Socket ID 0x00
Destination Address 0x34 0x2B 0x79 0x4D (52.43.121.77)
Destination Port

0x27 0x12 (decimal 10002)

Transmit Options 0x00
Payload 0x20 (ASCII space, any value should do)

Socket SendTo time server frame data:

7E 00 0B 45 01 00 34 2B 79 4D 27 12 00 20 3B

Receive TX Status

This is exactly the same as the previous transmission to the echo server on success.

Receive daytime value

When the response from the server is sent back, the XBee will provide it using a Socket Receive From frame.

Field Value
Frame type

0xCE (Socket Receive From)

Frame ID 0x00
Socket ID 0x00
Source address 0x34 0x2B 0x79 0x4D (52.43.121.77)
Source Port

0x27 0x12 (decimal 10002)

Status 0x00 (Success)
Payload <current UTC time>

Socket Receive From frame received from XBee containing time data:

7E 00 1E CE 00 00 34 2B 79 4D 27 12 00 32 30 31 39 2D 30 37 2D 31 38 20 31 38 3A 35 32 3A 34 33 0A 08

Close the socket

When the socket is no longer needed it should be closed to return resources to the system.

Field Value
Frame type

0x43 (Socket Close)

Frame ID 0x01
Status 0x00

Socket Close frame data:

7E 00 03 43 01 00 BB

Receive close response

Finally, the XBee indicates the socket has been closed with a Socket Close Response frame.

Field Value
Frame type

0xC3 (Socket CloseResponse)

Frame ID 0x01
Socket ID 0x00
Status 0x00 (Success)

Socket Close Response received from XBee:

7E 00 04 C3 01 00 00 3B