LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 12-20-2011, 01:18 AM   #1
vishnu anand
LQ Newbie
 
Registered: Dec 2011
Posts: 12

Rep: Reputation: Disabled
Question serial port in linux


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
 
Old 12-20-2011, 08:46 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,632

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by vishnu anand View Post
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???

First hit in Google:
http://www.easysw.com/~mike/serial/serial.html
 
1 members found this post helpful.
Old 12-20-2011, 11:08 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,691

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
How to send/receive data via the serial port really depends on what is connected on the other end. We need more information.
 
1 members found this post helpful.
Old 12-20-2011, 02:23 PM   #4
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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.
 
1 members found this post helpful.
Old 12-20-2011, 02:33 PM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,420

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
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.
Attached Files
File Type: txt Serial Port Program.txt (12.9 KB, 49 views)

Last edited by Satyaveer Arya; 12-20-2011 at 02:39 PM.
 
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 linux alex4evr Linux - Hardware 2 07-10-2011 02:05 PM
Is a USBtty (USB serial port) treated the same as tty (normal serial port) in C? spudgunner Programming 1 11-12-2010 01:19 PM
linux serial port to router console port connection? frankie_fix Linux - General 3 02-26-2007 09:32 PM
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM
Installing Linux Over Serial Port NLess Linux - General 7 01-24-2003 02:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:01 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