Author: LZM csdn: http://blog.csdn.net/kaly_liu
Email: kaly.liu@163.com
The TCP under the UIP acts as the server and actively sends data=== Recently, I was tired of searching for information on the Internet, and I was confused about it. I can't stand it because I didn't have an instance. Now I want to share it with you.
1. Add the pin definition of stc12c5a Single Chip Microcomputer: In reg52.h: SFr P4 = 0xc0;
2. modify the corresponding PIN and modify it according to the circuit diagram. The SPI interface uses the single-chip microcomputer I/O port to simulate it. You only need to design the module based on the actual hardware circuit (the IO port must have a/drop-down resistor. modify the IO pin definition in the hfile. Note that the enc28j60 adopts the 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 sequence has been tuned, it can be used directly. At least sck, Cs, Si, and so must be connected. If int is not used, it cannot be connected. If it is connected, the enc28j60 driver code has enabled receiving interruption, it can be received in the program, but when the interrupt mode is used, consider the synchronization of the received and sent packets. For example, when the NIC supports full duplex mode and the packet is being sent, another packet is interrupted, while the uIP protocol stack shares the cache (to save memory). If you call the protocol stack again, an error occurs in the protocol stack cache.
3. Add the serial port interrupt. Modify the init_uart () function in mcu_uart.c to enable the serial port interrupt function.
4. Add interrupt voiduart (void) interrupt 4 in Main. C.
To save the data received by the serial port to the cache, you must first set an array temp [64] to temporarily store the data, receive the number of data bytes rxnum, and receive the completion mark sendtotcpflag;
The data received from the serial port is received in one byte and one byte. Therefore, to receive the complete data and then process it again, a mark is required, indicating that all data is received and processed. Here, I add '*' and '#' before and after the data as the start and end signs respectively. If these two symbols appear, sendtotcpflag will automatically add one; when the sendtotcpflag is determined to be 2 in the program, it indicates that the data is received, and the data processing function is entered to process the received data. There is a placeNote:I directly use: sbuf = '*' For judgment. No, I don't know why temp [rxnum] must be used as the intermediate quantity, you can use temp [rxnum] = '*' to make a decision.
5. send data to the tcp client through the serial port.
The single-chip microcomputer first accepts the serial port data, and then sends the data to the PC client through the TCP server. In UIP, there are two methods to actively send data:
1) uip_input: when new data is detected, call uip_input () to unpackage the received IP packet, extract the user data, and then call app_call, call uip_send () or uip_udp_send () in app_call to send data.
2) When uip_periodic () is used, that is, when the main cycle times out and new data is not received, uip_periodic () is called, app_call is called in uip_input, and uip_send () is called () or uip_udp_send () to send data.
In a program, the first method is used. Only when the server detects new data can the server send data to the client. In the example0_app () function of the example0.c file, add the program:
Add an IF (sendtotcpflag> 2) Judgment Execution program in if (uip_newdata () | uip_rexmit (). The Code is as follows:
The procedure is as follows:
First, complete the device connection, and connect the serial port and network cable;
Step 2: Enable the serial port and network debugging Assistant;
Step 3: Enable 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: Enter the data "* XXXXX #" in the serial port #";
Step 5: The network debugging Assistant Client sends random data to the server;
Step 6, we can see that the serial port data is sent from the server to the client.
6. Server IP and port settings and MAC Address Settings. IP and MAC addresses are set in uipopt. H files (static files are set), and ports are set in tcp_server.c.
In addition, you can use the function to set the IP address and MAC address and modify the uip_fixedaddr value in the uipopt. h file:
# When define uip_fixedaddr 0 // 1 is set to 1, the MCU is set to a static IP address. When uip_fixedaddr is set to 0, IP can be set using the function (recommended ). You can add the following functions in Main. C. Of course, you must first define an array in Mian (): u16_t ipaddr [2];
The final implementation is:First, data is sent through the serial port, but not directly sent to the tcp client, and only stored in the MCU. Then, the tcp client sends data to the MCU. After the MCU receives the data, the serial port data is sent to the tcp client.
20130426 improving:
7.Modify the content in step 5 to enable the tcp client to send the command 'C' to the TCP server. The TCP server receives the data and considers that the data needs to be sent from the serial port to the tcp client. After the tcp client receives 'C', it enters a latency of up to 30 s. The serial port needs to send a piece of data within the time limit. When the TCP server receives data (the start and end signs are '*' and '#'), the value of sendtotcpflag is 2, in this case, the program will break out of the loop delay and send 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 data. To ensure that the serial data is sent after the TCP server receives the 'C' command. The specific code is modified as follows:
To ensure that the serial port data is sent after the TCP server receives the 'C' command, you need to judge in Main. When the value of senddataflag is 0, it indicates that the serial port data is not required at this time. Therefore, do not store any serial port data in temp [], and clear temp. When the value of senddataflag is 10, it indicates the required serial data, that is, the serial data is sent after the TCP server receives the 'C' command from the tcp client. The related code is as follows:
This program has been uploaded to the resource and connected to my comments. Http://download.csdn.net/detail/kaly_liu/5312012
Author: LZM csdn: http://blog.csdn.net/kaly_liu
Email: kaly.liu@163.com