Author Topic: also about the udp use!  (Read 9421 times)

Offline tr111

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
also about the udp use!
« on: August 29, 2007, 09:55:23 AM »
hello:
#ifdef SUPPORT_ADC
#define OWN_TASK  TASK_ADC
#define UDP_BUFFER_SIZE        512
#define MY_UDP_PORT            1999
 typedef struct stUDP_MESSAGE
    {
        unsigned short usLength;
        UDP_HEADER     tUDP_Header;                                        // reserve header space
        unsigned char  ucUDP_Message[UDP_BUFFER_SIZE];                   // reserve message space
    } UDP_MESSAGE;
static unsigned char ucUDP_IP_Address[IPV4_LENGTH] = {192, 168, 0, 37}; // address to send UDP test frames to
static USOCKET MyUDP_Socket;
static UDP_MESSAGE *ptrUDP_Frame;
unsigned char *p,buf[8]={0,1,2,3,4,5,6,7};
int err;
static int  fnUDPListner(USOCKET SocketNr, unsigned char ucEvent, unsigned char *ucIP, unsigned short usPortNr, unsigned char *data, unsigned short usLength)
{
   switch (ucEvent) {
   case UDP_EVENT_RXDATA:
      //if (usPortNr != MY_UDP_PORT) break;                            // ignore false ports
      //if (uMemcmp(ucIP, ucUDP_IP_Address, IPV4_LENGTH)) break;       // ignore if not from expected IP address

        //if (usLength <= UDP_BUFFER_SIZE) {                             // ignore frames which are too large
            //uMemcpy(&ptrUDP_Frame->ucUDP_Message, data, usLength);     // Send the received UDP frame back
         //   fnSendUDP(MyUDP_Socket, ucUDP_IP_Address, MY_UDP_PORT, (unsigned char*)&ptrUDP_Frame->tUDP_Header, usLength, OWN_TASK);
        //}
        fnSendUDP(MyUDP_Socket, ucIP, usPortNr, (data - sizeof(UDP_HEADER)), usLength, OWN_TASK); // echo back from transmitting IP and port
      break;

   case UDP_EVENT_PORT_UNREACHABLE:                                     // we have received information that this port is not available at the destination so quit
      break;
   }
   return 0;
}
static void fnConfigUDP(void)
{
    if (!((MyUDP_Socket = fnGetUDP_socket(TOS_MINIMISE_DELAY, fnUDPListner, (UDP_OPT_SEND_CS | UDP_OPT_CHECK_CS))) < 0)) {
        fnBindSocket(MyUDP_Socket, MY_UDP_PORT);                         // Bind socket
        ptrUDP_Frame    = uMalloc(sizeof(UDP_MESSAGE));                  // get some memory for UDP frame
    }
    else {
       return;                                                          // no socket - this must never happen (ensure that enough user UDP sockets have been defined - USER_UDP_SOCKETS in config.h)!!
    }
}



void fnADC(TTASKTABLE *ptrTaskTable)                           
{  //static UDP_MESSAGE *ptrUDP_Frame  = uMalloc(sizeof(UDP_MESSAGE));                  // get some memory for UDP frame
   const unsigned char test_data[] = {1,2,3,4,5,6,7,8};
int i;
//char err[1];
i=1;
    RUN_LED();                                          // hardware dependent
 

 if(i==1) fnConfigUDP(); 
 uMemcpy(&ptrUDP_Frame->ucUDP_Message, test_data, sizeof(test_data));     // Send the received UDP frame back
fnSendUDP(MyUDP_Socket, ucUDP_IP_Address, MY_UDP_PORT, (unsigned char*)&ptrUDP_Frame->tUDP_Header, sizeof(test_data), OWN_TASK);

i++;
if(i==100) i=2;
}
#endif

#ifdef SUPPORT_ADC
  { "ecDA",       fnADC,          NO_QUE,      (DELAY_LIMIT)(2.0 * SEC), (DELAY_LIMIT)(2 * SEC), UTASKER_STOP},  //
fnADC(TTASKTABLE *ptrTaskTable)  maybe run  per (2.0 * SEC)!
but I can get  the udp data at  pc one time!
I want to get  the udp data more ,because per 2 SECOND it will sent ,but only once I can get the data!
and the RUN_LED() is ok! the led can ON or OFF at per (2.0 * SEC)!                                   
WhY???
I want to get
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
after 2.0 * SEC
01 02 03 04 05 06 07 08
......................................

NO like that:
01 02 03 04 05 06 07 08
(over no more )

« Last Edit: August 29, 2007, 10:13:36 AM by tr111 »

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: also about the udp use!
« Reply #1 on: August 29, 2007, 10:54:30 AM »
Hi

You have correctly set up your new task to be scheduled at 2s interval and this is happening correctly, as you see on the LED.

The problem that you have is that you need to declare your variable int i as static int i.

As it is, the variable starts at 1 again each time the task is scheduled and so the initialisation is carried out each time - the second time will fail and overwrite the local UDP socket with an error code - subsequent fnSendUDP() calls will be to an invalid socket and also fail.

Good luck

Regards

Mark

Offline tr111

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Re: also about the udp use!
« Reply #2 on: August 30, 2007, 01:30:12 PM »
Thank you very much ! mark !