Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Functions to connect, send and receive data in serial port linux ! 1

Status
Not open for further replies.

shalon

Programmer
Mar 31, 2001
3
BR
I need simple functions to connect in
ttyS1 (com2) and send/receive data by
serial port of linux !
 
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

/*
* '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(&quot;/dev/ttyS1&quot;, O_RDWR | O_NOCTTY | O_NDELAY);

if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, &quot;open_port: Unable to open /dev/ttyS1 - %s\n&quot;,
strerror(errno));
}

return (fd);
}

void 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(&quot;Got %c.\n&quot;, chout);

chout=0;
usleep(20000);
}
/* Close the serial port */
close(mainfd);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top