Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 main()
- {
- int fd; // File descriptor
- int n;
- char buf;
- fd = open_port();
- // Read the configureation of the port
- struct termios options;
- tcgetattr( fd, &options );
- /* SEt Baud Rate */
- //cfsetispeed( &options, 2764800 );
- //cfsetospeed( &options, 2764800 );
- cfsetispeed( &options, B9600 );
- cfsetospeed( &options, B9600 );
- //I don't know what this is exactly
- options.c_cflag |= ( CLOCAL | CREAD );
- // Set the Charactor size
- options.c_cflag &= ~CSIZE; /* Mask the character size bits */
- options.c_cflag |= CS8; /* Select 8 data bits */
- // Set parity - No Parity (8N1)
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- //options.c_lflag |= CRTSCTS;
- // Disable Hardware flowcontrol
- // options.c_cflag &= ~CNEW_RTSCTS; -- not supported
- // Enable Raw Input
- options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- // Disable Software Flow control
- options.c_iflag &= ~(IXON | IXOFF | IXANY);
- // Chose raw (not processed) output
- options.c_oflag &= ~OPOST;
- /* set input mode (non-canonical, no echo,...) */
- //options.c_lflag = 0;
- options.c_cc[VTIME] = 0; /* inter-character timer unused */
- options.c_cc[VMIN] = 1; /* blocking read until 1 chars received */
- if ( tcsetattr( fd, TCSANOW, &options ) == -1 )
- printf ("Error with tcsetattr = %sn", strerror ( errno ) );
- else
- printf ( "%sn", "tcsetattr succeed" );
- fcntl(fd, F_SETFL, FNDELAY);
- // Write some stuff !!!
- n = write(fd, "ATZATZr", 7);
- if (n < 0)
- fputs("write() of 7 bytes failed!n", stderr);
- else
- printf ("Write succeed n = %in", n );
- /*for ( n = 0 ; n < 1000 ; n++)
- {
- n++;
- n--;
- }
- */
- sleep(12);
- n = read( fd, &buf, 1 );
- if ( n == -1 )
- printf ( "Error = %sn", strerror( errno ) );
- printf ( "Number of bytes to be read = %in", n );
- printf ( "Buf = %cn", buf );
- close( fd );
- return(0);
- }
- int open_port(void)
- {
- int fd; /* File descriptor for the port */
- fd = open("/dev/ttyXR0", O_RDWR | O_NOCTTY | O_NDELAY);
- if (fd == -1)
- {
- /*
- * Could not open the port.
- */
- perror("open_port: Unable to open /dev/ttyXR0 - ");
- }
- else
- {
- fcntl(fd, F_SETFL, FNDELAY);
- printf("opening of device /dev/ttyXR0 succeeded !!n");
- }
- printf ( "In Open port fd = %in", fd);
- return (fd);
- }
Add Comment
Please, Sign In to add comment