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:
Previous article:IIC AT24C256 reading and writing program
Next article:UART serial port protocol basics 1
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- I found a very good introductory book on 51 single-chip microcomputers and recommend it to beginners. It is suitable for self-study by novices (Wang Yun)
- Application of Bypass Capacitors in High-Speed Circuit Environment
- Design of software and hardware C delay program for MSP430
- Optical module SFF-8472 problem solving
- Develop smart devices based on ESP32 to access the smart home ecosystem HomeAssistant
- Domestic MCU
- STM32F767 ADC self-calibration problem
- EEWORLD University ---- Digi-Key: Accelerated Application of Graph Algorithms in Deep Learning
- Echo Cancellation - How to solve the serious problem of sound swallowing during dual talk in WebRTC AEC algorithm
- [TI millimeter wave radar evaluation]_2_1243Problems encountered in BOOST debugging