LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2009, 10:46 AM   #1
morty346
Member
 
Registered: Feb 2009
Posts: 52

Rep: Reputation: 15
Exclamation Serial Port Access Denied using open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);


I am creating a program that opens a serial port for communication but it gets denied when I open it
I have installed a board that has two ports and have verified they are working by using the terminal:
dmesg | grep tty
Code:
console [tty0] enabled
0000:02:00.0: ttyS0 at I/O 0xdf00 (irq = 21) is a 16550A
0000:02:00.0: ttyS1 at I/O 0xde00 (irq = 21) is a 16550A
Code:
 int open_port(void)
    {
      int fd; /* File descriptor for the port */


      fd = open("/dev/ttyS0", 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);

      return (fd);
    }
Code:
open_port: Unable to open /dev/ttyS0 -: Permission denied
return code -1
Thank you
Greg
 
Old 02-10-2009, 11:04 AM   #2
morty346
Member
 
Registered: Feb 2009
Posts: 52

Original Poster
Rep: Reputation: 15
Solved!

chmod o+rw /dev/ttyS0
chmod o+rw /dev/ttyS1

Thank you anyways!
Greg
 
Old 12-27-2010, 02:41 PM   #3
mwasif
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Rep: Reputation: 0
Question

I also faces that problem.
when I write this...
[Shaheen@Shaheen ~]$ chmod o+rw/dev/ttyS0
It return this...
chmod: missing operand after `o+rw/dev/ttyS0'
Try `chmod --help' for more information.
how can I solve this problem?
 
Old 12-27-2010, 03:27 PM   #4
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
Permissions on serial ports have caused me headaches a few times, so if you reboot and the permissions are reset, you may need to modify the default permissions applied to serial ports by udev.
 
Old 12-27-2010, 03:52 PM   #5
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
Quote:
Originally Posted by mwasif View Post
Code:
chmod o+rw/dev/ttyS0
There should be a space between 'w' and '/'

You might also find that all tty devices have permissions 0660, and that by adding yourself to the group that owns the tty device you have permission to open it, rather than manually going in and altering permissions.

PS For future reference you should create a new thread, rather than resurrect a dead one
 
Old 12-28-2010, 11:04 AM   #6
redhatstand
Member
 
Registered: Jul 2006
Location: Oxford, UK
Distribution: CentOS, Ubuntu
Posts: 37

Rep: Reputation: 7
For posterity: this can also be caused by running xen virtualization: the host machine loses access to the first serial port because xen grabs it (for very sound reasons)

If you encounter this problem you can add a kernel option before the grub boot loader runs with defaults (hit escape when prompted)

google xencons=tty for more info.

Andy T
 
1 members found this post helpful.
Old 12-30-2010, 12:32 PM   #7
mwasif
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Rep: Reputation: 0
Thanks everybody
How can I set the properties (baud rate, parity bit etc)?
what are the default values of baud rate, parity bit and data bits?
 
Old 12-30-2010, 12:54 PM   #8
redhatstand
Member
 
Registered: Jul 2006
Location: Oxford, UK
Distribution: CentOS, Ubuntu
Posts: 37

Rep: Reputation: 7
you might find

man setserial

a useful manpage to try?

You could also scan the source code for the setserial command for pointers

If that doesn't help (and you don't get a better answer than this one) maybe start another thread asking how to get from knowing a manpage name to the source code for the command...

Regards,

Andy T
 
Old 01-01-2011, 02:05 PM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by mwasif View Post
Thanks everybody
How can I set the properties (baud rate, parity bit etc)?
what are the default values of baud rate, parity bit and data bits?
Are you trying to do that in some programming language, or by using existing tools? If the latter, then redhatstand has already pointed out the setserial command. All of the common tools for doing serial communications will have a facility to set comm's paramters.
To build such capability into you own programs, you would use some form of the termios API. You can read about the generalities of termios and it's C language interface via the termios manpage.

--- rod.
 
1 members found this post helpful.
Old 01-02-2011, 11:25 AM   #10
mwasif
LQ Newbie
 
Registered: Dec 2010
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Are you trying to do that in some programming language, or by using existing tools? If the latter, then redhatstand has already pointed out the setserial command. All of the common tools for doing serial communications will have a facility to set comm's paramters.
To build such capability into you own programs, you would use some form of the termios API. You can read about the generalities of termios and it's C language interface via the termios manpage.

--- rod.
I doing work on C code. My code is almost same as given above. I don't understand how to set the communicating properties (baudrate etc). So please tell me how I set the properties?

Thanks in advance.
 
Old 01-02-2011, 12:43 PM   #11
redhatstand
Member
 
Registered: Jul 2006
Location: Oxford, UK
Distribution: CentOS, Ubuntu
Posts: 37

Rep: Reputation: 7
I googled for

simple termios example

and got 14,600 results - you should find something useful in that lot...
 
Old 01-02-2011, 05:23 PM   #12
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
These should get you all of that, and more:

Serial-HOWTO
Serial-Programming-HOWTO
Serial Programming Guide for POSIX Operating Systems

--- rod.

Last edited by theNbomr; 01-04-2011 at 11:02 AM.
 
1 members found this post helpful.
  


Reply



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
Serial Port Access Denied using open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); morty346 Linux - Newbie 4 02-11-2009 08:13 AM
writing to serial port, /dev/ttyS0 togunter Programming 2 06-08-2008 09:27 AM
How to read serial port (/dev/ttyS0) seraph-seph Linux - Hardware 4 11-01-2006 06:55 AM
Serial Port HowTo (Cannot open /dev/ttyS0) chun-mee Linux - Hardware 2 07-14-2006 05:35 PM
I've got a message about "/dev/null: access denied" Saulo SUSE / openSUSE 2 04-14-2006 10:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08: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
Open Source Consulting | Domain Registration