/*
 ============================================================================
 Name        : FinalProject.c
 Author      : Ning, Guanghan
 Version     :
 Copyright   : University of Missouri
 Description : Hello World in C, Ansi-style
 ============================================================================
 */
//======================================================
//For TCP image transmit
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 1000

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>
//=========================================================

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>    //header for creating threads
#include <sys/time.h>   //header for time
#include <unistd.h>
#include <fcntl.h>
#include "serial_ece4220.h"  //header for Reading from Serial Program

#include <semaphore.h>  //header for semaphores
#include "rtai.h"       //headers for rtai
#include "rtai_lxrt.h"
#include "rtai_sched.h"
#include "sys/time.h"    //header for time

//#include "stdafx.h"
#include "chenLeeCV.h"
#include "segment.h"

int kernel_pressed= 0;
int segment_finished= 0;
int flag= -1, flag1= -1; flag2= -1;
//int flag_num_received= 0;
int flag_num1_received= 0;
int flag_num2_received= 0;
int flag_num4_received= 0;
int first_time= 1;
int N_img= 1;
ClImage* segmentImg;


int digit1, digit2, digit4;
typedef struct Plate
{
	int* segment1;
	int* segment2;
	int* segment3;
	int* segment4;
}plate;

//==================
//socket

//================


//==============================================================================
//simulating Computer Vision Functions
void Read_image()
{
   //free(segmentImg);
   char *imgName; int save_success;
   sprintf(imgName, "%d%s", N_img, ".bmp");
   segmentImg = clLoadImage(imgName);
   save_success = clSaveImage("result.bmp", segmentImg);

   printf("An image has been read!\n"); N_img++;
   kernel_pressed= 0;
}
void PlateDetection(plate* detected_plate)
{
  // printf("PlateDetection done!\n");
}
void Segmentation(int* s1, int* s2, int*s3, int*s4)
{
	s1= s2= s3= s4= 0; //simulate the process of segmentation
   printf("Segmentation done!\n");
}
//===============================================================================
//helpful small functions
int receive(char* msg, char* buffer, int q)
{
	int same;
	same= strncmp(msg, buffer, q);  //note that the length should be the same!!!
	if(same== 0) //means (msg == buffer)
	{
	printf("%s received!\n", msg);
	return 1;
	}
	else
	{
	//printf("%s not received!\n", msg);
	return 0;
	}
}
//================================================================================
//Functions in threads
void SendSegment4(int* s)
{
	  //Server of sending images
	  //Send images to child boards using TCP protocal(as server)
    /*
      //printf("HAHA\n");
	  FILE *fd=fopen("segment.jpg","rb");
	  int server_sockfd,client_sockfd;
	  int server_len,client_len;
	  struct sockaddr_in server_address;
	  struct sockaddr_in client_address;
	  server_sockfd=socket(AF_INET,SOCK_STREAM,0);
	  server_address.sin_family=AF_INET;
	  server_address.sin_addr.s_addr=inet_addr("10.3.52.16");
	  server_address.sin_port= 2002;
	  server_len=sizeof(server_address);
	  bind(server_sockfd,(struct sockaddr *)&server_address,server_len);
	  listen(server_sockfd, 5);
	  char ch[MAXSIZE];
	  client_len=sizeof(client_address);
	  client_sockfd=accept(server_sockfd,(struct sockaddr *)&client_address,&client_len);
	  while(!feof(fd)){
	  fread(ch,1,MAXSIZE,fd);
	  write(client_sockfd,ch,MAXSIZE);
	}
	  close(client_sockfd);
	  fclose(fd);

	  //return 0;
*/
	         //send recognized number to the main board(as server)
		     int sockfd, new_fd;
		     struct sockaddr_in my_addr;
		     struct sockaddr_in their_addr;
		     unsigned int sin_size,myport,listnum;
		     myport = 2012;
		     listnum = 10; //maximum listening number > number of characters in a plate

		     if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1 )
		     {
		         perror("socket is error/n;");
		         exit(1);
		     }
		     my_addr.sin_family = PF_INET;
		     my_addr.sin_port = htons(myport);
		     my_addr.sin_addr.s_addr = INADDR_ANY;  //my ip address
		     bzero(&(my_addr.sin_zero),0);

		     //=====================================================
		     their_addr.sin_family = PF_INET;
		     their_addr.sin_port = htons(myport);
		     their_addr.sin_addr.s_addr =  inet_addr("10.3.52.15");  //all ip address can be clients
			 bzero(&(their_addr.sin_zero),0);
	         //========================================================

		      if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
		     {
		         perror("bind is error\n");
		         exit(1);
		     }

		     if(listen(sockfd,listnum) == -1)
		     {
		         perror("listen is error\n");
		         exit(1);
		     }
		     //printf("HELLO?!\n");
		     printf("start to listen for child Board <4>\n");


		     while(1)
		     {
		         sin_size = sizeof(struct sockaddr_in);

		         if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size)) == -1)
		         {
		             perror("accept is error\n");
		             continue;

		         }
		         printf("Main board server to send segment<4>:\n got connection from child board %s\n",inet_ntoa(their_addr.sin_addr));
               int connected = 1;

		                 char *p;
		                 char sock_buf[1024];
		                 bzero(sock_buf, 1024);
		                 p = sock_buf;

		         time_t t;  int rand_n;


		         while(connected)
		         {
		        	 if(flag>=0)
		        	 {
		        		 flag1= flag; //copy flag for another sendsegments
		        		 flag2= flag; //copy flag for another sendsegments
		        		 usleep(1000000); //wait for 1s
		        		                 srand((unsigned)time(&t));
		        		 		         rand_n= rand()%9;   // A random number from 0 to 10
		        		 		         char random_n[2];
		        		 		         random_n[0]= '0'+ rand_n;
		        		 printf("send 4th segment!\n");
						 //if(send(new_fd,"8\n",14, 0) == -1)
						 if(send(new_fd, &random_n[0],14, 0) == -1)
						 {
							 perror("send is error\n");
							 close(new_fd);
							 exit(0);
						 }
						 flag= -1;

		        	 }
		         }
		     }
}
void SendSegment1(int* s)
{
	         //send recognized number to the main board(as server)
		     int sockfd, new_fd;
		     struct sockaddr_in my_addr;
		     struct sockaddr_in their_addr;
		     unsigned int sin_size,myport,listnum;
		     myport = 2013;
		     listnum = 10; //maximum listening number > number of characters in a plate

		     if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1 )
		     {
		         perror("socket is error/n;");
		         exit(1);
		     }
		     my_addr.sin_family = PF_INET;
		     my_addr.sin_port = htons(myport);
		     my_addr.sin_addr.s_addr = INADDR_ANY;  //my ip address
		     bzero(&(my_addr.sin_zero),0);

		     //=====================================================
		     their_addr.sin_family = PF_INET;
		     their_addr.sin_port = htons(myport);
		     their_addr.sin_addr.s_addr =  inet_addr("10.3.52.14");  //all ip address can be clients
			 bzero(&(their_addr.sin_zero),0);
	         //========================================================

		      if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
		     {
		         perror("bind is error\n");
		         exit(1);
		     }

		     if(listen(sockfd,listnum) == -1)
		     {
		         perror("listen is error\n");
		         exit(1);
		     }
		     //printf("HELLO?!\n");
		     printf("start to listen for child Board <1>\n");


		     while(1)
		     {
		         sin_size = sizeof(struct sockaddr_in);

		         if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size)) == -1)
		         {
		             perror("accept is error\n");
		             continue;

		         }
		         printf("Main board server to send segment<1>:\n got connection from child board %s\n",inet_ntoa(their_addr.sin_addr));
               int connected = 1;

		                 char *p;
		                 char sock_buf[1024];
		                 bzero(sock_buf, 1024);
		                 p = sock_buf;

		         time_t t;  int rand_n;


		         while(connected)
		         {
		        	 if(flag1>=0)
		        	 {
		        		 usleep(1000000); //wait for 1s
		        		                 srand((unsigned)time(&t));
		        		 		         rand_n= rand()%5 + rand()%5;   // A random number from 0 to 10
		        		 		         char random_n[2];
		        		 		         random_n[0]= '0'+ rand_n;
		        		 printf("send 1st segment!\n");
						 //if(send(new_fd,"8\n",14, 0) == -1)
						 if(send(new_fd, &random_n[0],14, 0) == -1)
						 {
							 perror("send is error\n");
							 close(new_fd);
							 exit(0);
						 }
						 flag1= -1;
						 /*Only one sendsegment thread clears the flag*/
		        	 }
		         }
		     }
}
void SendSegment2(int* s)
{
	         //send recognized number to the main board(as server)
		     int sockfd, new_fd;
		     struct sockaddr_in my_addr;
		     struct sockaddr_in their_addr;
		     unsigned int sin_size,myport,listnum;
		     myport = 2014;
		     listnum = 10; //maximum listening number > number of characters in a plate

		     if((sockfd = socket(PF_INET,SOCK_STREAM, 0)) == -1 )
		     {
		         perror("socket is error/n;");
		         exit(1);
		     }
		     my_addr.sin_family = PF_INET;
		     my_addr.sin_port = htons(myport);
		     my_addr.sin_addr.s_addr = INADDR_ANY;  //my ip address
		     bzero(&(my_addr.sin_zero),0);

		     //=====================================================
		     their_addr.sin_family = PF_INET;
		     their_addr.sin_port = htons(myport);
		     their_addr.sin_addr.s_addr =  inet_addr("10.3.52.13");  //all ip address can be clients
			 bzero(&(their_addr.sin_zero),0);
	         //========================================================

		      if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr)) == -1)
		     {
		         perror("bind is error\n");
		         exit(1);
		     }

		     if(listen(sockfd,listnum) == -1)
		     {
		         perror("listen is error\n");
		         exit(1);
		     }
		     //printf("HELLO?!\n");
		     printf("start to listen for child Board <2>\n");


		     while(1)
		     {
		         sin_size = sizeof(struct sockaddr_in);

		         if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size)) == -1)
		         {
		             perror("accept is error\n");
		             continue;

		         }
		         printf("Main board server to send segment<2>:\n got connection from child board %s\n",inet_ntoa(their_addr.sin_addr));
               int connected = 1;

		                 char *p;
		                 char sock_buf[1024];
		                 bzero(sock_buf, 1024);
		                 p = sock_buf;

		         time_t t;  int rand_n;


		         while(connected)
		         {
		        	 if(flag2>=0)
		        	 {
		        		 usleep(1000000); //wait for 1s
		        		                 srand((unsigned)time(&t));
		        		 		         rand_n= rand()%4 + rand()%6;   // A random number from 0 to 10
		        		 		         char random_n[2];
		        		 		         random_n[0]= '0'+ rand_n;
		        		 printf("send 2nd segment!\n");
						 //if(send(new_fd,"8\n",14, 0) == -1)
						 if(send(new_fd, &random_n[0],14, 0) == -1)
						 {
							 perror("send is error\n");
							 close(new_fd);
							 exit(0);
						 }
						 flag2= -2;
						 /*Only one sendsegment thread clears the flag*/
		        	 }
		         }
		     }
}
void ReceiveNumber1(int* p_num)
{
	//Client of receiving numbers
	//Receive numbers from child boards: TCP protocal
	int sockfd;
		char buffer[1024];
		struct sockaddr_in server_addr;
		struct hostent *host;
		int portnumber,nbytes;

		if((host=gethostbyname("10.3.52.14"))==NULL)
		{
		printf("Gethostname error\n");
		//exit(1);
		}

		//segmentation fault here

		if((portnumber=atoi("2001"))<0)
		{
		printf("Port number error\n");
		//exit(1);
		}
		if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
		{
		printf("Socket Error:%s\a\n",strerror(errno));
		//exit(1);
		}
		bzero(&server_addr,sizeof(server_addr));
		server_addr.sin_family=AF_INET;
		server_addr.sin_port=htons(portnumber);
		server_addr.sin_addr=*((struct in_addr *)host->h_addr);
		if(connect(sockfd,(struct sockaddr *)(&server_addr),
		sizeof(struct sockaddr))==-1)
		{
		printf("Connect Error:%s\a\n",strerror(errno));
		//exit(1);
		}

		while(1)
		{
			if((nbytes=read(sockfd,buffer,1024))==-1)
			{
			printf("Read Error:%s\n",strerror(errno));
			//exit(1);
			}
			buffer[nbytes]='\0';
			printf("Received number from child board(1):  %s\n",buffer);

			//Send received numbers to main thread by named pipes
			//*p_num= buffer[nbytes-1]-'0';
			*p_num= buffer[0] -'0';
			 //printf("*p_num= %d\n", *p_num);
			//Send received numbers to main thread by named pipes
			// int pipe1;
			// pipe1= open("Number1",O_WRONLY);
		    // write(pipe1, p_num, sizeof(int));
			//num1= *p_num;
			digit1= *p_num;
			 flag_num1_received= 1;


			 usleep(2000000); //wait for 0.1s
		}
		 close(sockfd);

}
void ReceiveNumber2(int* p_num)
{
	//Receive numbers from child boards: TCP protocal
	int sockfd;
			char buffer[1024];
			struct sockaddr_in server_addr;
			struct hostent *host;
			int portnumber,nbytes;

			if((host=gethostbyname("10.3.52.13"))==NULL)
			{
			printf("Gethostname error\n");
			//exit(1);
			}

			//segmentation fault here

			if((portnumber=atoi("2002"))<0)
			{
			printf("Port number error\n");
			//exit(1);
			}
			if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
			{
			printf("Socket Error:%s\a\n",strerror(errno));
			//exit(1);
			}
			bzero(&server_addr,sizeof(server_addr));
			server_addr.sin_family=AF_INET;
			server_addr.sin_port=htons(portnumber);
			server_addr.sin_addr=*((struct in_addr *)host->h_addr);
			if(connect(sockfd,(struct sockaddr *)(&server_addr),
			sizeof(struct sockaddr))==-1)
			{
			printf("Connect Error:%s\a\n",strerror(errno));
			//exit(1);
			}

			while(1)
			{
				if((nbytes=read(sockfd,buffer,1024))==-1)
				{
				printf("Read Error:%s\n",strerror(errno));
				//exit(1);
				}
				buffer[nbytes]='\0';
				printf("Received number from child board(2):  %s\n",buffer);

				//Send received numbers to main thread by named pipes
				//*p_num= buffer[nbytes-1]-'0';
				*p_num= buffer[0] -'0';
				 //printf("*p_num= %d\n", *p_num);
				//Send received numbers to main thread by named pipes
				// int pipe1;
				// pipe1= open("Number1",O_WRONLY);
			    // write(pipe1, p_num, sizeof(int));
				//num1= *p_num;
				 digit2= *p_num;
				 flag_num2_received= 1;


				 usleep(2000000); //wait for 0.1s
			}
			 close(sockfd);

}
void ReceiveNumber3(int* p_num)
{
	//Receive numbers from child boards: TCP protocal







	//Send received numbers to main thread by named pipes
	 int pipe3;
	 pipe3= open("Number3",O_WRONLY);
     write(pipe3, p_num, sizeof(int));
}
void ReceiveNumber4(int* p_num)
{

	//Receive numbers from child boards: TCP protocal as client
	int sockfd;
	char buffer[1024];
	struct sockaddr_in server_addr;
	struct hostent *host;
	int portnumber,nbytes;

	if((host=gethostbyname("10.3.52.15"))==NULL)
	{
	printf("Gethostname error\n");
	//exit(1);
	}

	//segmentation fault here

	if((portnumber=atoi("2004"))<0)
	{
	printf("Port number error\n");
	//exit(1);
	}
	if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
	{
	printf("Socket Error:%s\a\n",strerror(errno));
	//exit(1);
	}
	bzero(&server_addr,sizeof(server_addr));
	server_addr.sin_family=AF_INET;
	server_addr.sin_port=htons(portnumber);
	server_addr.sin_addr=*((struct in_addr *)host->h_addr);
	if(connect(sockfd,(struct sockaddr *)(&server_addr),
	sizeof(struct sockaddr))==-1)
	{
	printf("Connect Error:%s\a\n",strerror(errno));
	//exit(1);
	}

	while(1)
	{
		if((nbytes=read(sockfd,buffer,1024))==-1)
		{
		printf("Read Error:%s\n",strerror(errno));
		//exit(1);
		}
		buffer[nbytes]='\0';
		printf("Received number from child board(4):  %s\n",buffer);

		//Send received numbers to main thread by named pipes
		//*p_num= buffer[nbytes-1]-'0';
		*p_num= buffer[0] -'0';
		 //printf("*p_num= %d\n", *p_num);
		// int pipe4;
		// pipe4= open("Number4",O_WRONLY);
		// write(pipe4, p_num, sizeof(int));
		//num4= *p_num;
		digit4= *p_num;
		 flag_num4_received= 1;


		 usleep(2000000); //wait for 0.1s
	}
	 close(sockfd);

}
void ReadImage(plate *detected_plate)
{

	int check;
	//printf("flag = %d\n", flag);
	/*
	pthread_t     seg1_thread;
	pthread_t     seg2_thread;
	pthread_t     seg3_thread;
	pthread_t     seg4_thread;*/
	pthread_t     num1_thread;
    pthread_t     num2_thread;
    pthread_t     num3_thread;
    pthread_t     num4_thread;
    int num1, num2, num3, num4;
    num1=7; num2=8; num3=6; num4= 4;

	int fd_fifo_in;
	fd_fifo_in= open("/dev/rtf/1", O_RDWR);  //1 is fifo_write from kernel. then it is fifo_in for user space

    //int pipe1;
   // pipe1= open("prints",O_WRONLY);
	first_time= 1;
	  while(1)
	{
		  usleep(250000);
		//read from fifo to see if button is pressed
		 //printf("In READIMAGE, flag= %d\n", flag);
		flag = read(fd_fifo_in, &kernel_pressed, sizeof(kernel_pressed));
		//flag2= -1;
		//printf("\n kernel_pressed = %d\n", kernel_pressed);
		printf("\n Button is pressed!\n");
		//printf("(First time, flag)= (%d, %d)\n", first_time, flag);
		if ((flag>=0)&&(first_time== 1))
		{
			     printf("Begin to receive number from child boards!\n");

			    check = pthread_create(&num1_thread, NULL, (void *)&ReceiveNumber1, (void *)(&num1));
				  if (check!=0)  printf("num1_thread error\n");
				check = pthread_create(&num2_thread, NULL, (void *)&ReceiveNumber2, (void *)(&num2));
				  if (check!=0)  printf("num2_thread error\n");
				//check = pthread_create(&num3_thread, NULL, (void *)&ReceiveNumber3, (void *)(&num3));
				 // if (check!=0)  printf("num3_thread error\n");*/
				check = pthread_create(&num4_thread, NULL, (void *)&ReceiveNumber4, (void *)(&num4));
				  if (check!=0)  printf("num4_thread error\n");

			     first_time= 0;
			     usleep(250000);
			     flag= -1;
			     /*
			     segment_finished= 0;
				 flag2= 2;
				 Read_image();
				 PlateDetection(detected_plate);
				 Segmentation((*detected_plate).segment1,(*detected_plate).segment2, (*detected_plate).segment3, (*detected_plate).segment4 );
				 segment_finished= 1;   //flag to indicate this for the other 2 set of threads
				 usleep(250000);
				 flag= -1;*/
		}
		if(flag>= 0)
		{
			segment_finished= 0;
			//flag2= 2;
			Read_image();
			PlateDetection(detected_plate);
		    Segmentation((*detected_plate).segment1,(*detected_plate).segment2, (*detected_plate).segment3, (*detected_plate).segment4 );
		    segment_finished= 1;   //flag to indicate this for the other 2 set of threads
		    usleep(250000);
		    flag= -1;
		} else;
	}
}

//=================================================================================

int main(void)
{
	//--------------------------------------------------------------------------//
	printf("----------------------------------------------------------------\n");
	printf("---------Plate segmentation and recognition---------------------\n");
	printf("-----------------Main Board Program-----------------------------\n");
	printf("----------------------------------------------------------------\n");
	printf("\n\n\n\n\n\n\n");


   //------------------------Declaration of variables--------------------------//
   int check;//, flag= -1;
   int pipe1, pipe2, pipe3, pipe4;
   //unsigned char *kernel_buffer;
   pthread_t     ReadImage_thread;

   pthread_t     seg1_thread;
   pthread_t     seg2_thread;
   pthread_t     seg3_thread;
   pthread_t     seg4_thread;

   pthread_t     num1_thread;
   pthread_t     num2_thread;
   pthread_t     num3_thread;
   pthread_t     num4_thread;


   int *num1, *num2, *num3, *num4;
   plate *detected_plate= (plate*)malloc(sizeof(plate));
   num1= (*detected_plate).segment1;
   num2= (*detected_plate).segment2;
   num3= (*detected_plate).segment3;
   num4= (*detected_plate).segment4;
   //
  //*num1=7; *num2=8; *num3=6; *num4= 4;

   int fd_fifo_in, fd_fifo_out;
   fd_fifo_in= open("/dev/rtf/1", O_RDWR);  //1 is fifo_write from kernel. then it is fifo_in for user space
   fd_fifo_out= open("/dev/rtf/0", O_RDWR);

   system("mkfifo Number1 >& /dev/null");     //create a pipe file
   system("mkfifo Number2 >& /dev/null");     //create another pipe file
   system("mkfifo Number3 >& /dev/null");     //create a pipe file
   system("mkfifo Number4 >& /dev/null");     //create another pipe file

   //Create the thread to read images from memory and do extraction and segmentation
   check = pthread_create(&ReadImage_thread, NULL, (void *)&ReadImage, (void *)(detected_plate));
   if (check!=0)  printf("ReadImage_thread error\n");
   //wait until this is finished
   //pthread_join(ReadImage_thread, NULL);

   //Create the thread to read images from memory
    check = pthread_create(&seg1_thread, NULL, (void *)&SendSegment1, (void *)((*detected_plate).segment1));
     if (check!=0)  printf("seg1_thread error\n");
   check = pthread_create(&seg2_thread, NULL, (void *)&SendSegment2, (void *)((*detected_plate).segment2));
     if (check!=0)  printf("seg2_thread error\n");
  //  check = pthread_create(&seg3_thread, NULL, (void *)&SendSegment, (void *)((*detected_plate).segment3));
   //    if (check!=0)  printf("seg3_thread error\n");
   check = pthread_create(&seg4_thread, NULL, (void *)&SendSegment4, (void *)((*detected_plate).segment4));
	 if (check!=0)  printf("seg4_thread error\n");

     //pthread_join(seg1_thread, NULL);
     //pthread_join(seg2_thread, NULL);
     //pthread_join(seg3_thread, NULL);
     //pthread_join(seg4_thread, NULL);



	  //pthread_join(num1_thread, NULL);
	  //pthread_join(num2_thread, NULL);
	  //pthread_join(num3_thread, NULL);
	  //pthread_join(num4_thread, NULL);



	  while(1)
   {
	   //exhibit the recognized numbers when button is pressed
	   //usleep(200000);
	   //usleep(250000);

	   // flag = read(fd_fifo_in, &flag, sizeof(flag));
	   //flag2= read(fd_fifo_in, &kernel_pressed, sizeof(kernel_pressed));
	   //printf("In while, flag2= %d\n", flag2);
		  if((flag_num1_received)&&(flag_num2_received)&&(flag_num4_received))//if(1)////if(flag2>= 0) //
	   {
			 //pipe1= open("Number1",O_RDWR);
			 //pipe2= open("Number2",O_RDWR);
			 //pipe3= open("Number3",O_RDWR);
			 //pipe4= open("Number4",O_RDWR);
	     /*
	     pipe1= open("Number1",O_RDONLY);
	     pipe2= open("Number2",O_RDONLY);
	     pipe3= open("Number3",O_RDONLY);
	     */
	    // pipe1= open("Number1",O_RDONLY);
	    // pipe4= open("Number4",O_RDONLY);

	    //printf("In while, flag2= %d\n", flag2);
        //read(pipe1, &num1, sizeof(num1));
		//read(pipe2, &num2, sizeof(num2));
		//read(pipe3, &num3, sizeof(num3));
	   // read(pipe1, &num1, sizeof(num1));
	//	read(pipe4, &num4, sizeof(num4));

		//flag2= -1;

	      // num1= (*detected_plate).segment1;
	       //num4= (*detected_plate).segment4;
		   printf("The plate number is: ** *%d*%d%d\n", digit4, digit2, digit1);
		   flag_num1_received= 0;
		   flag_num2_received= 0;
		   flag_num4_received= 0;
	   }
   }

	  pthread_join(ReadImage_thread, NULL);



   //kernel_buffer= malloc(num_bytes_to_read* sizeof(unsigned char));

   return EXIT_SUCCESS;
}

