UIP of MCU--TCP acts as a server and actively sends data ACK

Publisher:asd123yuiLatest update time:2019-05-21 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

TCP under uip acts as a server and actively sends data === I have been working on this recently. It is so tiring to find information on the Internet, and the descriptions are all vague and there are no examples. I really can't stand it. I finally figured it out and now I'm going to share it.

1. Add the pin definition of STC12C5A microcontroller: in reg52.h: sfr P4=0xC0;

2. Modify the corresponding pins according to the circuit diagram. The SPI interface is simulated by the IO port of the microcontroller. You only need to modify the IO pin definition in the spi.h file according to the actual hardware circuit design (the IO port requires pull-up/pull-down resistors). It should be noted that ENC28J60 uses SPI0 mode, that is, the rising edge of the clock signal receives data and the falling edge sends data. In this example, the SPI communication timing has been adjusted and can be used directly. At least SCK, CS, SI, and SO must be connected. INT is not used and can be left unconnected. If connected, the ENC28J60 driver code has enabled the receive interrupt, which can be received in the program. However, when using the interrupt mode, please consider the synchronization of receiving and sending packets. For example, the network card supports full-duplex mode. When a packet is being sent, another packet is received to generate an interrupt, and the uIP protocol stack is cached in common (to save memory). If the protocol stack is called again, the protocol stack cache will be wrong.


3. Add serial port interrupt, modify init_uart() function in MCU_UART.C, and enable serial port interrupt

4. Add interrupt voiduart(void) interrupt 4 in main.c

In order to save the data received by the serial port in the cache first, you need to set an array temp[64] to temporarily store the data, the number of received data bytes rxnum, and the receiving completion flag sendtotcpflag;

Data received from the serial port is received byte by byte, so to process after receiving the complete data, a flag is needed to indicate that all data has been received, and then process it. Here I use '*' and '#' before and after the data as the start and end flags respectively. When these two symbols appear, sendtotcpflag will automatically increase by 1; when sendtotcpflag is judged to be 2 in the program, it means that the data has been received, and then enter the data processing function to process the received data. There is one thing to note: I directly use: SBUF=='*' to make a judgment, which does not work. I don't know why temp[rxnum] must be used as an intermediate quantity, and temp[rxnum]=='*' can be used as a judgment.

 

5. Realize the serial port to send data to the TCP client.

The microcontroller first receives the serial port data, and then sends the data to the PC client through the TCP server. In uIP, there are two ways to actively send data:

1) Use uip_input: When new data is detected, call uip_input() to unpack the received IP packet, extract the user data, and then call app_call, in which uip_send() or uip_udp_send() is called to send the data.

2) Use uip_periodic(), that is, when the main loop times out and no new data is received, call uip_periodic(), call app_call in it like uip_input, and then call uip_send() or uip_udp_send() to send data.

In the program, the first method is used. The server can send data to the client only when it detects new data. In the example0_app() function of the example0.c file, add the following program:

Add a judgment execution program of if (sendtotcpflag>2) in if (uip_newdata()|| uip_rexmit()), the code is as follows:

The specific operation process is as follows:

First, connect the device perfectly, serial port, and network cable;

The second step is to open the serial port and network debugging assistant;

Step 3. Turn on the device and connect the network debugging assistant to the server; set the port number and IP number of the TCP client: Port: 8000 IP: 192.168.1.13

Step 4: Input the data to be sent in the serial port “*xxxxx#”;

Step 5: The network debugging assistant client sends random data to the server;

In the sixth step, you can see the serial port data just sent from the server to the client.

 

6. The server's IP, port, and MAC address settings. The IP and MAC address settings are in the uipopt.h file (the settings are static); the port is in tcp_server.c.

In addition, you can use the function to set the IP and MAC addresses and modify the value of UIP_FIXEDADDR in the uipopt.h file:

#define UIP_FIXEDADDR 0 //1 When 1, MCU is set to static IP. When UIP_FIXEDADDR is 0, you can use the function to set IP (recommended). You can add the following function in main.c. Of course, you must first define an array in main(): u16_t ipaddr[2];

 

What is finally achieved is: first, the serial port sends data, but it will not be sent directly to the TCP client, but will only be saved in the microcontroller; then the TCP client sends data to the microcontroller, and when the microcontroller receives the data, it will send the serial port data to the TCP client.

 

20130426 is being improved:

7. Modify the content in 5 to implement the function. The TCP client sends the command 'C' to the TCP server. The TCP server receives this data and thinks that it is necessary to send data from the serial port to the TCP client. After the TCP client receives 'C', it enters a delay with an upper limit of 30S, and the serial port needs to send a piece of data within the time limit. When the TCP server receives the data (the data uses '*' and '#' as the start and end marks), the serial port data receiving flag sendtotcpflag value is 2. At this time, the condition for jumping out of the delay is met, and the program breaks out of the loop delay and sends the data received from the serial port to the TCP client. The senddataflag in the code is used to mark the source of the serial port data, in order to ensure that the serial port data is sent after the TCP server receives the 'C' command. The specific code modifications are as follows:

In order to ensure that the serial port data is sent after the TCP server receives the 'C' command, it is necessary to judge in main that when the value of senddataflag is 0, it means that the serial port data at this time is not needed, so any serial port data should not be saved in temp[] at this time, and temp[] needs to be cleared. When the value of senddataflag is 10, it means that the serial port data is needed at this time, that is, the serial port data is sent after the TCP server receives the 'C' command sent by the TCP client. The relevant code is as follows:


Keywords:MCU Reference address:UIP of MCU--TCP acts as a server and actively sends data ACK

Previous article:IIC AT24C256 reading and writing program
Next article:UART serial port protocol basics 1

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号