pgm name: open_port1.c
This pgm will work only in linux os
*/
#include
#include
#include
#include
#include
#include
/*
* * 'open_port()' - Open serial port 1.
* *
* * Returns the file descriptor on success or -1 on error.
* */
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, "open_port: Unable to open /dev/ttyS0 - %s\n",
strerror(errno));
}
return (fd);
}
main()
{
int mainfd=0; /* File descriptor */
char chout;
struct termios options;
mainfd = open_port();
fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */
/* Get the current options for the port */
tcgetattr(mainfd, &options);
cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
cfsetospeed(&options, B9600);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
/* Enable data to be processed as raw input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);
/* Set the new options for the port */
tcsetattr(mainfd, TCSANOW, &options);
while (1)
{
read(mainfd, &chout, sizeof(chout)); /* Read character from ABU */
if (chout != 0)
printf("Got %c.\n", chout);
chout=0;
usleep(20000);
}
/* Close the serial port */
close(mainfd);
}
// execute the commands
gcc open_port1.c -o open
./open
// program for writing data to the serial port
// pgm name: write_port.c
#include
#include
#include
#include
#include
#include
/*
* * 'open_port()' - Open serial port 1.
* *
* * Returns the file descriptor on success or -1 on error.
* */
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, "open_port: Unable to open /dev/ttyS1 - %s\n",
strerror(errno));
}
return (fd);
}
main()
{
int mainfd=0; /* File descriptor */
char chout;
struct termios options;
mainfd = open_port();
fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */
/* Get the current options for the port */
tcgetattr(mainfd, &options);
cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
cfsetospeed(&options, B9600);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8; /* Select 8 data bits */
options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */
/* Enable data to be processed as raw input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);
/* Set the new options for the port */
tcsetattr(mainfd, TCSANOW, &options);
write(mainfd, "manoj\r", 6 ) ; /* Close the serial port */
close(mainfd);
}
gcc write_port.c -o write
./write
Note: Run both the program in the different machine
2) Use serial port cable
3) use the command minicom -s
4) set the serial port setup
5) For any problem u can mail me manojlu03@yahoo.com
0 comments:
Post a Comment