Mutiplexed Displays Using Ethernet Socket Server.

Dependencies:   SN74CBT3257 Binary_Switch OSL10564_74HC595

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "TCPServer.h"
00004 #include "TCPSocket.h"
00005 #include "SN74CBT3257.h"
00006 #include "Binary_Switch.h"
00007 #include "7segLed_HC595.h"
00008 
00009 #define NUM_OF_DIGIT 4
00010 
00011 
00012 #define IP         "192.168.1.190"
00013 #define GATEWAY    "192.168.1.1"
00014 #define MASK       "255.255.255.0"
00015 
00016  int   Validate_Recieved_Packet(char *Packet,int *num0, int *num1, int * num2,int *num3,
00017                                 int * num4, int *num5, int *num6, int *num7);
00018                                 
00019 Serial pc(USBTX,USBRX);
00020 SN74CBT3257  Bus(PD_0,PD_1,PD_2,PD_3,PD_4,PD_5,PD_6,PD_7 );
00021 BinarySwitch  Switch(PD_8,PD_9,PD_10,PD_11,PD_12,PD_13,PD_14,PD_15);
00022 SevenSegLed led_7segs(PC_12,PC_10, PC_9, PC_8,  NUM_OF_DIGIT);
00023                              
00024 int main()
00025 {
00026    
00027     int N0,N1,N2,N3;
00028     int N4,N5,N6,N7;
00029     int  valid=0;
00030     int SUCCESS=-1;
00031     printf("TCP server example\n");
00032     
00033     EthernetInterface eth;
00034     SocketAddress sockaddr;
00035     
00036      //eth.disconnect();
00037     int i=eth.set_network(IP,MASK,GATEWAY);
00038     printf("set IP status: %i \r\n",i);
00039     i=eth.connect();
00040     printf("connect status: %i \r\n",i);
00041     const char *ip = eth.get_ip_address();
00042     const char *mac = eth.get_mac_address();
00043     printf("IP address is: %s\n\r", ip ? ip : "No IP");
00044     printf("MAC address is: %s\n\r", mac ? mac : "No MAC");
00045     
00046     printf("set IP status: %i \r\n",i);    
00047     printf("The Server IP address is '%s'\n", eth.get_ip_address());
00048     
00049     TCPServer srv;
00050     TCPSocket client_sock;
00051     SocketAddress client_addr;
00052     char buffer[200] = {'\0'};
00053     char Rx_buffer[200] = {'\0'};  
00054     /* Open the server on ethernet stack */
00055     srv.open(&eth);
00056     
00057     /* Bind the HTTP port (TCP 80) to the server */
00058     srv.bind(eth.get_ip_address(), 23);
00059     
00060     /* Can handle x simultaneous connections */
00061    
00062     srv.listen(1);
00063 
00064 while(true)
00065 {
00066     srv.accept(&client_sock, &client_addr);
00067     printf("Accepted %s:%d\n", client_addr.get_ip_address(), 
00068                     client_addr.get_port());
00069     strcpy(buffer, "Hello \n\r");
00070    SUCCESS= client_sock.send(buffer, strlen(buffer));
00071    if(SUCCESS>=1)
00072    {
00073       printf("Sent : %s\r\n",buffer);
00074     SUCCESS=client_sock.recv(Rx_buffer, 200); 
00075     }
00076     
00077     if(SUCCESS>=1)
00078     {
00079     printf("Recieved : %s\r\n",Rx_buffer);
00080     printf("Am Here \r\n");  
00081     }
00082    // int Floor=Switch.Read_Switch_State();
00083 //    printf("   You are in Floor number %d : \r\n",Floor); 
00084     
00085     valid= Validate_Recieved_Packet(Rx_buffer,&N0,&N1,&N2,&N3,&N4,&N5,&N6,&N7); 
00086     if(valid==1 && SUCCESS>=1)
00087     {
00088         valid=-1 ;SUCCESS=-1;
00089         wait(0.1);
00090     Bus.Select_Floor_Zero();
00091   
00092     led_7segs=N0;
00093     printf("Floor 0 Bays Available :%d \r\n",N0);
00094     Bus.Select_Floor_One();
00095     led_7segs=N1;
00096      wait(0.1);
00097      
00098     printf("Floor 1 Bays Available : %d\r\n",N1);
00099     Bus.Select_Floor_Two();
00100     led_7segs=N2;
00101     wait(0.1);
00102     printf("Floor 2 Bays Available : %d\r\n",N2);
00103     Bus.Select_Floor_Three();
00104     led_7segs=N3;
00105     wait(0.1);
00106     printf("Floor 3 Bays Available : %d\r\n",N3);
00107     
00108     Bus.Select_Floor_Four();
00109     led_7segs=N4;
00110     printf("Floor 4 ays Available : %d\r\n",N4);
00111     Bus.Select_Floor_Five();
00112     led_7segs=N5;
00113     printf("Floor 5 ays Available : %d\r\n",N5);
00114     Bus.Select_Floor_Six();
00115     led_7segs=N6;
00116     wait(0.1);
00117     printf("Floor 6 ays Available : %d\r\n",N6);
00118     Bus.Select_Floor_Seven();
00119     led_7segs=N7;
00120     wait(0.1);
00121     printf("Floor 7 ays Available : %d\r\n",N7);
00122     wait(1);
00123     }
00124      client_sock.close();
00125     }
00126     
00127 }
00128 
00129  int   Validate_Recieved_Packet(char *Packet,int *num0, int *num1, int *num2,int *num3,
00130                                 int *num4, int *num5, int *num6, int *num7)
00131 {   
00132   if( Packet[0]=='E'  && Packet[1]=='S'  && Packet[2]=='T'  && Packet[3]=='S'  
00133                       && Packet[4]==' '   && Packet[12]==',' && Packet[20]==',' && Packet[28]==',' 
00134                       && Packet[36]==',' && Packet[44]==','  && Packet[52]==',' && Packet[60]==',' 
00135                       && Packet[68]==' ' && Packet[69]=='E' && Packet[70]=='S'  && Packet[71]=='T' 
00136                       && Packet[72]=='E')
00137    
00138    { 
00139     printf("Valid Data R4ecieved \r\n");  
00140     
00141     if(Packet[5]=='0' && Packet[6]=='0')
00142          { 
00143             printf("Zeroth Floor  \r\n"); 
00144             int one,two,three,four; 
00145             four=Packet[8]-0x30;
00146             three=Packet[9]-0x30;
00147             two=Packet[10]-0x30;
00148             one=Packet[11]-0x30;
00149             *num0=four*1000+three*100+two*10+one;
00150         }      
00151    
00152     if(Packet[13]=='0' && Packet[14]=='1')
00153          { 
00154             printf("First Floor  \r\n"); 
00155             int one,two,three,four; 
00156             four=Packet[16]-0x30;
00157             three=Packet[17]-0x30;
00158             two=Packet[18]-0x30;
00159             one=Packet[19]-0x30;
00160             *num1=four*1000+three*100+two*10+one;
00161         }    
00162      
00163      if(Packet[21]=='0' && Packet[22]=='2')
00164         { 
00165             printf("Second Floor  \r\n"); 
00166             int one,two,three,four; 
00167             four=Packet[24]-0x30;
00168             three=Packet[25]-0x30;
00169             two=Packet[26]-0x30;
00170             one=Packet[27]-0x30;
00171             *num2=four*1000+three*100+two*10+one;
00172         }          
00173            
00174     if(Packet[29]=='0' && Packet[30]=='3')
00175         { 
00176             printf("Three Floor  \r\n"); 
00177             int one,two,three,four; 
00178             four=Packet[32]-0x30;
00179             three=Packet[33]-0x30;
00180             two=Packet[34]-0x30;
00181             one=Packet[35]-0x30;
00182             *num3=four*1000+three*100+two*10+one;
00183         }       
00184    
00185    if(Packet[37]=='0' && Packet[38]=='4')
00186         { 
00187             printf("Fourth Floor  \r\n"); 
00188             int one,two,three,four; 
00189             four=Packet[40]-0x30;
00190             three=Packet[41]-0x30;
00191             two=Packet[42]-0x30;
00192             one=Packet[43]-0x30;
00193             *num4=four*1000+three*100+two*10+one;
00194         }   
00195    
00196     if(Packet[45]=='0' && Packet[46]=='5')
00197         { 
00198              printf("Fifth Floor  \r\n"); 
00199             int one,two,three,four; 
00200             four=Packet[48]-0x30;
00201             three=Packet[49]-0x30;
00202             two=Packet[50]-0x30;
00203             one=Packet[51]-0x30;
00204             *num5=four*1000+three*100+two*10+one;
00205         }   
00206    
00207    if(Packet[53]=='0' && Packet[54]=='6')
00208         { 
00209             printf("Sixth Floor  \r\n"); 
00210             int one,two,three,four; 
00211             four=Packet[56]-0x30;
00212             three=Packet[57]-0x30;
00213             two=Packet[58]-0x30;
00214             one=Packet[59]-0x30;
00215             *num6=four*1000+three*100+two*10+one;
00216         }    
00217    
00218     if(Packet[61]=='0' && Packet[62]=='7')
00219         { 
00220             int one,two,three,four; 
00221             four=Packet[64]-0x30;
00222             three=Packet[65]-0x30;
00223             two=Packet[66]-0x30;
00224             one=Packet[67]-0x30;
00225             *num7=four*1000+three*100+two*10+one;
00226             printf("Seventh Floor :  \r\n"); 
00227         }   
00228         return 1;     
00229    }
00230    else
00231    {
00232      return -1;  
00233     }
00234 }
00235 
00236 
00237 
00238 int Validate_Card_Reply_Packet(char *rbuffer,char *barc_string)
00239 {
00240     int i=0,j=0;
00241     if(rbuffer[0]=='1' &&rbuffer[1]=='1' && rbuffer[2]=='1' &&rbuffer[3]=='1'
00242             && rbuffer[4]==':' &&rbuffer[19]==',' && rbuffer[20]=='C' &&
00243                rbuffer[21]=='A' && rbuffer[22]=='R'  && rbuffer[23]=='D' && rbuffer[24]==',')
00244 
00245     {
00246         //printf("Valid card ................................Reply \r\n");
00247         i=25;
00248         while(rbuffer[i]!=',')
00249          {
00250             if(rbuffer[i]=='\r'|rbuffer[i]=='\n') {
00251                 i++;
00252             }
00253             else {
00254                 barc_string[j]=rbuffer[i];
00255                 i++;
00256                 j++;
00257             }
00258          } 
00259         barc_string[i++]='\0';
00260         //printf(" Valid card reply:%s",barc_string);
00261         return j;
00262     } 
00263     else
00264         return 0;
00265        
00266 }
00267 
00268 
00269