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 doesn't work

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



Joined: 05 Feb 2016
Posts: 33

View user's profile Send private message

UDP doesn't work
PostPosted: Thu Aug 25, 2016 7:54 pm     Reply with quote

Hello everyone,

Very basically I want to communicate my 18f4620 with my PC. The software which is at PC side is a server and the PIC is client.

I started the server and also tested by sending some datas from my android phone. I used two type of code. Here is:

Code:


#include <main.h>

enum states{SET_TICK_COUNTER,MONITORING,UDP_PUT_FAIL,TXD_PROCESS};
int8 adcData=0;
states state=SET_TICK_COUNTER;
int32 tickCounter=0;
UDP_SOCKET s1;


void IPAddressInit(void)
{
   //MAC address of this unit
   MY_MAC_BYTE1=0x00 ;
   MY_MAC_BYTE2=0x4;
   MY_MAC_BYTE3=0xA3;
   MY_MAC_BYTE4=0x00;
   MY_MAC_BYTE5=0x00;
   MY_MAC_BYTE6=0x00;

   //IP address of this unit
   MY_IP_BYTE1=MY_DEFAULT_IP_ADDR_BYTE1;
   MY_IP_BYTE2=MY_DEFAULT_IP_ADDR_BYTE2;
   MY_IP_BYTE3=MY_DEFAULT_IP_ADDR_BYTE3;
   MY_IP_BYTE4=MY_DEFAULT_IP_ADDR_BYTE4;

   //network gateway
   MY_GATE_BYTE1=MY_DEFAULT_GATE_BYTE1;
   MY_GATE_BYTE2=MY_DEFAULT_GATE_BYTE2;
   MY_GATE_BYTE3=MY_DEFAULT_GATE_BYTE3;
   MY_GATE_BYTE4=MY_DEFAULT_GATE_BYTE4;

   //subnet mask
   MY_MASK_BYTE1=MY_DEFAULT_MASK_BYTE1;
   MY_MASK_BYTE2=MY_DEFAULT_MASK_BYTE2;
   MY_MASK_BYTE3=MY_DEFAULT_MASK_BYTE3;
   MY_MASK_BYTE4=MY_DEFAULT_MASK_BYTE4;
}

void udpTask()

   switch(state)
   { 
      case SET_TICK_COUNTER:
         tickCounter = TickGet() + (TICK_SECOND);
         state = MONITORING;
      case MONITORING:
         if((long)(TickGet() - tickCounter) > 0)
         {
         output_high(PIN_B0);
         state=MONITORING;
         break;
           
         }else{
         
            state=UDP_PUT_FAIL;
            output_low(PIN_B0);
         }                 
      case UDP_PUT_FAIL:
         if((UDPIsPutReady(s1)>0))
         {  adcData=read_adc();
            state=TXD_PROCESS;
            output_high(PIN_B1);
         }
            else
            {  state=MONITORING;
               output_high(PIN_B0);
               break;
            }
      case TXD_PROCESS:
         
            UDPPut(adcData);
               
            UDPFlush();
            output_low(PIN_B1);
            state=SET_TICK_COUNTER;
            break;
         
      default:
         break; 
   }
   
}


void main()

   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(ALL_ANALOG);
   set_adc_channel(0);
   disable_interrupts(GLOBAL);
   set_tris_b(0x00);
   output_b(0xff);
   
   
   IPAddressInit();
   //TickInit();
   StackInit();
   output_low(PIN_B0);
   
   
   UDPInit();
   output_low(PIN_B1); 
 
   BOOL durum;
   
   do{
   s1=UDPOpen(NULL,"192.168.0.18",1453);
   durum=UDPIsOpened(s1);
   if(durum==FALSE)
      {  output_low(PIN_B2);
         StackTask();
      }else{
         output_high(PIN_B2);
      }
   }while(!durum);
   
   output_b(0x00);
   output_high(PIN_B7);
   
   while(TRUE)
   {

      // TCP/IP code
      StackTask();
      StackApplications();
     
      udpTask();
   }
      UDPClose(s1);//never will be executed

}






Then I thougt I had a mistake maybe and I wrote very simple code. It didn't also run.

Code:

#include <main.h>

int8 adcData=0;
BOOL durum=TRUE;
UDP_SOCKET s1;
int32 tickCounter;

void IPAddressInit(void)
{
   //MAC address of this unit
   MY_MAC_BYTE1=MY_DEFAULT_MAC_BYTE1;
   MY_MAC_BYTE2=MY_DEFAULT_MAC_BYTE2;
   MY_MAC_BYTE3=MY_DEFAULT_MAC_BYTE3;
   MY_MAC_BYTE4=MY_DEFAULT_MAC_BYTE4;
   MY_MAC_BYTE5=MY_DEFAULT_MAC_BYTE5;
   MY_MAC_BYTE6=MY_DEFAULT_MAC_BYTE6;

   //IP address of this unit
   MY_IP_BYTE1=MY_DEFAULT_IP_ADDR_BYTE1;
   MY_IP_BYTE2=MY_DEFAULT_IP_ADDR_BYTE2;
   MY_IP_BYTE3=MY_DEFAULT_IP_ADDR_BYTE3;
   MY_IP_BYTE4=MY_DEFAULT_IP_ADDR_BYTE4;

   //network gateway
   MY_GATE_BYTE1=MY_DEFAULT_GATE_BYTE1;
   MY_GATE_BYTE2=MY_DEFAULT_GATE_BYTE2;
   MY_GATE_BYTE3=MY_DEFAULT_GATE_BYTE3;
   MY_GATE_BYTE4=MY_DEFAULT_GATE_BYTE4;

   //subnet mask
   MY_MASK_BYTE1=MY_DEFAULT_MASK_BYTE1;
   MY_MASK_BYTE2=MY_DEFAULT_MASK_BYTE2;
   MY_MASK_BYTE3=MY_DEFAULT_MASK_BYTE3;
   MY_MASK_BYTE4=MY_DEFAULT_MASK_BYTE4;
}

void main()
{
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL|ADC_TAD_MUL_0);
   set_adc_channel(0);
   
   set_tris_b(0x00);
   output_b(0x00);

   IPAddressInit();
   TickInit();
   enable_interrupts(GLOBAL);
   StackInit();
   
   UDPInit();
   
   do{
   s1=UDPOpenEx("192.168.0.18",2,0,1453);
   //s1=UDPOpen(NULL,"192.168.0.18",1453);
   if(s1==INVALID_UDP_SOCKET)
      {output_high(PIN_B1);durum=FALSE;}
   }while(!durum);
   
   output_b(0x00);
   output_high(PIN_B0);
   
   
   while(TRUE)
   {

      // TCP/IP code
      StackTask();

      StackApplications();
     
      adcData=255;
     
      if((UDPIsPutReady(s1)>0))
        { 
            UDPPut(adcData);
            UDPFlush();   
         }
      }   
   
      //TODO: User Code
   }




At two situation machine gets an ip and response for any ping. But There were not any data at the server side.

Thanks.
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