Abstract: This article introduces a case study in which a touch screen is first drawn to display on the LCD screen, and data is transmitted through the network to display on the computer screen. The computer controls the clearing of graphics on the LCD screen, achieving communication between ARM and X86 platforms based on UDP protocol. Therefore, a solution for communication between ARM and other platforms based on UDP protocol is proposed. Introduction: With the application of artificial intelligence, ARM products have spread to various fields: industrial control, wireless communication, consumer electronics, imaging and security products, including popular digital cameras and printers
This article introduces a case study in which a touch screen is first drawn to display on the LCD screen, and data is transmitted through the network to display on the computer screen. The computer controls the clearing of graphics on the LCD screen, achieving communication between ARM and X86 platforms based on UDP protocol. Therefore, a solution for communication between ARM and other platforms based on UDP protocol is proposed.
Introduction
With the application of artificial intelligence, ARM products have spread to various fields: industrial control, wireless communication, consumer electronics, imaging and security products, including the majority of popular digital cameras and printers that use ARM technology. 32-bit SIM smart cards in mobile phones also use ARM technology. In addition, ARM microprocessors and technologies are also applied in many different fields and will have a wider range of applications in the future. Therefore, communication between ARM and other platforms is particularly important.
1. Essence of UDP Protocol
UDP protocol is the abbreviation for User Datagram Protocol, which is mainly used to support network applications that need to transfer data between computers. Many client/server mode network applications, including online video conferencing systems, require the use of UDP protocol. The UDP protocol has been in use for many years since its inception. Although its initial glory has been overshadowed by some similar protocols, even today, UDP is still a very practical and feasible network transport layer protocol.
The UDP protocol uses port numbers to reserve their respective data transmission channels for different applications. UDP and TCP protocols use this mechanism to support multiple applications sending and receiving data simultaneously at the same time. The data sender (which can be the client or server) sends UDP datagrams through the source port, while the data receiver receives data through the target port. Some network applications can only use static ports that are pre reserved or registered for them; Other network applications can use unregistered dynamic ports. Because the UDP header uses two bytes to store the port number, the effective range of the port number is from 0 to 65535. Generally, port numbers greater than 49151 represent dynamic ports.
The length of a datagram refers to the total number of bytes, including the header and data portion. Because the length of the header is fixed, this field is mainly used to calculate the variable length data portion (also known as the data payload). The maximum length of a datagram varies depending on the working environment. In theory, the maximum length of a datagram, including the header, is 65535 bytes. However, some practical applications often limit the size of the datagram, sometimes reducing it to 8192 bytes.
The UDP protocol uses checksums in the header to ensure data security. The verification value is first calculated by a special algorithm on the data sender, and after being transmitted to the receiver, it needs to be recalculated.
If a datagram is tampered with by a third party during transmission or damaged due to line noise or other reasons, the verification calculation values of the sender and receiver will not match, so the UDP protocol can detect whether there is an error. In fact, the verification function is optional in the UDP protocol, and turning it off can improve the performance of the system. This is different from the TCP protocol, which requires a checksum.
2. Implementation cases
The implementation example is as follows: Draw a picture on the touch screen to display it on the LCD screen, transfer data through the network to display it on the computer screen, and control the computer to clear the graphics on the LCD screen.
The steps are as follows:
(1) New construction project
(2) Edit the initialization network function void InitNetWork() in the main. c file//Initialize the network{
U32 ipaddr32, ipmaskaddr32, ipgateaddr32;
U8 * Mac;
Ipaddr32=Get_ipaddr()// Obtain IP address
Ipmaskaddr32=Get_maskaddr()// Get subnet mask
Ipgateaddr32=Get_gwaddr()// Get Gateway
Mac=Get_mac()// Obtain the network card address NetPortChoose (0);
//Choosing a network port must be done before configuring the network
InitOSNet (ipaddr32, ipaskaddr32,
Ipgateaddr32, Mac)// configure network
OSTimeDly (1000)// Task suspended for 1 second
Printk (“init Ethernet and UDP is
Okay! \N);
}
(3) Define a computer socket, global variable (4), and write Main-Task tasks and message loops mainly responsible for responding to touch screen messages, drawing on the screen, and then transferring data to the computer.
The processing of touch screen messages is similar to keyboard messages, with the message type pMsg ->Message being OSM-TOUCH-SCREEN, and the message parameter pMsg ->LParam containing the action information of the touch screen, defined as follows:
#Define TCHSCR-ACTION-NULL
0
#Define TCHSCR-ACTION-CLICK 1//Touch
Screen click
#Define TCHSCR-ACTION-DBCLICK 2//Touch
Touch screen double-click
#Define TCHSCR-ACTION-DOWN 3//Touch
Screen pressed
#Define TCHSCR-ACTION-UP 4//Touch
Screen lifting
#Define TCHSCR-ACTION-MOVE 5//Touch
Screen movement
The message parameter pMsg ->WParam includes touch
The coordinate information of a point, with the low 16 bits representing the X coordinate value and the high 16 bits representing the Y coordinate value. Here, when the touch screen generates a “press” action, the MoveTo() function is used to set the starting point coordinates for drawing. When the “move” action is generated, the LineTo() function is used to draw line segments.
3. Solution
3.1 Establishing a Socket
To establish S o c k e t, the program can call the socket function, which returns a handle similar to a file descriptor. The prototype of the socket function is:
Int socket (int domain, int type, intprotocol);
Domain indicates the protocol family used, usually PF_INET, representing the Internet protocol family (TCP/IP protocol family); The type parameter specifies the type of socket: SOCK_STREAM or SOCK-DGRAM. The Socket interface also defines the original Socket (SOCK-RAW), allowing the program to use low-level protocols; Protocol is usually assigned a value of “0” The Socket() call returns an integer socket descriptor that you can use in subsequent calls.
A Socket descriptor is a pointer to an internal data structure that points to the descriptor table entry. When calling the Socket function, the socket execution body will create a Socket, which actually means allocating storage space for a Socket data structure. The Socket execution body manages the descriptor table for you.
A network connection between two network programs includes five types of information: communication protocol, local protocol address, local host port, remote host address, and remote protocol port. The Socket data structure contains these five types of information.
3.2 Configure Socket
After returning a socket descriptor through a socket call, it is necessary to configure the socket before using it for network transmission. Connection oriented socket clients save local and remote information in the socket data structure by calling the Connect function. The client and server without connected sockets, as well as the server facing connected sockets, configure local information by calling the bind function.
The Bind function associates the socket with a port on the local machine, and then you can listen for service requests on that port.
The prototype of the Bind function is:
Int bind (int sockfd, struct sockaddr * my-addr, int addrlen);
Sockfd is the socket descriptor returned by calling the socket function, and my-addr is a pointer to a sockaddr type that contains information such as the local IP address and port number; Addrlen is often set to 3.3 to establish a connection
The connection oriented client program uses the Connect function to configure the socket and establish a TCP connection with the remote server. The prototype of the function is:
Int connect (int sockfd, struct sockaddr * serv addr, int addrlen);
Sockfd is the socket descriptor returned by the socket function; Serv_carr is a pointer that contains the remote host IP address and port number; Addrlen is the length of the remote address structure.
The Connect function returns -1 when an error occurs and sets errno to the corresponding error code. Client program design does not require calling bind(), as in this case, only the IP address of the destination machine needs to be known, and the client does not need to worry about which port to connect to the server through. The socket executor automatically selects an unoccupied port for your program and notifies you when the program data will arrive on the port.
The Connect function initiates a direct connection to the remote host. Only connection oriented client programs that use sockets need to connect this socket to the remote host. The connectionless protocol never establishes a direct connection. The connection oriented server never initiates a connection, it only passively listens to client requests on the protocol port.
The Listen function puts the socket in passive listening mode and creates an input data queue for the socket, saving incoming service requests in this queue until the program processes them.
Int listen (int sockfd, int backlog);
Firstly, when the accept function monitors a socket that receives a connection request, the PC104-DPIO DRL-DPM-BKF executor will create a new socket, which will be linked to the address of the requesting connection process. The initial socket that receives a service request can still continue to listen on the previous socket and perform data transfer operations on the new socket descriptor.
3.4 Data transmission
The Send() and recv() functions are used for data transfer on connection oriented sockets.
Sockfd is the socket descriptor you use to transfer data; Msg is a pointer to the data to be sent; Len is the length of data in bytes; Flags are generally set to 0 (for the usage of this parameter, please refer to the man manual).
The Send() function returns the actual number of bytes sent, which may be less than the data you want to send. In the program, the return value of send() should be compared with the number of bytes to be sent. When the return value of send() does not match len, this situation should be addressed.
3.5 End of transmission
After all data operations are completed, you can call the close() function to release the socket and stop any data operations on that socket.