/* program for PC1*/
#include<stdio.h>
#include<termios.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
/************************ port open() *****************************************/
int port_open(void)
{
int port_fd;
port_fd=open("/dev/ttyS0",O_RDWR|O_NONBLOCK|O_NOCTTY);
// printf("Value of port fd is %d\n",port_fd);
if(port_fd==-1)
{
perror("Enable to open com port");
exit(0);
}
else
{
// printf("Com port opened successfully!!!\n");
fcntl(port_fd,F_SETFL,0);
}
return port_fd;
}
/************************ port config() *****************************************/
void port_config(int fd)
{
int ret,ret1,ret2;
struct termios settings;
// printf("Value of fd in port config is %d\n",fd);
tcgetattr(fd,&settings);
/*settng the baud rate*/
//add error checking
ret1= cfsetispeed(&settings,B9600);
ret2=cfsetospeed(&settings,B9600);
if(ret1==-1 && ret2==-1)
{
perror("config failed for setting baud rate");
exit(0);
}
// else printf("com port baud rate config successfull\n");
/*set local mode & enable reciever */
settings.c_cflag |= (CLOCAL|CREAD);
settings.c_cflag &= ~PARENB;
settings.c_cflag &= ~CSTOPB;
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8;
//add error checking
ret=tcsetattr(fd,TCSANOW,&settings);
if(ret==-1)
{
perror("config failed");
exit(0);
}
// else printf("com port config successfull\n");
}
/************************ main() *****************************************/
int main(int argc,char *argv[])
{
int fd,port_fd;
unsigned char uart_frame[255],filename_length,packets=-1;
unsigned char packet_number=1;
int file_length=0,ret1=1,ret2=0;
unsigned char buf[249],filename_buf[255],data_buf[249];
int i,bytes_written,data_bytes_written;
int j,k;
if(argc!=2)
{
printf("Enter the filename\n");
exit(0);
}
fd=open(argv[1],O_RDONLY);/* opening the file for reading*/
if(fd==-1)
{
perror("Cannot open file");
exit(1);
}
else
printf("File opened succesfully\n");
filename_length=strlen(argv[1]);/* string length of filename*/
printf("\nfilename length is = %d\n",filename_length);
/*****calculation of number of packets created*******/
while(ret1 > 0)
{
ret1=(read(fd,buf,sizeof(buf)));
packets++;
}
close(fd);
fd=open(argv[1],O_RDONLY);/* opening the file for reading*/
printf("packets created: %d\n",packets);
strcpy(filename_buf,argv[1]) ;
//printf("%s",filename_buf);
port_fd=port_open();
port_config(port_fd);
/*forming filename packet*/
/*********************************see this section carefully***********************************/
uart_frame[0]= 'Q';
uart_frame[1]='F'; //file frame(name of file) indication
uart_frame[2]= packets;// 0x34;
uart_frame[3]= filename_length;//0x36;
for(i=0;i<filename_length;i++)
{
uart_frame[i+4] = filename_buf[i];
}
uart_frame[i+4]= 0x10;
uart_frame[i+5]= 0x0d;
uart_frame[i+6]= '\0';
printf("contents of uart_frame are:=>%s\n",uart_frame);
bytes_written=write(port_fd,uart_frame,filename_length+6);
if(bytes_written<0)
{
perror("error in writting\n");
exit(0);
}
else
{
printf("no of bytes written %d\n",bytes_written);
}
printf("packet in frame is %s\n",&uart_frame);
/*forming data packet*/
//sleep(1);
for(j=0;j<packets;j++)
{
//sleep(2);
printf("in for loop \n");
ret1= read(fd,data_buf,sizeof(data_buf)) ;
//printf(" string is%s\n ",data_buf);
printf("read data=%d\n ",ret1);
printf("i am in data packet write\n");
uart_frame[0]= 'X';
uart_frame[1]='D';
uart_frame[2]= packet_number+1;//packets;// 0x34;
uart_frame[3]= ret1;//0x36;
for(i=0;i<ret1;i++)
uart_frame[i+4] = data_buf[i];
uart_frame[i+4]= '\r';
uart_frame[i+5]= '\n';
uart_frame[i+6]= '\0';
data_bytes_written=write(port_fd,uart_frame,ret1+6);
if(data_bytes_written<0)
{
perror("error in writting\n");
exit(0);
}
else
{
printf("no of data bytes written %d\n",data_bytes_written);
}
printf("packet in frame is %s\n",&uart_frame);
//uart_frame[]='';
}
close(port_fd);
close(fd);
return 0;
}
//program for pc2
/****************************************************************************************************************************************************************************
PROGRAM : TO RECEIVE PROGRAM VIA RS-232
****************************************************************************************************************************************************************************/
/****************Standerd Header Files*******************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<termios.h>
#include<sys/types.h>
#include<sys/stat.h>
unsigned char Data[10240];
unsigned char array[255];
static unsigned char *ptr;
int port_open(void) //defination of function port open
{
int port_fd;
port_fd=open("/dev/ttyS0",O_RDONLY|O_NONBLOCK|O_NOCTTY);
if(port_fd==-1)
{
perror("Enable to open com port");
exit(0);
}
else
{
printf("Com port opened successfully!!!\n");
fcntl(port_fd,F_SETFL,0);
}
return port_fd;
} //End of port open
void port_config(int fd) //defination of function port config
{
int ret,ret1,ret2;
struct termios settings;
tcgetattr(fd,&settings);
/*settng the baud rate*/
//add error checking
ret1= cfsetispeed(&settings,B9600);
ret2=cfsetospeed(&settings,B9600);
if(ret1==-1 && ret2==-1)
{
perror("config failed for setting baud rate");
exit(0);
}
else printf("com port baud rate config successfull\n");
/*set local mode & enable reciever */
settings.c_cflag |= (CLOCAL|CREAD);
settings.c_cflag &= ~PARENB;
settings.c_cflag &= ~CSTOPB;
settings.c_cflag &= ~CSIZE;
settings.c_cflag |= CS8;
//add error checking
ret=tcsetattr(fd,TCSANOW,&settings);
if(ret==-1)
{
perror("config failed");
exit(0);
}
// else printf("com port config successfull\n");
} //End of port config
unsigned char *read_data(int fd) //defination of function read data
{
static int count,k=0;
//unsigned char array[255];
//unsigned char *ptr;
int nbytes,i,j=0;
ptr=array;
printf("I am in read data\n");
while((nbytes=read(fd,ptr,array+sizeof(array)-ptr-1))>0)
{
count++;
printf("Count is %d %d\n",count,j);
printf("i am in read function call and no of bytes read are %d\n",nbytes);
ptr+= nbytes;
if(ptr[-1]== '\n')//'|| ptr[-2]==0x13)
// printf("%s",array);
break;
}
printf("%s\n",array);
*ptr='\0';
return (&array[0]);
}//end of read_data
void receive_packets(int fd) //defination of port receive packets
{
unsigned char *str,*str1;
int i,j,k;
unsigned char FileName[255];
// unsigned char Data[10240];
int PacketCount=1,PacketNumber,LengthOfData;
int FileNameLength;
FILE *fp;
unsigned char ch;
static int t=1;
printf("I am in receive packets\n");
str=read_data(fd);
printf("the filename frame read is %s\n",str);
if(str[1]=='F') //Filename packet
{
printf("in if\n");
PacketCount=str[2]; //packet count
printf("packet count =%d\n",PacketCount);
FileNameLength=str[3]; //Length of the filename
printf("filelength =%d\n",FileNameLength);
for(i=0;i<FileNameLength;i++)
FileName[i]=str[i+4];
FileName[i]='\0';
k=0;
printf("File Name: %s\n",FileName);
}
//sleep(1);
//PacketCount=str[2];
for(i=0;i<PacketCount;i++)
{
printf("\ni love india:=>packetcount= %d\n",PacketCount);
printf("in for loop of data frame read for %d time\n",t++);
str=read_data(fd);
printf("the read string in for loop is %s\n",str);
if(str[1]=='D') //Data packet
{
printf("in if loop checking data packet\n");
PacketNumber=str[2];
LengthOfData=str[3];
for(j=0;j<LengthOfData;j++)
{
Data[k]=str[j+4];
k++;
}
}//end of data packet
}
Data[k]='\0';
printf("Data:%s",Data);
fp=fopen(FileName,"w+");
if(fp==NULL)
printf("Unable to open file\n");
else
{
printf("file opened successfully\n");
for(j=0;j<k;j++)
{
ch=Data[j];
fputc(ch,fp);
}
fclose(fp);
}
} //end of the receive_packet
void port_close(int fd)
{
close(fd);
} //end of the port_close
int main(void)
{
int fd;
char *a;
fd=port_open();
printf("fd returned in main is %d\n",fd);
port_config(fd);
receive_packets(fd);
port_close(fd);
//while(1);
return 0;
} //end of main