2017-03-28 4 views
1

Kern verwenden: Cortex-M4Wie kann ich lwIP TCP/IP-Stack mit Mikrocontroller STM32F4 (Client)

Mikrocontroller: STM32F407 (STM32F4 Entdeckung board)

IP Stack: lwIP 1.4.1

ich bin mit diesem Mikrocontroller eine Automate zu steuern, und ich möchte über eine HTTP-Anforderung in Form von einem paar Informationen zu einem separaten Web-Server senden:

http://192.168.1.3/api/xdevices.json?SetR=01

lwIP hat einen HTTP-Server für den Mikroprozessor, aber ich bin nach dem Gegenteil (Mikrocontroller ist der Client).

Ich bin nicht sicher, was ich falsch mache, aber nach TCP_Connect es geht immer Handler tcp_error:

#include "stm32f4xx.h" 
    /* Include my libraries here */ 
    #include "defines.h" 
    #include "tm_stm32f4_delay.h" 
    #include "tm_stm32f4_disco.h" 
    #include "tm_stm32f4_usart.h" 
    #include "tm_stm32f4_ethernet.h" 
    #include "tm_stm32f4_watchdog.h" 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <lwip/tcp.h> 
    #include <tcp.h> 


    uint32_t tcp_send_packet(void); 
    void tcpErrorHandler(void *arg, err_t err); 
    err_t tcpSendCallback(void *arg, struct tcp_pcb *tpcb,u16_t len); 

    err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err); 

    struct tcp_pcb *testpcb; 

erste Funktion ist tcp_new:

void tcp_setup(void) 
    { 
    uint32_t data = 0xdeadbeef; 
    /* create an ip */ 
    struct ip_addr ip; 
    IP4_ADDR(&ip,192,168,1,4); //IP of my PHP server 
    /* create the control block */ 
    testpcb = tcp_new(); //testpcb is a global struct tcp_pcb 
         // as defined by lwIP 
    /* dummy data to pass to callbacks*/ 
    tcp_arg(testpcb, &data); 
    /* register callbacks with the pcb */ 
    // tcp_recv(testpcb, tcpRecvCallback); 
    tcp_sent(testpcb, tcpSendCallback); 
    tcp_err(testpcb, tcpErrorHandler); 
    /* now connect */ 
tcp_connect(testpcb, &ip, 21, connectCallback); 
    TM_DISCO_LedOn(LED_ORANGE); 
    } 

Mein Rückrufe:

void tcpErrorHandler(void *arg, err_t err){ 
    TM_DISCO_LedOn(LED_BLUE); 
    } 


    err_t tcpSendCallback(void *arg, struct tcp_pcb *tpcb,u16_t len) 
    { 
    TM_DISCO_LedOn(LED_RED);        
    } 

    err_t connectCallback(void *arg, struct tcp_pcb *tpcb, err_t err) 
    { 
    TM_DISCO_LedOn(LED_RED); 

    tcp_send_packet(); 
    return 0; 
    } 

meine Kopfzeile:

uint32_t tcp_send_packet(void) 
    { 
    char *string = "SetR=01\r\n\r\n "; 
    uint32_t len = strlen(string); 

    /* push to buffer */ 
    tcp_write(testpcb, string, strlen(string), TCP_WRITE_FLAG_COPY); 
    /* now send */ 
    tcp_output(testpcb); 

    return 0; 
    } 

    void lwip_init(); 

Hauptfunktion:

int main(void) { 
    /* Initialize system */ 
    SystemInit(); 
    lwip_init(); 
    /* Initialize delay */ 
    TM_DELAY_Init(); 
    /* Initialize leds on board */ 
    TM_DISCO_LedInit(); 

    /* Initialize button */ 
    TM_DISCO_ButtonInit(); 


    while(1) { 

    tcp_setup(); 
    } } 

Klar ich etwas vergessen, ich weiß nur nicht, was es ist (ich verwende Keil Arm)

danke

Antwort

1
    .
  • Init LwIP mit lwip_init
  • Rufen Sie tcp_setup außerhalb while loop nur einmal nicht in While-Schleife, um nur eine einzurichten TCP-Verbindung, nicht unbegrenzt
  • In while loop, LwIP eingehende Daten regelmäßig verarbeiten. Da Sie meine (TM Tilen Majerle) Ethernet-Wrapper für STM32F4 verwenden, sollten Sie diese Zeilen in Ihrem while-Schleife

    /* Check if any packet received */ 
    if (LwIP_CheckFrameReceived()) { 
        /* Process received ethernet packet */ 
        LwIP_Pkt_Handle(); 
    } 
    
    /* Handle periodic timers for LwIP */ 
    LwIP_Periodic_Handle(EthernetLocalTime); 
    

hinzufügen oder TM_ETHERNET_Update Funktion aus Ihrer Bibliothek aufrufen. Ihre Wahl, es funktioniert genauso.

In Ihrem Fall:

int main() { 
    .... 
    lwip_init(); 
    tcp_setup(); 
    while (1) { 
     TM_ETHERNET_Update(); 
    } 
} 
+0

Thanks a lot es funktionierte perfekt es zu schätzen wissen (y) –