biomed12
Joined: 05 Feb 2016 Posts: 33
|
UDP doesn't work |
Posted: Thu Aug 25, 2016 7:54 pm |
|
|
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. |
|