LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 10-29-2013, 04:08 PM   #1
alirezan1
Member
 
Registered: Nov 2004
Location: Vancouver
Distribution: Ubunty, CentOS ,Mandriva, Gentoo, RedHat, Fedora, Knoppix
Posts: 147

Rep: Reputation: 15
serial port without tty?


Hello
I am a newbie to linux and drivers and I'm hoping I could get some help with a serial port question I have.

I have noticed that Linux installs TTY on all serial ports and USB ports, by default. This means evreything has to go through a TTY layer before it is passed through to the serial port. I have read a few tutorials and articles on that, and I think I understand that.

My question is, is there any possible way to not use TTY for serial port access? To send data in "RAW" mode?

The reason for this question is, if I understand the concept of TTY correctly, TTY requires a carriage return newline (\r\n) to send the data through to the serial port. Without the newline the data won't be transmitted.

This poses a major headache for my existing programs I am trying to port to Linux from other OS's. I have external devices that write to serial port without issuing newline. If the data is not actually sent out some of my existing codes will not function properly.

Can anybody help me out with understanding this and how I can actually work around this please?

Thanks
 
Old 10-29-2013, 04:55 PM   #2
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,212

Rep: Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140Reputation: 1140
No reason you need a carriage return to send data. Just use cfmakeraw and set the flags for raw I/O in your program (assuming it's C?)

Here's a program that reads raw binary from the serial port and dumps it to a file, no carriage returns or newlines anywhere in what's being sent:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>

//#define BREAK_SIZE 0xA00000 // Split to a new file every 10 MiB
#define BREAK_SIZE 0x100000 // Split to a new file every 1 MiB

int main ()
{
   int sfd, ofd;
   int n,n2;
   int bytes;
   int incr = 0;
   int bytesum = 0;
   char filename[255];

   struct termios options;
   char buff[10000];

   sfd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
   if (sfd == -1)
   {
      fprintf(stderr, "unable to open serial port\n");
      exit(1);
   }
   else
   {
      fcntl(sfd, F_SETFL, FNDELAY);
   }

   tcgetattr(sfd, &options);

   cfsetispeed(&options, B115200);
   cfsetospeed(&options, B115200);

   cfmakeraw(&options);

   options.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
   options.c_cflag |= (CLOCAL | CREAD | CS8);

   options.c_oflag &= ~OPOST; // select raw output
   options.c_cc[VMIN] = 1;
   options.c_cc[VTIME] = 0;

   tcsetattr(sfd, TCSANOW, &options);

   n2 = sprintf(filename, "/data/data_%06d.dat", incr);

   ofd = open(filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
   if (ofd == -1)
   {
      fprintf(stderr, "unable to open output file\n");
      exit(2);
   }

   while (1)
   {
      ioctl(sfd, FIONREAD, &bytes);
      if (bytes > 0)
      {
         n = read(sfd, buff, 10000);
         //fprintf(stderr, "available/read: %d/%d\n",bytes,n);
         if (n < 0)
         {
            fprintf(stderr, "read failed\n");
         }
         if (n > 0)
         {
            if (bytesum > BREAK_SIZE)
            {
               close(ofd);
               incr++;
               n2 = sprintf(filename, "/data/data_%06d.dat", incr);

               ofd = open(filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
               if (ofd == -1)
               {
                  fprintf(stderr, "unable to open output file\n");
                  exit(2);
               }
               bytesum = 0;
            }

            write(ofd, buff, n);
            bytesum += n;
         }
      }
      usleep(1000);
   }
}

Last edited by suicidaleggroll; 10-29-2013 at 04:56 PM.
 
1 members found this post helpful.
Old 10-29-2013, 05:04 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,152

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
sfd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
When the O_NOCTTY flag is used it will not become the process's controlling terminal even if the process does not have one. As stated using CR and/or LF is not required to send serial data.

You didn't say how you were porting your application but I would not use bash script.

Last edited by michaelk; 10-29-2013 at 05:22 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using serial port (tty) even if /dev/ppp driver use it ? Vincent_ Linux - Networking 2 09-22-2011 03:35 AM
Is a USBtty (USB serial port) treated the same as tty (normal serial port) in C? spudgunner Programming 1 11-12-2010 02:19 PM
Help: tty console to serial port Rostfrei Linux - General 1 11-03-2010 05:05 AM
Porting Linux, the Serial Port, and TTY Interloper Linux - Software 2 06-25-2009 04:53 AM
TTy driver how to use underlying serial port vicky_driver Linux - Hardware 0 12-26-2008 06:48 AM


All times are GMT -5. The time now is 03:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration