cvi的UDP通信

#define READER_PORT         17224       // arbitrarily chosen from the Dynamic/Private port range (49152-65535)
#define MULTICAST_ADDRESS   "239.194.0.217" // arbitrarily selected from the multicast address range (224.0.0.0-239.255.255.255)
 

//发送端

int main (int argc, char *argv[])
{
    int error = 0;
    
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;  /* out of memory */
    if ((panelHandle = LoadPanel (0, "UDPWriter.uir", PANEL)) < 0)
        return -1;
    
    // Initialize some additional UI settings.
    errChk (SetCtrlAttribute(panelHandle, PANEL_DATA, ATTR_MAX_ENTRY_LENGTH, MAX_DATA_INPUT_LENGTH));
    errChk (SetCtrlAttribute(panelHandle, PANEL_UNICAST_IP, ATTR_MAX_ENTRY_LENGTH, MAX_IP_INPUT_LENGTH));
    errChk (SetCtrlVal(panelHandle, PANEL_MULTICAST_IP, MULTICAST_ADDRESS));
    
    // Create a UDP channel from which to send messages. The port number can be any available
    // port since we expect only to write, not receive data.

    //创建一个UDP通道对象,使用该对象发送和/或接收UDP数据报。
    errChk (CreateUDPChannel(UDP_ANY_LOCAL_PORT, &writerChannel));
    
    DisplayPanel (panelHandle);
    RunUserInterface ();
    DiscardPanel (panelHandle);
Error:
    if (writerChannel)
        DisposeUDPChannel(writerChannel);
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));
    return 0;
}

int CVICALLBACK Send (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    int error = 0;
    
    if (event == EVENT_COMMIT)
    {
        char data[MAX_DATA_INPUT_LENGTH + 1];
        char destAddr[MAX_IP_INPUT_LENGTH + 1];
        int unicast, multicast, broadcast;
        
       // errChk (GetCtrlVal(panel, PANEL_DATA, data));   
        memset(data, 1, 139);
        
        errChk (GetCtrlVal(panel, PANEL_UNICAST, &unicast));
        errChk (GetCtrlVal(panel, PANEL_MULTICAST, &multicast));
        errChk (GetCtrlVal(panel, PANEL_BROADCAST, &broadcast));
        // Depending on which radio button is selected, the message is addressed to one of:
        //  (1) a specific IP address or DNS-resolvable host name (Unicast),
        //  (2) a predetermined multicast address to which multiple readers may be subscribed (Multicast),
        //  (3) the broadcast address, which propogates to every computer on the subnet (Broadcast).
        if (unicast)
            errChk (GetCtrlVal(panel, PANEL_UNICAST_IP, destAddr));
        else if (multicast)
            errChk (GetCtrlVal(panel, PANEL_MULTICAST_IP, destAddr));
        else if (broadcast)
            strcpy(destAddr, UDP_LAN_BROADCAST_ADDR);
        
        // Write the message to a predetermined port number on which all readers will listening.
        errChk (UDPWrite(writerChannel, READER_PORT, destAddr, data, strlen(data) + 1));
    }
Error:
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));
    return 0;
}

/// HIFN Handles mutual exclusivity of the Unicast, Multicast, and Broadcast radio buttons.
int CVICALLBACK RadioCallback (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    int error = 0;
    
    if (event == EVENT_COMMIT)
    {
        int on;
        errChk (GetCtrlVal(panel, control, &on));
        if (!on)
        {
            // Ensure one radio button is always selected.
            errChk (SetCtrlVal(panel, control, 1));
        }
        else
        {                                                    
            if (control != PANEL_UNICAST)
                errChk (SetCtrlVal(panel, PANEL_UNICAST, 0));
            if (control != PANEL_MULTICAST)
                errChk (SetCtrlVal(panel, PANEL_MULTICAST, 0));
            if (control != PANEL_BROADCAST)
                errChk (SetCtrlVal(panel, PANEL_BROADCAST, 0)); 
        }
    }
Error:
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));
    return 0;
}

//接收端

//-----------------------------------------------------------------------------
// Program entry point                                                 
//-----------------------------------------------------------------------------
int main (int argc, char *argv[])
{
    int error = 0;
    
    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;  /* out of memory */
    if ((panelHandle = LoadPanel (0, "UDPReader.uir", PANEL)) < 0)
        return -1;
    
    errChk (SetCtrlVal(panelHandle, PANEL_MULTICAST_IP, MULTICAST_ADDRESS));
        
    errChk (CreateUDPChannelConfig(READER_PORT, UDP_ANY_ADDRESS, 0, UDPCallback, NULL, &readerChannel));
    
    DisplayPanel (panelHandle);
    RunUserInterface ();
    DiscardPanel (panelHandle);
Error:
    if (readerChannel)
        DisposeUDPChannel(readerChannel);
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));
    return 0;
}

//-----------------------------------------------------------------------------
// UDP callback function                                                
//-----------------------------------------------------------------------------
/// HIFN This function is called when data arrives on the UDP channel. It is up
/// HIFN  to this function to read the waiting data.
/// HIPAR channel/The channel that has waiting data.
/// HIPAR eventType/The type of UDP event. UDP_DATAREADY is the only event type.
/// HIPAR errCode/Non-zero indicates an error that occurred with the event.
/// HIPAR callbackData/Callback data (if any) that was assigned on the channel.
int CVICALLBACK UDPCallback (unsigned channel, int eventType, int errCode, void *callbackData)
{
    int   error = 0,  i,  size;
    unsigned char   *msg = NULL;
    
    if (eventType == UDP_DATAREADY)
    {
        char            srcAddr[16];
        unsigned int    srcPort;
        char            msgSourceString[16 + 10];
        
        // Pass NULL as the input buffer to determine the size of the arrived data.
        errChk (size = UDPRead(channel, NULL, 0, UDP_DO_NOT_WAIT, NULL, NULL));
        
        printf("size: %d\r\n", size);
        nullChk (msg = malloc(size));
        
        // Read the waiting message into the allocated buffer.
        errChk (size = UDPRead(channel, msg, size, UDP_DO_NOT_WAIT, &srcPort, srcAddr));
        
    //    printf("\r\n");
        for(i = 0; i < size; i++)
        {
            printf(" %d", msg[i]);     
        }
        
        printf("\r\n"); 
        
        // Display the message, preceeded by the sender's IP address and port number.
        sprintf(msgSourceString, "[%s:%d]: ", srcAddr, srcPort);
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msgSourceString));
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msg));
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, "\n"));
    }
Error:                                                             
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));;
    if (msg)
        free(msg);
    return 0;
}

//-----------------------------------------------------------------------------
// UI callback functions                                                
//-----------------------------------------------------------------------------
/// HIFN Toggles subscription to a predefined multicast address. When subscribed,
/// HIFN  this channel will be forwarded a copy of any message addressed to
/// HIFN  the multicast address and the reader's port number.
int CVICALLBACK DoMulticast (int panel, int control, int event,
        void *callbackData, int eventData1, int eventData2)
{
    int error = 0;
    
    if (event == EVENT_COMMIT)
    {
        int on;
        char ipAddr[16];
        
        errChk (GetCtrlVal(panel, control, &on));
        errChk (GetCtrlVal(panel, PANEL_MULTICAST_IP, ipAddr));
        
        if (on)
            errChk (UDPMulticastSubscribe(readerChannel, ipAddr, NULL)); 
        else
            errChk (UDPMulticastUnsubscribe(readerChannel, ipAddr, NULL)); 
    }
Error:
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35439171/article/details/84893016
今日推荐