Solved

accessing the comports using c

Posted on 2002-04-18
4
208 Views
Last Modified: 2010-04-20
I need help on how to access the com ports in LINUX using C. What I need to do is open and send/recive data from com1 or 2. How do I do this???
0
Comment
Question by:lulle
4 Comments
 
LVL 4

Expert Comment

by:MFCRich
ID: 6951679
On Linux typically com1 is "/dev/ttyS0" and com2 is "/dev/ttyS1". This may change if you have a non-typical hardware setup.
0
 
LVL 2

Expert Comment

by:hindmost
ID: 6959951
A good example of the basics of accessing and controlling a modem.

/*
 *  AUTHOR: Sven Goldt (goldt@math.tu-berlin.de)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
*/
/*
 This is like all programs in the Linux Programmer's Guide meant
 as a simple practical demonstration.
 It can be used as a base for a real terminal program.
*/

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/modem"
#define ENDMINITERM 2 /* ctrl-b to quit miniterm */

#define _POSIX_SOURCE 1 /* POSIX compliant source */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

void child_handler(int s)
{
   STOP=TRUE;
}

main()
{
int fd,c;
struct termios oldtio,newtio,oldstdtio,newstdtio;
struct sigaction sa;
 
/*
  Open modem device for reading and writing and not as controlling tty
  because we don't want to get killed if linenoise sends CTRL-C.
*/
 fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
 if (fd <0) {perror(MODEMDEVICE); exit(-1); }
 
 tcgetattr(fd,&oldtio); /* save current modem settings */
 
/*
  Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
  Also don't hangup automatically and ignore modem status.
  Finally enable receiving characters.
*/
 newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
 
/*
 Ignore bytes with parity errors and make terminal raw and dumb.
*/
 newtio.c_iflag = IGNPAR;
 
/*
 Raw output.
*/
 newtio.c_oflag = 0;
 
/*
 Don't echo characters because if you connect to a host it or your
 modem will echo characters for you. Don't generate signals.
*/
 newtio.c_lflag = 0;
 
/* blocking read until 1 char arrives */
 newtio.c_cc[VMIN]=1;
 newtio.c_cc[VTIME]=0;
 
/* now clean the modem line and activate the settings for modem */
 tcflush(fd, TCIFLUSH);
 tcsetattr(fd,TCSANOW,&newtio);

/*
  Strange, but if you uncomment this command miniterm will not work
  even if you stop canonical mode for stdout. This is a linux bug.
*/
 tcsetattr(1,TCSANOW,&newtio); /* stdout settings like modem settings */
 
/* next stop echo and buffering for stdin */
 tcgetattr(0,&oldstdtio);
 tcgetattr(0,&newstdtio); /* get working stdtio */
 newstdtio.c_lflag &= ~(ICANON | ECHO);
 tcsetattr(0,TCSANOW,&newstdtio);

/* terminal settings done, now handle in/ouput */
 switch (fork())
 {
  case 0: /* child */
   /* user input */
   close(1); /* stdout not needed */
   for (c=getchar(); c!= ENDMINITERM ; c=getchar()) write(fd,&c,1);
   tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */
   tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */
   close(fd);
   exit(0); /* will send a SIGCHLD to the parent */
   break;
  case -1:
   perror("fork");
   tcsetattr(fd,TCSANOW,&oldtio);
   close(fd);
   exit(-1);
  default: /* parent */
   close(0); /* stdin not needed */
   sa.sa_handler = child_handler;
   sa.sa_flags = 0;
   sigaction(SIGCHLD,&sa,NULL); /* handle dying child */
   while (STOP==FALSE) /* modem input handler */
   {
    read(fd,&c,1); /* modem */
    write(1,&c,1); /* stdout */
   }
   wait(NULL); /* wait for child to die or it will become a zombie */
   break;
 }
}
0
 
LVL 8

Accepted Solution

by:
Anthony2000 earned 300 total points
ID: 6960353
Here are some more examples for different situations.
http://new.linuxnow.com/docs/content/Serial-Programming-HOWTO-html/Serial-Programming-HOWTO-3.html

Also, if you perform a search (using www.google.com for example) for "linux serial programming" and "howto" you can get additional information about programming serial ports in general.

Anthony.
0
 

Expert Comment

by:CleanupPing
ID: 9089086
lulle:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
File and print server with Ubuntu? 7 42
Like to learn Linux, need advice for a good book and setup. 3 44
centos linux 65 123
Securing Azure Oracle instance of Linux VM 4 56
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now