GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Dec 21, 2014 9:34 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Simple C read serial port sample
PostPosted: Tue Aug 27, 2013 6:23 am 
Offline
GTK+ Geek

Joined: Wed Mar 18, 2009 2:26 pm
Posts: 62
I thought this might be helpful to someone on this forum if they were looking for some code to read data from a serial port.
Nothing to do directly with GTK though.
This code was built to read from a USB serial interface device and can be
Code:
#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("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
   if (fd == -1)
   {
      /* Could not open the port.*/
      perror("open_port: Unable to open /dev/ttyS0 - ");
   }
   else
//   fcntl(fd, F_SETFL, 0); /* Blocking READ waits for data in the buffer */
   fcntl(fd, F_SETFL, FNDELAY); /* return whatever is there in the buffer */

   return (fd);
}

// n = write(fd, "ATZ\r", 4);
// if (n < 0)
// fputs("write() of 4 bytes failed!\n", stderr);

int main(int argc, char *argv[])
{
   int fd = open_port();
   struct termios options;
   /*
   * Get the current options for the port...
   */
   tcgetattr(fd, &options);
   /*
   * Set the baud rates to B9600...
   */
   cfsetispeed(&options, B9600);
   cfsetospeed(&options, B9600);
   /*
   * Enable the receiver and set local mode...
   */
   options.c_cflag |= (CLOCAL | CREAD);
   /*
   * Set the new options for the port...
   */
   tcsetattr(fd, TCSANOW, &options);
   
   /* set to 8N1 mode */
   options.c_cflag &= ~PARENB;
   options.c_cflag &= ~CSTOPB;
   options.c_cflag &= ~CSIZE; /* Mask the character size bits */
   options.c_cflag |= CS8; /* Select 8 data bits */
//    options.c_cflag &= ~CNEW_RTSCTS;

   /* Canonical input is line-oriented. Input characters are put into a buffer
    * which can be edited interactively by the user until a
    * CR (carriage return) or LF (line feed) character is received.
    */
   options.c_lflag |= (ICANON | ECHO | ECHOE);
   /* Raw input is unprocessed. Input characters are passed through exactly as
    * they are received, when they are received. Generally you'll deselect the
    * ICANON, ECHO, ECHOE, and ISIG options when using raw input:
    */
   //options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
   
   /*Software flow control is enabled using the IXON, IXOFF, and
    * IXANY constants:
    */
   options.c_iflag |= (IXON | IXOFF | IXANY);
   /* To disable software flow control simply mask those bits:*/
   //options.c_iflag &= ~(IXON | IXOFF | IXANY);

   /* Processed output is selected by setting the OPOST option
    * in the c_oflag member:
    */
   options.c_oflag |= OPOST;
   /* Raw output is selected by resetting the OPOST option
    * in the c_oflag member:
    */
   //options.c_oflag &= ~OPOST;
   char b[1];
   for(;;)
   {
      int n = read(fd, b, 1);
      if(n > -1)
      {
         printf("%x\n", b[0]);
      }
   }
   close(fd);
   return 0;
}

_________________
Hello, העלו, Здравствулте!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group