Linux - NewbieThis 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
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.
can any one explain me with example about the serial port programming in linux??
first i want to configure a port and thn write a file to port and thn again read the file from the port..please explain with example
can any one explain me with example about the serial port programming in linux??
first i want to configure a port and thn write a file to port and thn again read the file from the port..please explain with example
Spell out your words. And Google can explain it with an example, did you bother to look???
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Rep:
Hello vishnu,
First you have to open serial port.
The code to open serial port 1 on a PC running Linux:
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/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);
}
Then you write data to the port:
Code:
n = write(fd, "ATZ\r", 4);
if (n < 0)
fputs("write() of 4 bytes failed!\n", stderr);
The write function returns the number of bytes sent or -1 if an error occurred.
Then read data from the port and then close the port.
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420
Rep:
I'm attaching a Linux serial test program which requires the command parameters to be entered on the command line when the program is started. This program will send output typed on the computer keyboard after the program is started, through the serial port specified.
This program will allow a user to communicate from one computer to another using two serial lines. It is intended only as a test for the serial connection allowing any possible serial setting. This program is named com when compiled. To use it, the following is an example command line:
Quote:
com /dev/ttyS0 38400 8 1 0 4
This line works for com1 at 38400 baud, 8 data bits. I make it a file, call it "fcom1" and put it in my system path and invoke it as a script program.
Find the attachment.
Last edited by Satyaveer Arya; 12-20-2011 at 02:39 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.