Functions for TCP server and mikroC dsPic


#1

Below are functions to set wiz550io as TCP server
Fill free to use they are for MikroC IDE and dsPic in C Any improvemt and suggestion are welcome.
for debug resons I use Uart2 so first of all you have to

    Uart2_Init(115200);  // DEBUG port use uart2
    U1MODEbits.ALTIO = 1;               // clear the way for SPI

if you don’t like the messages to uart2 then comment the uart lines
to begine TCP server

 do { // main loop start here
  

        do{   //  Socket 0 initialize
          SocketRegister_WRITE(Sn_MR ,SelectSocket_0_Register,Sn_MR_TCP);         //protocol = TCP
          SocketRegister_WRITE(Sn_PORT0,SelectSocket_0_Register, SocketPort[1]);  // set port number
          SocketRegister_WRITE(Sn_PORT1,SelectSocket_0_Register, SocketPort[2]);
          SocketRegister_WRITE(Sn_CR,SelectSocket_0_Register,SockCMD_OPEN);                //give the open command
          if(SocketRegister_READ(Sn_SR,SelectSocket_0_Register) != Sn_SR_SOCK_INIT){       //wait for the socket to come online
            SocketRegister_WRITE(Sn_CR,SelectSocket_0_Register,SockCMD_CLOSE);
            }
         }while(SocketRegister_READ(Sn_SR,SelectSocket_0_Register) != Sn_SR_SOCK_INIT);
                   UART_Write_Text("Soket 0 opend at port : " );
                   Addr_Hi= SocketRegister_Read(Sn_PORT0,SelectSocket_0_Register);
                   Addr_Lo= SocketRegister_READ(Sn_PORT1,SelectSocket_0_Register);
                   Int_Addr=make16(Addr_Hi,Addr_Lo) ;
                    IntToStr(Int_Addr,IntTxT);  UART_Write_Text(IntTxT); UART_Write_Text("   "); UART_Write(0x0D);UART_Wr_Ptr(0xA);

        // wait until Socket 0 gose to Listen

        do{
            /* listen SOCKET
            Sn_CR = LISTEN;
            // wait until Sn_SR is changed to SOCK_LISTEN
            if (Sn_SR != SOCK_LISTEN) Sn_CR = CLOSE; goto START;
            */
           SocketRegister_WRITE(Sn_CR ,SelectSocket_0_Register,SockCMD_LISTEN);
           }while (SocketRegister_READ(Sn_SR,SelectSocket_0_Register) !=Sn_SR_SOCK_LISTEN);

          // wait here until someone wanta to comunicate with you
          do{
             //  if (Sn_SR == SOCK_ESTABLISHED) goto ESTABLISHED stage;

            UART_Write_Text("Soket 0 now is at state : " );
                   i= SocketRegister_Read(Sn_SR,SelectSocket_0_Register);
                    ByteToStr(i,ByteTxt);  UART_Write_Text(ByteTxt); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);

            UART_Write_Text("Soket 0 Sn_IR : " );
                   i= SocketRegister_Read(Sn_IR,SelectSocket_0_Register);
                    ByteToStr(i,ByteTxt);  UART_Write_Text(ByteTxt); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);
           
           UART_Write_Text("Soket 0 number of bytes received : " );
                           Number_Hi= SocketRegister_Read(Sn_RX_RSR0,SelectSocket_0_Register);
                           Number_Lo= SocketRegister_Read(Sn_RX_RSR1,SelectSocket_0_Register);
                            intToStr(make16(number_hi,Number_Lo),IntTxT);  UART_Write_Text(IntTxT); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);


                 /* you have to watch Sn_IR not Sn_SR
                 Second methode of Establishment in TCP manual does not work (sn_SR==Sn_SR_SOCK_ESTABLISHED) DOES NOT WORK
                 I must watch Sn_IR o be con== 0x1
                 */
             }while(SocketRegister_READ(Sn_IR,SelectSocket_0_Register)==0);
            
               // now data are here -->prosses data
            SocketRegister_READ(Sn_IR,SelectSocket_0_Register);
             if((i & Sn_IR_CON)==Sn_IR_CON){
                  // you MUST write Sn_IR for : a) sn_SR become Established b) to can get number of byts arrived
                   SocketRegister_WRITE(Sn_IR ,SelectSocket_0_Register,1);
                 }
                           UART_Write_Text("Soket 0 now is at state : " );
                           i= SocketRegister_Read(Sn_SR,SelectSocket_0_Register);
                            ByteToStr(i,ByteTxt);  UART_Write_Text(ByteTxt); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);

                           UART_Write_Text("Soket 0 Sn_IR : " );
                           i= SocketRegister_Read(Sn_IR,SelectSocket_0_Register);
                            ByteToStr(i,ByteTxt);  UART_Write_Text(ByteTxt); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);

                           UART_Write_Text("Soket 0 number of bytes received : " );
                           Number_Hi= SocketRegister_Read(Sn_RX_RSR0,SelectSocket_0_Register);
                           Number_Lo= SocketRegister_Read(Sn_RX_RSR1,SelectSocket_0_Register);
                            intToStr(make16(number_hi,Number_Lo),IntTxT);  UART_Write_Text(IntTxT); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);

                 SocketInterrupt=SocketRegister_READ(Sn_IR,SelectSocket_0_Register);
                 SocketInterrupt = SocketInterrupt & Sn_IR_RECV;    // 0x4  data received
                 while(SocketInterrupt==Sn_IR_RECV){   // proessess packet
                       SocketRegister_WRITE(Sn_IR,SelectSocket_0_Register,Sn_IR_RECV);
                       // transfer data to receive buffer
                       // read from socket 0 pointer len of received size
                      Number_Hi=SocketRegister_READ(Sn_RX_RD0,SelectSocket_0_Register);
                      Number_Lo= SocketRegister_READ(Sn_RX_RD1,SelectSocket_0_Register);
                              StartAddressPointer =Number_Hi * 256 + Number_Lo;
                              UART_Write_Text("Start Address Pointer: "); IntToStr(StartAddressPointer,IntTxT);UART_Write_Text(IntTxT);uart_write(0xD);UART_Write(0xA);

                      Number_Hi=SocketRegister_READ(Sn_RX_RSR0,SelectSocket_0_Register);
                      Number_Lo=SocketRegister_READ(Sn_RX_RSR1,SelectSocket_0_Register);
                              Sn_RX_ReceivedSize=Number_Hi * 256 + Number_lo;
                              UART_Write_Text("Received Bytes Len: "); IntToStr(Sn_RX_ReceivedSize,IntTxT);UART_Write_Text(IntTxT);uart_write(0xD);UART_Write(0xA);

                       WizEnablePin=0; Delay_us(2);        // /SCS   ACTIVE LOW   begin SPI frame
                       i=((StartAddressPointer & 0xFF00) >> 8);
                       SPI1_Write(i);
                       i=(StartAddressPointer & 0x00FF);
                       SPI1_Write(i);
                       SPI1_Write(Select_Socket_0_RX_Buffer);
                       for (Len_of_bytes=0;Len_of_bytes<Sn_RX_ReceivedSize;Len_of_bytes++){
                           i= SPI1_Read(i);                // read data   this pointer forward
                            ReceiveBuffer[Len_of_bytes] =i;
                       }
                       WizEnablePin = 0x01;                   //  /SCS  end SPI frame
                       delay_us(2);

                     // increase Sn_RX_RD as length of len
                      StartAddressPointer += Sn_RX_ReceivedSize;
                      Split_Number(StartAddressPointer);
                      SocketRegister_WRITE(Sn_RX_RD0,SelectSocket_0_Register,Number_Hi);
                      SocketRegister_WRITE(Sn_RX_RD1,SelectSocket_0_Register,Number_Lo);
                      // set RECV command -->Sn_CR = RECV;

                      SocketRegister_WRITE(Sn_CR,SelectSocket_0_Register,SockCMD_RECV);

                      // examin the first 5 bytes and do
                      if(memcmp(ReceiveBuffer, "Get /", 5)) {      // only GET method is supported here
                         showMainPage();
                         }
                      if(memcmp(ReceiveBuffer, "Get /sms", 8)) {      // only GET method is supported here
                         SendSmsValues();
                         }
                      if(memcmp(ReceiveBuffer, "Get /set", 8)) {      // only GET method is supported here
                         SendSetupPage();
                         }

                       /* do{
                             UART_Write_Text("Received Bytes Len: "); IntToStr(Sn_RX_ReceivedSize,IntTxT);UART_Write_Text(IntTxT);uart_write(0xD);UART_Write(0xA);
                             UART_Write_Text("Text received :");
                             for (Len_of_bytes=0;Len_of_bytes<Sn_RX_ReceivedSize;Len_of_bytes++){
                                 UART_Write(ReceiveBuffer[Len_of_bytes]);
                               }
                                UART_Write(0xD);UART_Write(0xA);
                           UART_Write_Text("Soket 0 Sn_IR : " );
                           i= SocketRegister_Read(Sn_IR,SelectSocket_0_Register);
                            ByteToStr(i,ByteTxt);  UART_Write_Text(ByteTxt); UART_Write_Text("   ");UART_Write(0x0D);UART_Wr_Ptr(0xA);

                         }while(1);
                         */
                         // read Sn IR for while loop
                         SocketInterrupt=SocketRegister_READ(Sn_IR,SelectSocket_0_Register);
                         SocketInterrupt = SocketInterrupt & Sn_IR_RECV;    // 0x4  data received

                  }

and responce back with

void showMainPage(){
unsigned int Total_Bytes_Sent=0;
int len_to=0;

 char msg1[]="<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><title>anemometer </title>"
    "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-7'>";
.........
.........
// close page
 char msg13[]="</tr></table></div></div></body></html>";

      // start to send page
      Total_Bytes_Sent= Write_Tx_Buffer(msg1);
      .......
.........
.........
      
        SocketRegister_WRITE(Sn_CR,SelectSocket_0_Register,SockCMD_DISCON);  // send disconect
        i= SocketRegister_Read(Sn_IR,SelectSocket_0_Register);
        if ((i & 0x2)==0x2){  SocketRegister_WRITE(Sn_IR,SelectSocket_0_Register,0x2); }  // disconect
       SocketRegister_WRITE(Sn_CR,SelectSocket_0_Register,SockCMD_SEND);


}    // end of showMainPage()
unsigned int Write_Tx_Buffer(char msgString[]){
   unsigned int sn_tx_wr=0;
   unsigned int i=0;
  // this function write the msgstring to module buffer and returns the legth of the string
  Sn_TX_TxSize=strlen(msgString);
  
            // write to TX_buffer at  pointer
            Number_Hi=SocketRegister_READ(Sn_TX_RD0,SelectSocket_0_Register);
            Number_Lo=SocketRegister_READ(Sn_TX_RD1,SelectSocket_0_Register);
            StartAddressPointer=Number_Hi * 256 + Number_Lo;
            
                      WizEnablePin=0; Delay_us(2);        // /SCS   ACTIVE LOW   begin SPI frame
                       i=((StartAddressPointer & 0xFF00) >> 8);
                       SPI1_Write(i);
                       i=(StartAddressPointer & 0x00FF);
                       SPI1_Write(i);
                       SPI1_Write(Selects_Socket_0_TX_Buffer);
                       for (Len_of_bytes=0;Len_of_bytes<Sn_TX_TxSize;Len_of_bytes++){
                           SPI1_Write(msgString[Len_of_bytes]);  // write this pointer Tx_Buffer forward
                       }
                       WizEnablePin = 0x01;                   //  /SCS  end SPI frame
                       delay_us(2);
              Number_Hi=SocketRegister_READ(Sn_TX_WR0,SelectSocket_0_Register);
              Number_Lo=SocketRegister_READ(Sn_TX_WR1,SelectSocket_0_Register);
              sn_tx_wr=(Number_Hi * 256 + Number_Lo) + Sn_TX_TxSize;
              Split_Number(sn_tx_wr);
              SocketRegister_WRITE(Sn_TX_WR0,SelectSocket_0_Register,Number_Hi);
              SocketRegister_WRITE(Sn_TX_WR1,SelectSocket_0_Register,Number_Lo);

        // send command SEND
        SocketRegister_WRITE(Sn_CR,SelectSocket_0_Register,SockCMD_SEND);
        delay_us(2);   // give some time
         i= SocketRegister_Read(Sn_IR,SelectSocket_0_Register);
         if ((i & 0x10)==0x10){  SocketRegister_WRITE(Sn_IR,SelectSocket_0_Register,0x10); }  // send OK

   return  Sn_TX_TxSize;

}

#2

change the
char msg1[]="HTTP/1.1 200 OK\n Content-type:text/html; \n\n…
this will lead all browser to treat packets as HTML

THIS WORKS only on pc

function are tested and works properly
you can use them