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

The TCP connection

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



Joined: 28 Apr 2006
Posts: 2
Location: Malaysia

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

The TCP connection
PostPosted: Thu Jul 06, 2006 12:17 am     Reply with quote

Hi,

I've test the ENC28J60 with the code from CCS. The test our was all right, just only I've something that are not understood.

1) How I going to make the TCP connection once the devices was connected and will not have the timeout? From the example program, once the TCP connection was connected, it will run for sometime and will disconnected from the connection. Is there any possible that they is no timeout are invilve?

2) I found out that I've make the connection from the TCP Client software, given by CCS, and after few times connect and disconnect, the devices was shows the "SOCKET ERROR". However, the TCP Client software still can connect to the devices, but I can't receive any data when I press button and the data send was displayed on the LCD. How I going to fix it?

3) When I press the reset, sometime the devices is not respones to the PC, meants when the TCP Clients software connot connect to the devices, but sometimes it can. How to overcome this problem?

4) How I going to copy the webpages into the EEPROM, because there is no way that I write the webpage inside the main program?


Code:

//////////////////////////////////////////////////////////////////////////////
//
// ex13b.c - Example 13B from the Embedded Internet/Embedded Ethernet tutorial
//
// A TCP example, where the PIC acts as a TCP server.  Connects to a PC running
// the CCS provided example program TCP_CLIENT.EXE. 
//
// NOTE: Change the code in IPAddrInit() to your desired IP address, which
//       is based on your network.
//
//////////////////////////////////////////////////////////////////////////////

#define STACK_USE_ICMP  1
#define STACK_USE_ARP   1
#define STACK_USE_TCP   1
#include "ccstcpip.h"

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

#define NUM_LISTEN_SOCKETS 2

#define EXAMPLE_TCP_PORT   (int16)1000

char lcd_str[NUM_LISTEN_SOCKETS][20];

//this function is called by MyTCPTask() when the specified socket is connected
//to the PC running the TCPSERVER.EXE demo.
//returns TRUE if BUTTON2 was pressed, therefore we must disconnect the socket
int8 TCPConnectedTask(TCP_SOCKET socket, int8 which) {
   char c;
   static int8 counter[NUM_LISTEN_SOCKETS];
   static int8 button1_held[NUM_LISTEN_SOCKETS];
   char str[20];
   int8 i=0;

   if (TCPIsGetReady(socket)) {
      while (TCPGet(socket, &c)) {
         lcd_str[which][i++]=c;
         if (i>=20) {i=19;}
         lcd_str[which][i]=0;
      }
   }

//when button 1 is pressed: send message over TCP
//when button 2 is pressed: disconnect socket
   if (BUTTON1_PRESSED() && !button1_held[which] && TCPIsPutReady(socket)) {
      button1_held[which]=TRUE;
      sprintf(str,"BUTTON C=%U",counter[which]++);
      TCPPutArray(socket,str,strlen(str));
      TCPFlush(socket);
   }
   if (!BUTTON1_PRESSED()) {
      button1_held[which]=FALSE;
   }
  #if defined(BUTTON2_PRESSED())
   if (BUTTON2_PRESSED()) {
      return(TRUE);
   }
  #endif
   return(FALSE);
}


void MyTCPTask() {
   static TICKTYPE lastTick[NUM_LISTEN_SOCKETS];
   static TCP_SOCKET socket[NUM_LISTEN_SOCKETS]={INVALID_SOCKET};
   static enum {
      MYTCP_STATE_NEW=0, MYTCP_STATE_LISTENING=1,
      MYTCP_STATE_CONNECTED=2, MYTCP_STATE_DISCONNECT=3,
      MYTCP_STATE_FORCE_DISCONNECT=4
   } state[NUM_LISTEN_SOCKETS]={0};
   static NODE_INFO remote[NUM_LISTEN_SOCKETS];
   TICKTYPE currTick;
   int8 dis;
   int8 i;

   currTick=TickGet();

   for (i=0;i<NUM_LISTEN_SOCKETS> ((int16)TICKS_PER_SECOND * 300)) {
                  state[i]=MYTCP_STATE_DISCONNECT;
                  sprintf(&lcd_str[i][0],"TIMEOUT");
                  lastTick[i]=currTick;
               }
               else {
                  dis=TCPConnectedTask(socket[i],i);
                  if (dis) {
                     sprintf(&lcd_str[i][0],"DISCONNECT");
                     state[i]=MYTCP_STATE_DISCONNECT;
                     lastTick[i]=currTick;
                  }
               }
            }
            else {
               sprintf(&lcd_str[i][0],"DISCONNECTED");
               state[i]=MYTCP_STATE_FORCE_DISCONNECT;
            }
            break;

         case MYTCP_STATE_DISCONNECT:
            if (TCPIsPutReady(socket[i])) {
               state[i]=MYTCP_STATE_FORCE_DISCONNECT;
            }
            else if (TickGetDiff(currTick, lastTick[i]) > (TICKS_PER_SECOND * 10)) {
               state[i]=MYTCP_STATE_FORCE_DISCONNECT;
            }
            break;

         case MYTCP_STATE_FORCE_DISCONNECT:
            TCPDisconnect(socket[i]);
            state[i]=MYTCP_STATE_NEW;
            break;
      }
   }
}

void LCDTask(void) {
   static TICKTYPE lastTick;
   TICKTYPE currTick;
   static enum {LCD_STATE_DISPLAY=0, LCD_STATE_WAIT=1} state=0;

   currTick=TickGet();

   switch(state) {
      case LCD_STATE_DISPLAY:
         printf(lcd_putc,"\f%s\n%s",&lcd_str[0][0],&lcd_str[1][0]);
         state=LCD_STATE_WAIT;
         lastTick=currTick;
         break;

      case LCD_STATE_WAIT:
         if (TickGetDiff(currTick,lastTick) > (TICKS_PER_SECOND / 4))
            state=LCD_STATE_DISPLAY;
         break;
   }
}

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

   init_user_io();

   lcd_init();

   sprintf(&lcd_str[0][0],"INIT");
   sprintf(&lcd_str[1][0],"INIT");
   lcd_putc('\f');

   StackInit();

   while(TRUE) {
      StackTask();
      MyTCPTask();
      LCDTask();
   }
}



Thanks.

Regards

Kah Joo
_________________
Everything can be done, do you willing to do it!!!
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