> | # BSD UDP Serial Sample ## Introduction This sample demonstrates how to write a UDP serial application using the Berkeley Software Distribution (BSD) standard socket APIs to communicate with a SG15. For the purpose of this demonstration, the sample application creates a networking socket it uses to send and receive serial data encapsulated in UDP datagrams from the SG15. The SG15 uses its UDP Client and Server features to communicate. ## SG15 Setup This sample was designed to operate with the SG15 operating in UDP mode. Configure the SG15 1. Use the SG15 configuration tool to access the settings of this specific SG15. 2. Choose the "Network" tab from the Configuration menu. 3. Configure the SG15 to communicatie through UDP by doing the following: a. Check "UDP" in the Connect Mode section. b. Enter the IP address of the PC running the sample in the "Remote IP" field. c. Specify 9000 as the UDP port number. d. Click "Apply now". ## How To Build This sample has been compiled and tested using GCC 3.2 on Red Hat Linux 8.0. It contains a Makefile that is used for building the application. To build this sample 1. Change directory to where the sample source files reside. 2. Type make. ## Step-by-Step 1. Create a local socket The call to create a socket looks like this:
>     int Socket = socket(AF_INET, Type, Protocol);
> Where `Type` is `SOCK_DGRAM` and `Protocol` is `IPPROTO_UDP` for a blocking, connectionless datagram socket. 2. Bind to a local port To bind the socket to a local port, call the following function:
>     bind(Socket, &SockAddr, sizeof(SockAddr));
> Binding to a local port must be done before using the socket to calls like `sendto`, and is used to associate the currently unnamed socket with a local name. The `SockAddr` parameter is the `struct sockaddr` representation of the local name to bind to. This name consists of an address family (for UDP, always `AF_INET`), a host address, and a port number. In this sample, the calls to `socket` and `bind` are both done in the function `MySocketOpen`. Since this sample demonstrates a UDP application, a well-known port (`9000`) is supplied to the call to `MySocketOpen`. The same well-known port is also used by the SG15 as part of its UDP Client destination address information. 3. Transmit data Transmitting data is done by calling `sendto`.
>     sendto(Socket, Data, Length, 0,
>            &SockAddr, sizeof(SockAddr));
> To simplify transmitting data this sample uses its own function named `MySocketSendTo`.
>     MySocketSendTo(MySocket, SendData, sizeof(SendData),
>                    AddressToSendTo, PortToSendTo);
> The parameters needed for `MySocketSend` are: `MySocket` which is the socket returned from `MySocketOpen`, `SendData` which is a pointer to the buffer holding the data to send, `sizeof(SendData)` which is the number of bytes `SendData` points to, and `AddressToSendTo` and `PortToSendTo` which provide the IP address and port number of the SG15 to send the data to. The default UDP port number of an SG15 is 9000 4. Receive data To retrieve data received at the local address, call `recvfrom`.
>     recvfrom(Socket, Data, Length, 0,
>              &SockAddr, &SockAddrLength);
> With the SG15 setup for UDP, a scanned barcode will be sent directly back to the sample application. Like `sendto`, the sample provides an alternative function, `MySocketRecvFrom` that is somewhat easier to use.
>     MySocketRecvFrom(MySocket, RecvData, sizeof(RecvData),
>                      AddressReceivedFrom,
>                      &AddressReceivedFromLength)
> The caller to `MySocketRecv` need only supply the prerequisite `MySocket`, a buffer `RecvData` to hold the received data, `sizeof(RecvData)` which specifies the length of `RecvData`, `AddressReceivedFrom` which provides space to return a string representation of the IP address and port number of the SG15 that sent the data, and `&AddressReceivedFromLength` which specifies the length of `AddressReceivedFrom`. 5. Close the socket Accomplish this task by calling the following two functions:
>     shutdown(Socket, SHUT_RDWR);
>     close(Socket);
When the sample application is done using the socket, the connection it represents must be terminated properly (`shutdown`) and any resources it may be using should be released (`close`). ## File List hexdumpdata.cpp hexdumpdata.h Makefile mysocket.cpp mysocket.h udpserial.cpp ## Keywords socket; sendto; recvfrom; shutdown; close