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 support@ccsinfo.com

Help with UDP rx/tx

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



Joined: 13 May 2013
Posts: 10

View user's profile Send private message

Help with UDP rx/tx
PostPosted: Tue Apr 01, 2014 12:57 pm     Reply with quote

I need to send rfid id via ethernet to one pc that will replay with id acquired, this through ethernet.

I have choosed udp protocol but i don't understand why sometimes socket becomes invalid and program stop to receives data.

This is my code:
Code:
   
    #include <a.h>
    #include "my_lcd.c"
    #include "DS3231.c"
     
    #define LCD_ON output_low(PIN_B4);
    #define LCD_OFF output_high(PIN_B4);
     
    #define STACK_USE_ICMP  1
    #define STACK_USE_ARP   1
    #define STACK_USE_UDP   1
     
    #define MAX_SOCKETS     6 
    #define MAX_UDP_SOCKETS 4
     
    UDP_SOCKET last_rx_socket=INVALID_UDP_SOCKET;
    #CASE // make compiler case sensative
    #define UDP_SOURCE_PORT 1024  //set port to receive data from
    #define UDP_DESTINATION_PORT  1025  //set port to send data to
    #define UDP_NUM_LISTEN_SOCKETS   2  //set maximum number of computers to communicate with
     
    void PicInit(void);
    void UDPTxTask(void);
    void UDPRxTask(void);
     
    NODE_INFO RemoteNode;
     
    int counter=0,crediti=0,ok=0,w=0;
    unsigned char LeggiSeriale[3] = {0xA0, 0x01, 0x01};
    char *weekday[] = {"", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab","Dom"}; 
    int8 VoceMenu=0;
     
    unsigned char  year = 14;
    unsigned char month = 3;
    unsigned char date = 12;
    unsigned char day = 3;
    unsigned char hours = 15;
    unsigned char minutes = 9;
    unsigned char seconds = 0;
    unsigned short pm_am;
    register float temp;
     
    void AzzeraBuffer();
    void Read_Data();
    void RSeriale();
    void Read_I2C();
    unsigned char ibuffer[4]={0,0,0,0},RX[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
     
     
    void IPAddressInit(void)
    {
     //IP address of this unit
       MY_IP_BYTE1=192;
       MY_IP_BYTE2=168;
       MY_IP_BYTE3=1;
       MY_IP_BYTE4=84;
     
       //network gateway
       MY_GATE_BYTE1=192;
       MY_GATE_BYTE2=168;
       MY_GATE_BYTE3=1;
       MY_GATE_BYTE4=254;
     
       //subnet mask
       MY_MASK_BYTE1=255;
       MY_MASK_BYTE2=255;
       MY_MASK_BYTE3=255;
       MY_MASK_BYTE4=0;
     
       //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;
    }
     
     
    void main()
    {
             PicInit();
             IPAddressInit(); 
             StackInit();         
             while(1)       
             {
             
                StackTask();
                UDPRxTask();
                UDPTxTask();
               
            if (ibuffer[0]!=0){
             printf(lcd_putc,"\nRX:     %2X%2X%2X%2X", ibuffer[0],ibuffer[1],ibuffer[2],ibuffer[3]);
             ibuffer[0]=0;
             UDPDiscard();
               delay_ms(200);
            }
                if (!input(PIN_D0)){
                   Read_Data();
       
         if (ok==1){
         lcd_putc("\fCARD ID:");
         printf(lcd_putc,"%2X%2X%2X%2X", RX[3],RX[4],RX[5],RX[6]);
         delay_ms(1000);
          }
          else
            UDPDiscard();
                   }
                }
    }
     
     
     
    void PicInit(void){
    LCD_ON;
    delay_ms(500);
    LCD_OFF;
    //DS3231_init();
    delay_ms(500);
    LCD_ON;
    delay_ms(500);
    LCD_OFF;
    //setDate(day,date,month,year);
    //setTime(hours,minutes,seconds,0,0);
    lcd_init();
    delay_ms(500);
    LCD_ON;
    delay_ms(500);
    LCD_OFF;
    lcd_putc("\fReader ETH v1.00\n(c) 2014 Pompili F.");
    delay_ms(1000);
    enable_interrupts(GLOBAL);
    LCD_ON;
     }
     
     
    void AzzeraBuffer(){
    for (counter=0;counter<16;counter++)
    RX[counter]=0x00;
    } 
     
     
    void Read_Data(){
    ok=0x0;
    crediti=0x0;
    RSeriale();
    if(RX[1]!=0x01 || RX[2]!=0x00)
    {ok=0;return;};
    ok=1;
    return ;
    }
     
    void RSeriale(){
    AzzeraBuffer();
    i2c_start();
    for(counter=0;counter<3;counter++)  i2c_write(LeggiSeriale[counter]);
    i2c_stop();
    delay_ms(200);
    Read_I2C();
    }

    void Read_I2C(){
    i2c_start();
    i2c_write(0xA1);
    RX[0]=i2c_read(1);
    for(counter=1;counter<RX[0];counter++)
    RX[counter]=i2c_read(1);
    RX[counter]=i2c_read(0);
    i2c_stop();
    }
     
    void UDPRxTask(void) {
       static enum {UDP_RX_OPEN=0, UDP_RX_LISTEN=1}
                state=0;
       static UDP_SOCKET socket=INVALID_UDP_SOCKET;
       int8 z=0, c;
       
          if (socket==INVALID_UDP_SOCKET)
             state=UDP_RX_OPEN;
     
          switch (state) {
             case UDP_RX_OPEN:
                socket=UDPOpen(UDP_SOURCE_PORT,NULL,INVALID_UDP_SOCKET);
                if (socket!=INVALID_UDP_SOCKET) {
                   printf("\r\nUDP SOCKET %U OPEN FOR LISTENING", socket);
                   state=UDP_RX_LISTEN;
                }
                break;
     
             case UDP_RX_LISTEN:
                if (UDPIsGetReady(socket)) {
                   last_rx_socket=socket;
                 
                   printf(" SP:%LU DP:%LU S:%U] - ",
                      UDPSocketInfo[socket].remotePort,
                      UDPSocketInfo[socket].localPort,
                      socket
                   );
                 
                   while(UDPGet(&c)) {
                      ibuffer[z]=c;
                      printf("%u",ibuffer[z]);
                      z++;
                   }
                 
                }
                break;
          }
       
    }
     
    void UDPTxTask(void) {
       static enum
       {UDP_TX_OPEN=2, UDP_TX_WAIT=0, UDP_TX_ISREADY=3} stato=0;
       static int8 tx_socket;
     
       switch (stato) {
          case UDP_TX_WAIT:
             if (tx_socket==INVALID_UDP_SOCKET) {
             stato=UDP_TX_OPEN;
             }
          case UDP_TX_OPEN:
           tx_socket=UDPOpen(UDP_SOURCE_PORT, NULL, UDP_DESTINATION_PORT);
             if (tx_socket==INVALID_UDP_SOCKET)
             {
                printf("\r\nCan't open UDP socket for transmit");
                stato=UDP_TX_WAIT;
             }
             else
             {
                stato=UDP_TX_ISREADY;
             }
          case UDP_TX_ISREADY:
       
             if ((UDPIsPutReady(tx_socket)) && (RX[3]!=0)) {
           
             UDPPut(RX[3]);
             UDPPut(RX[4]);
             UDPPut(RX[5]);
             UDPPut(RX[6]);
             UDPFlush();
             AzzeraBuffer();
             }
             stato=UDP_TX_WAIT;
             UDPClose(tx_socket);
       }
    }
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