Re: Reading from serial devices...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 3/13/06, Matthew Percival <matthew@xxxxxxxxx> wrote:
> G'Day,
>
> > The problem is an issue of the serial port being in "cooked" mode -
> > the driver doesn't deliver the characters in the buttern until either
> > a carriage return or a line feed (I forget which one) is received.
> > There is a way to put the driver into "raw" mode, which delivers every
> > single character immediately. However, I don't have any example code
> > in front of me, and the only example code that I have is in C. I can
> > forward you the example code a little later today, if you are
> > intereseted. I'm surry there's a way to do it via command-line/script,
> > I just don't know it off the top of my head.
>
>         That seems to fit the behaviour I have noticed.  If you could send me
> that C code, it would be useful.  I am working with Lua, so more than
> likely whatever needs to be done is not possible in the language, but
> with example C code I could add the functionality.  At the very least,
> it would help me to understand what needs to be done.
>


Basic skeleton code as follows:


#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>

long BAUD = B115200;
long DATABITS = CS8;
long STOPBITS = 0;
long PARITYON = 0;
long PARITY = 0;

int keyenceCommFD;
struct termios oldtio, newtio;

pid_t myPid;

int makeSerportRaw(void)
{
  keyenceCommFD = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (keyenceCommFD < 0)
  {
    return EXIT_FAILURE;
  }

  myPid = getpid();

  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
  newtio.c_cflag = BAUD | DATABITS | STOPBITS | PARITYON | PARITY |
CLOCAL | CREAD;
  newtio.c_cc[VMIN] = 1;
  newtio.c_cc[VTIME] = 0;

  tcgetattr(keyenceCommFD, &oldtio);


  fcntl(keyenceCommFD, F_SETOWN, myPid);
  fcntl(keyenceCommFD, F_SETFL, FASYNC);

  tcsetattr(keyenceCommFD, TCSANOW, &newtio);
  return EXIT_SUCCESS;
}


int restoreSerport(void)
{
  tcsetattr(keyenceCommFD, TCSANOW, &oldtio);
  return EXIT_SUCCESS;
}


The important thing in the code is that the newtio.c_lflag does NOT
contain ICANON. You can get more info about all the termios-related
flags via man termios.

HTH-

James


>         Thanks,
>
>         Matthew
>
>
> --
> Kernelnewbies: Help each other learn about the Linux kernel.
> Archive:       http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
>
>

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux