我有一个PIC32 ethernet evaluation board并试图让它发送一个简单的UDP包。
这是我目前的代码:

/*
 * This macro uniquely defines this file as the main entry point.
 * There should only be one such definition in the entire project,
 * and this file must define the AppConfig variable as described below.
 */
#define THIS_IS_STACK_APPLICATION

// Include all headers for any enabled TCPIP Stack functions
#include "TCPIP Stack/TCPIP.h"
#include "TCPIPConfig.h"

#include <plib.h>
#include <p32xxxx.h>

// Declare AppConfig structure and some other supporting stack variables
APP_CONFIG AppConfig;
BYTE AN0String[8];

int main()
{
    static enum
    {
        SM_OPEN,
        SM_BROADCAST,
    } smState = SM_OPEN;
    typedef BYTE UDPSocket;
    UDP_SOCKET s;
    NODE_INFO myRemoteNode;

    myRemoteNode.IPAddr.v[0]=169;
    myRemoteNode.IPAddr.v[1]=254;
    myRemoteNode.IPAddr.v[2]=2;
    myRemoteNode.IPAddr.v[3]=2;
    myRemoteNode.MACAddr.v[0]=0x01;
    myRemoteNode.MACAddr.v[1]=0xA0;
    myRemoteNode.MACAddr.v[2]=0xF0;
    myRemoteNode.MACAddr.v[3]=0x7B;
    myRemoteNode.MACAddr.v[4]=0xE5;
    myRemoteNode.MACAddr.v[5]=0x45;

    TRISD = 0xff00; // tri-state register for the LED ports

    // Initialize UDP
    UDPInit();

    while(1)
    {
        switch(smState)
        {
            case SM_OPEN:

                // Talk to a remote DHCP server.
                s = UDPOpen(68, &myRemoteNode, 67);

                if ( s == INVALID_UDP_SOCKET )
                {
                    PORTD = 0x01; // Switch on the red LED on PIC32 ethernet starter kit to indicate an error
                    // Socket is not available
                    // Return error.
                }
                else
                    // Broadcast DHCP Broadcast message.
                    smState = SM_BROADCAST;
                break;

            case SM_BROADCAST:

                PORTD = 0x02; // Switch on the yellow LED to indicate the connection

                if ( UDPIsPutReady(s) )
                {
                    // Turn on yellow and green LED to indicate UDP is put ready
                    PORTD = 0x06;

                    // Socket is ready to transmit. Transmit the data...
                    // Note that there is DHCPSocket parameter in UDPPut.
                    // This UDPPut call will use active socket
                    // as set by UDPIsPutReady() - that is DHCPSocket.
                    UDPPut(0x55);

                    // Now transmit it.
                    UDPFlush();
                }
                UDPClose(s);
                break;
        }
    }
}

当我在MPLAB-IDE中编译和运行代码时,看起来电路板发送了两个包,然后什么也没有发送。我把电路板直接连接到运行Wireshark的笔记本电脑上,但Wireshark无法检测到任何接收到的数据包。
我成功地让董事会运行http服务器演示,它使用TCP,所以我知道这不是硬件问题。

最佳答案

这个问题真的需要在电子堆栈交换或mc论坛。我认为问题在于初始化后的前2秒左右,数据包永远无法到达或通过phy。我不知道为什么,但我最近碰到过这个。我认为你的代码是好的,虽然我会使用UDPOpenEx,假设你有相当晚的芯片应用程序libs。尝试等待几秒钟,或重新发送。如果你连接的是IP而不是节点信息,那么它将是arp,而且可能会工作,因为arp必须首先解析。

关于c - PIC32以太网套件和UDP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9458559/

10-11 12:51