Saturday, April 20, 2013

C program for serial communication between 2 pcs using null modem connection using RS232


//C program for serial communication between 2 pcs using null modem connection using RS232

#include <bios.h>
#include <conio.h>
#define COM1       0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 | 0x08 | 0x00 | 0x03)
int main(void)
{
   
int in, out, status;
   bioscom(
0, SETTINGS, COM1); /*initialize the port*/
   cprintf(
"Data sent to you: \n ");
   
while (1)
   {
      status = bioscom(
30, COM1); /*wait until get a data*/
      
if (status & DATA_READY)
           if ((out = bioscom(
20, COM1) & 0x7F) != 0)  /*input a data*/
              putch(out);
           if (kbhit())
           {
              
if ((in = getch()) == 27)   /* ASCII of Esc*/
                 
break;
              bioscom(
1, in, COM1);   /*output a data*/
           }
   }
   
return 0;
}


c code to generate pascal triangle


// c code to generate pascal triangle
#include<stdio.h>
#include<conio.h>
int main(){
clrscr();
int r,row,s,n,binom;
printf("enter number of rows in pascal triangle:\n");
scanf("%d",&row);
for(r=0;r<row;r++){
for(s=40-3*r;s>0;s--)
printf(" ");
for(n=0;n<=r;n++){
if(n==0 || r==n)
binom=1;
else
binom=binom*(r-n+1)/n;
printf("%6d",binom);
}
printf("\n");
}
getch();
return 0;
}

c code to find the vallue of sine and cosine


//c code to find the vallue of sine and cosine
#include<stdio.h>
#include<conio.h>
#define pi 3.141593
int main(){
clrscr();
int i,n=15;
float x,sine,cosine,sine_term,cosine_term;
printf("enter angle in degree:\n");
scanf("%f",&x);
x*=pi/180;
sine=sine_term=x;
cosine=cosine_term=1;
for(i=1;i<n;i++){
sine_term*=(-1)*x*x/(2*i)/(2*i+1);
sine+=sine_term;
cosine_term*=(-1)*x*x/(2*i-1)/(2*i);
cosine+=cosine_term;
}
printf("sin(x)=%f\n",sine);
printf("cosine(x)=%f\n",cosine);
getch();
return 0;
}

c code to convert a number in base 10 to others(2-16)


//c code to convert a number in base 10 to others(2-16)
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main(){
clrscr();
int base,number,i=0,j;
static char digit[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int converted_num[8];
printf("enter a number to be converted and base (2 to 16)\n");
scanf("%d%d",&number,&base);
for(j=number;j>0;j/=base,i++){
converted_num[i]=j%base;
       // i++;
}
printf("%d in base %d is ",number,base);
for(--i;i>=0;--i){
j=converted_num[i];
printf("%c",digit[j]);
}
getch();
return 0;
}

c code to convert a number in base(2-9) to base 10



//c code to convert a number in base(2-9) to base 10
#include<stdio.h>
#include<math.h>
#include<conio.h>
int main(){
clrscr();
int base,number,i=0,j,rem,temp;
long sum=0;
printf("enter a number and it's base(2 to 9)\n");
scanf("%d%d",&number,&base);
for(j=number;j>0;j/=10){
rem=j%10;
sum+=rem*pow(base,i);
i++;
}
printf("%d in base 10 is %ld",number,sum);
getch();
return 0;
}