Ver código fonte

printf use udp

haitao 10 meses atrás
pai
commit
aeeb89eebb
3 arquivos alterados com 46 adições e 57 exclusões
  1. BIN
      app/MDK/Objects/dtu_simple.axf
  2. 1 9
      app/USR/source/main.c
  3. 45 48
      app/USR/source/udp_echo.c

BIN
app/MDK/Objects/dtu_simple.axf


+ 1 - 9
app/USR/source/main.c

@@ -79,7 +79,6 @@ int main(void)
 		gd_485_DE_pin_init();
 		config_485_port(COM_485,115200,8,1,0);
 		gd_485_DE_tx();
-		printf("hello");
     /* init task */
     xTaskCreate(init_task, "INIT",1024, NULL, INIT_TASK_PRIO, NULL);
     /* start scheduler */
@@ -109,7 +108,7 @@ void init_task(void * pvParameters)
     /* initilaize the tcp client: echo 1026 */
 //    tcp_client_init();
     /* initilaize the udp: echo 1025 */
- //   udp_echo_init();
+   udp_echo_init();
 
 #ifdef USE_DHCP
     /* start DHCP client */
@@ -132,7 +131,6 @@ void init_task(void * pvParameters)
 
 void led_task(void * pvParameters)
 {  
-	
     for( ;; ){
         /* toggle LED3 each 250ms */
         gd_eval_led_toggle(LED_DEV);
@@ -141,12 +139,6 @@ void led_task(void * pvParameters)
 }
 
 
-int fputc(int ch, FILE *f)
-{
-    usart_data_transmit(COM_485, (uint8_t) ch);
-    while (RESET == usart_flag_get(COM_485, USART_FLAG_TBE));
-    return ch;
-}
 
 void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName )
 {

+ 45 - 48
app/USR/source/udp_echo.c

@@ -47,6 +47,7 @@ OF SUCH DAMAGE.
 #include "main.h"
 #include "lwip/api.h"
 
+
 #define UDP_TASK_PRIO       ( tskIDLE_PRIORITY + 5)
 #define MAX_BUF_SIZE        50
 
@@ -158,56 +159,40 @@ static void udp_task(void *arg)
     \param[out] none
     \retval     none
 */
-static void udp_task(void *arg)
+volatile int8_t sockfd=-1;
+#define UDP_LOCAL_PORT 12345
+#define UDP_REMOTE_PORT 54321
+#define SERCER_IP_ADDRESS "192.168.2.22"       //接收服务器ip
+struct sockaddr_in remote_addr;
+static int udp()
 {
-    int  ret, recvnum, sockfd = -1;
-    int udp_port = 1025;
-    struct sockaddr_in rmt_addr, bod_addr;        
-    char buf[100];
-    u32_t len;
-    struct ip_addr ipaddr;
+	int ret;
+	struct sockaddr_in local_addr;
+	    // 创建UDP套接字
+  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+  if (sockfd < -1) {
+    perror("Error creating socket");
+		goto exit;
+  }
+	local_addr.sin_family=AF_INET;
+	local_addr.sin_port=htons(UDP_LOCAL_PORT);
+	local_addr.sin_addr.s_addr=INADDR_ANY;
+	ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
+  if(ret < 0)
+	{ 
+    lwip_close(sockfd);
+    sockfd = -1;
+    goto exit;
+  }
+	//设置远程服务器地址 TODO:服务器地址配置后自动获取
+	remote_addr.sin_family=AF_INET;
+	remote_addr.sin_port=htons(UDP_REMOTE_PORT);
+	inet_aton(SERCER_IP_ADDRESS,&(remote_addr.sin_addr));
+exit:
+			return -1;
+}
 
-    IP4_ADDR(&ipaddr, IP_S_ADDR0, IP_S_ADDR1, IP_S_ADDR2, IP_S_ADDR3);
 
-    /* set up address to connect to */
-    rmt_addr.sin_family = AF_INET;
-    rmt_addr.sin_port = htons(udp_port);
-    rmt_addr.sin_addr.s_addr = ipaddr.addr;
-  
-    bod_addr.sin_family = AF_INET;
-    bod_addr.sin_port = htons(udp_port);
-    bod_addr.sin_addr.s_addr = htons(INADDR_ANY);
-    
-    while (1){
-        /* create the socket */
-        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
-        if(sockfd < 0){
-            continue;
-        }
-        
-        ret = bind(sockfd, (struct sockaddr *)&bod_addr, sizeof(bod_addr));
-        
-        if(ret < 0){ 
-            lwip_close(sockfd);
-            sockfd = -1;
-            continue;
-        }  
-        
-        len = sizeof(rmt_addr);
-        /* reveive packets from rmt_addr, and limit a reception to MAX_BUF_SIZE bytes */
-        recvnum = recvfrom(sockfd, buf, MAX_BUF_SIZE, 0, (struct sockaddr *)&rmt_addr, &len);
-        
-        while(-1 != sockfd){
-            if(recvnum > 0){
-                /* send packets to rmt_addr */
-                sendto(sockfd, buf, recvnum, 0, (struct sockaddr *)&rmt_addr, sizeof(rmt_addr));
-            }
-            recvnum = recvfrom(sockfd, buf, MAX_BUF_SIZE, 0, (struct sockaddr *)&rmt_addr, &len);
-        }
-
-        lwip_close(sockfd);
-    } 
-}
 
 #endif /* LWIP_SOCKET */
 
@@ -220,5 +205,17 @@ static void udp_task(void *arg)
 */
 void udp_echo_init(void)
 {
-    xTaskCreate(udp_task, "UDP", DEFAULT_THREAD_STACKSIZE, NULL, UDP_TASK_PRIO, NULL);   
+    //xTaskCreate(udp, "UDP", DEFAULT_THREAD_STACKSIZE, NULL, UDP_TASK_PRIO, NULL);
+		udp();	
+}
+
+
+
+int fputc(int ch, FILE *f)
+{
+	if(sockfd>=0)
+	{
+		sendto(sockfd, &ch, 1, 0, (struct sockaddr *)&remote_addr,sizeof(remote_addr));
+    return ch;
+	}
 }