Serial Port Access Denied using open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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?
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.
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
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)
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...
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.