CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

UDP won't Work

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ashworth



Joined: 10 May 2006
Posts: 3

View user's profile Send private message Send e-mail

UDP won't Work
PostPosted: Wed May 10, 2006 2:38 am     Reply with quote

I have recently purchased a CCS Embedded Ethernet Development board which has the ENC28J60 on it. Everything was working fine - TCP/IP, HTTP etc until I tried the UDP example. As this is the one I really needed I think I need some Help. I am not an expert C programmer so don't even know if my code is correct. I have removed as much of the Chiff chaff as possible to leave only the code I need. All it should do is to stream out a few bytes to the specified port The LCD display is updating correctly but nothing is actually being transmitted via UDP as far as I can tell.


Code:

#define STACK_USE_ICMP  1
#define STACK_USE_ARP   1
#define STACK_USE_UDP   1

#define MAX_SOCKETS                     6//was 6
#define MAX_UDP_SOCKETS                 4//was 4

#include "ccstcpip.h"

#if STACK_USE_CCS_PICENS
 #include "drivers\mlcd.c"
#else
 #include "drivers\dlcd.c"
#endif


#define UDP_SOURCE_PORT 61556
#define UDP_DESTINATION_PORT  61557
#define UDP_NUM_LISTEN_SOCKETS   2//was 2

UDP_SOCKET last_rx_socket=INVALID_UDP_SOCKET;

#define tx_socket UDPOpen(UDP_SOURCE_PORT, &UDPSocketInfo[last_rx_socket].remoteNode, UDP_DESTINATION_PORT)


void UDPTxTask(void)
{
 //  static int8 tx_socket;
   int   Counter;
   char  Char1;

   Counter++;
   Char1=Counter;

//   tx_socket=UDPOpen(UDP_SOURCE_PORT, &UDPSocketInfo[last_rx_socket].remoteNode, UDP_DESTINATION_PORT);
   if (UDPIsPutReady(tx_socket))
   {
      UDPPut("H");
      UDPPut("E");
      UDPPut("L");
      UDPPut("L");
      UDPPut("O");
      UDPPut("\r");
      UDPPut("\n");
     UDPFlush();
 //     UDPClose(tx_socket);
      printf(lcd_putc,"\fVolts :%u",Counter);

    }
}

void main(void) {
   MACAddrInit();
   IPAddrInit();

   init_user_io();

   lcd_init();
   printf(lcd_putc,"\fUDP Test");
   delay_ms(2000);

   StackInit();

   while(TRUE)
   {
      StackTask();
      UDPTxTask();
      delay_ms(100);
   }
}
 :grin:
Guest








PostPosted: Thu May 18, 2006 7:50 am     Reply with quote

Hi ,


The problem is , when you use the CCS example , the fisrt things to do is to receive a packet . When the packet is received by the enc28j60 , the &UDPSocketInfo[last_rx_socket].remoteNode is update and the enc28j60 know the IP adress and the Mac adress

You must know the mac and ip adress. (wiht a arp request , see the TCP example )

Sorry , but my english is bad
Embarassed
Ashworth



Joined: 10 May 2006
Posts: 3

View user's profile Send private message Send e-mail

How to Trasmit UDP Only then
PostPosted: Thu May 18, 2006 8:30 am     Reply with quote

Thankyou for that information. It makes sense now.

Can you or anyone out there tell me how I just send out UDP packets to a specific port?

Being UDP, I didn't think that you need to know the destination IP only the port number?

I have been using Etherreal and I can see packets appearing, but for some reason its just not being picked up at the other end.

Thanks,

Rob
Alain



Joined: 18 Apr 2006
Posts: 3
Location: France

View user's profile Send private message

PostPosted: Thu May 18, 2006 3:22 pm     Reply with quote

You can see here , one example using UDP.
This is a client , and it try to send string "R/103" to the server "Devicelink" (192.168.0.2). When i know the ip server address , i try to know the mac address of the server to initialised the NODE_INFO DeviceLink (the server)

This work fine Smile

Code:
//IP address of the PC running DeviceLink.EXE
IP_ADDR server;
static NODE_INFO DeviceLink;



#define UDP_SOURCE_PORT 2500
#define UDP_DESTINATION_PORT  10000
#define UDP_NUM_LISTEN_SOCKETS   2







void UDPTxTask(void) {

   static int8 button;
   static TICKTYPE lastTick;
   static int8 tx_socket;



   static enum {UDP_TX_ARP_INIT=0,UDP_TX_ARP_REQ=1, UDP_TX_ARP_WAIT=2, UDP_TX_OPEN=3, UDP_TX_WAIT=4, UDP_TX_DEBOUNCE=5,
      UDP_TX_ISREADY=6} state=0;

   TICKTYPE currTick;
   int8 dis;
   currTick=TickGet();

   switch (state) {

    case UDP_TX_ARP_INIT:
         memcpy(&DeviceLink.IPAddr, &server, sizeof(IP_ADDR));
         printf(lcd_putc,"\nARP REQUEST     ");
         state=UDP_TX_ARP_REQ;

      case UDP_TX_ARP_REQ:
         if (ARPIsTxReady()) {
            ARPResolve(&DeviceLink.IPAddr);
            lastTick=currTick;
            state=UDP_TX_ARP_WAIT;
         }
         break;

      case UDP_TX_ARP_WAIT:
         if (ARPIsResolved(&DeviceLink.IPAddr, &DeviceLink.MACAddr)) {
            state=UDP_TX_WAIT;
            printf(lcd_putc,"\n READY !!   ");
         }
         else if (TickGetDiff(currTick, lastTick) > (TICKS_PER_SECOND * 2)) {
            state=UDP_TX_ARP_REQ;
         }
         break;



      case UDP_TX_WAIT:
         
         if (BUTTON1_PRESSED()) {
               state=UDP_TX_DEBOUNCE;
               button=1;
         }
           
         break;

      case UDP_TX_DEBOUNCE:
         if (button==1 && !BUTTON1_PRESSED())
            state=UDP_TX_OPEN;
       
         break;

      case UDP_TX_OPEN:
         tx_socket=UDPOpen(UDP_SOURCE_PORT, &DeviceLink, UDP_DESTINATION_PORT);
         if (tx_socket==INVALID_UDP_SOCKET)
         {
            printf("\r\nCan't open UDP socket for transmit");
            state=UDP_TX_WAIT;
         }
         else
         {
            printf("\r\nStart Transmit UDP BUTTON%U", button);
            state=UDP_TX_ISREADY;
         }
         break;

      case UDP_TX_ISREADY:
         if (UDPIsPutReady(tx_socket)) {
            printf(UDPPut, "R/103");
            UDPFlush();
            UDPClose(tx_socket);
            state=UDP_TX_WAIT;
            printf(lcd_putc,"\n M/A moteur");
         }
         break;
   }
}

void ServerAddrInit(void) {
   //IP address of the PC running TCPSERVER.EXE
   server.v[0]=192;
   server.v[1]=168;
   server.v[2]=0;
   server.v[3]=2;
}

void main(void) {
   MACAddrInit();
   IPAddrInit();

   init_user_io();

   lcd_init();
   printf(lcd_putc,"\f DEVICELINK\nPacific Fighter");

   ServerAddrInit();
   StackInit();

   while(TRUE) {
      StackTask();
      UDPTxTask();
     // UDPRxTask();
   }
}
Ashworth



Joined: 10 May 2006
Posts: 3

View user's profile Send private message Send e-mail

UDP Success
PostPosted: Wed Jun 07, 2006 4:08 am     Reply with quote

Thanks,

Sorry for late reply, but that works excellent. I tihk the problem was just that I wasn't specifing a destination IP.

Don't know how their example could ever work.

Thanks,

Rob
Ricardo
Guest







Can you post all code for me??
PostPosted: Fri Aug 04, 2006 4:54 pm     Reply with quote

Something is wrong here.
Cannot connect to my enc28j60.
Can you send your example and all files dependences, i�m trying to make in my
Send to [email protected]
Thanks
Sory bad english
thientaisodo



Joined: 31 Aug 2009
Posts: 5

View user's profile Send private message

PostPosted: Sat Aug 30, 2014 12:27 pm     Reply with quote

Alain wrote:
You can see here , one example using UDP.
This is a client , and it try to send string "R/103" to the server "Devicelink" (192.168.0.2). When i know the ip server address , i try to know the mac address of the server to initialised the NODE_INFO DeviceLink (the server)

This work fine Smile

Code:
//IP address of the PC running DeviceLink.EXE
IP_ADDR server;
static NODE_INFO DeviceLink;



#define UDP_SOURCE_PORT 2500
#define UDP_DESTINATION_PORT  10000
#define UDP_NUM_LISTEN_SOCKETS   2







void UDPTxTask(void) {

   static int8 button;
   static TICKTYPE lastTick;
   static int8 tx_socket;



   static enum {UDP_TX_ARP_INIT=0,UDP_TX_ARP_REQ=1, UDP_TX_ARP_WAIT=2, UDP_TX_OPEN=3, UDP_TX_WAIT=4, UDP_TX_DEBOUNCE=5,
      UDP_TX_ISREADY=6} state=0;

   TICKTYPE currTick;
   int8 dis;
   currTick=TickGet();

   switch (state) {

    case UDP_TX_ARP_INIT:
         memcpy(&DeviceLink.IPAddr, &server, sizeof(IP_ADDR));
         printf(lcd_putc,"\nARP REQUEST     ");
         state=UDP_TX_ARP_REQ;

      case UDP_TX_ARP_REQ:
         if (ARPIsTxReady()) {
            ARPResolve(&DeviceLink.IPAddr);
            lastTick=currTick;
            state=UDP_TX_ARP_WAIT;
         }
         break;

      case UDP_TX_ARP_WAIT:
         if (ARPIsResolved(&DeviceLink.IPAddr, &DeviceLink.MACAddr)) {
            state=UDP_TX_WAIT;
            printf(lcd_putc,"\n READY !!   ");
         }
         else if (TickGetDiff(currTick, lastTick) > (TICKS_PER_SECOND * 2)) {
            state=UDP_TX_ARP_REQ;
         }
         break;



      case UDP_TX_WAIT:
         
         if (BUTTON1_PRESSED()) {
               state=UDP_TX_DEBOUNCE;
               button=1;
         }
           
         break;

      case UDP_TX_DEBOUNCE:
         if (button==1 && !BUTTON1_PRESSED())
            state=UDP_TX_OPEN;
       
         break;

      case UDP_TX_OPEN:
         tx_socket=UDPOpen(UDP_SOURCE_PORT, &DeviceLink, UDP_DESTINATION_PORT);
         if (tx_socket==INVALID_UDP_SOCKET)
         {
            printf("\r\nCan't open UDP socket for transmit");
            state=UDP_TX_WAIT;
         }
         else
         {
            printf("\r\nStart Transmit UDP BUTTON%U", button);
            state=UDP_TX_ISREADY;
         }
         break;

      case UDP_TX_ISREADY:
         if (UDPIsPutReady(tx_socket)) {
            printf(UDPPut, "R/103");
            UDPFlush();
            UDPClose(tx_socket);
            state=UDP_TX_WAIT;
            printf(lcd_putc,"\n M/A moteur");
         }
         break;
   }
}

void ServerAddrInit(void) {
   //IP address of the PC running TCPSERVER.EXE
   server.v[0]=192;
   server.v[1]=168;
   server.v[2]=0;
   server.v[3]=2;
}

void main(void) {
   MACAddrInit();
   IPAddrInit();

   init_user_io();

   lcd_init();
   printf(lcd_putc,"\f DEVICELINK\nPacific Fighter");

   ServerAddrInit();
   StackInit();

   while(TRUE) {
      StackTask();
      UDPTxTask();
     // UDPRxTask();
   }
}


Hi @Alain !
Thanks for your UDPTxTask(), it's work very fine with me.
But i trouble with UDPRxTask(). It not work.
I'm using UDP_SOURCE_PORT 6666 and UDP_DESTINATION_PORT 6666 too for UDP transceive.
Please help me :(

Code:
void UDPRxTask(void)
{
   char  c;
   TICKTYPE currTick;

   currTick=TickGet();

   if (g_UDPRXSocket == INVALID_UDP_SOCKET)
   {
      g_UDPRXSocket = UDPOpen(UDP_SOURCE_PORT,NULL,INVALID_UDP_SOCKET);
   }
   else if (UDPIsGetReady(g_UDPRXSocket))
   {
      UDPGet(&c);
      enable_print_ip=0;
      printf(lcd_putchar,"UDP Received: %c            ",c);
      UDPDiscard();
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group