?
Solved

Serial Comm with C

Posted on 1997-08-09
1
Medium Priority
?
464 Views
Last Modified: 2013-12-26
Hardware: Sun UltraSparc2 running Solaris 2.5.1
I've got the Sun connected to a DOS computer via serial cable.  I need to know how to use termios in C (on the Sun) to communicate with the serial port so I can
1. Read incoming information - it will be in the format similiar to:
  34.56 -23.45 56.76 \n
and store each float in a variable.
2. Be able to write to the serial port as well. To tell the DOS program running on the other end to send the next line of information as above. (This is done by sending a 'f\n' to the DOS computer.)
It is necessary NOT to use any buffers because the information needs to be read in real time and used in the rest of my program on the Sun.
I've read the termios man pages on termios and I'm lost. Code on how to do this is would be greatly appreciated.
0
Comment
Question by:rodden
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
pxh earned 760 total points
ID: 1294203
Here is code, which I used to coounicate on a serial port. I also have Solaris 2.5.1, I think it's not relevant that my application runs on a SPARC 10.

I don't understand, why you are concerned about buffering. Your data are always ending with "\n", I think that flushes the buffer. Anyhow, the line

NewT.c_cc[VMIN]  = 1;            /* read single characters */

should take care of it.

I hope this code helps. To really read you can loop over "read(IOdes, buf, 1);", where "buf" is a character string to store the incoming data, until "\n" is encountered. With
"sscanf(buf, "%f %f %f", &f1, &f2, &f3);" you can store the numbers as floats.

Peter (pxh@mpe-garching.mpg.de)

code follows:

....
/* declarations */
static char      IOdev[] = "/dev/ttya";
static FILE      *IOfile = NULL;
static int      IOdes = -1;
static int      UslVal = 0;
static speed_t      IOspeed = B9600;
struct termio      OldT = {0}, NewT;
....

/* open serial port read/write */
if ( (IOdes = open(IOdev, O_RDWR | O_NOCTTY)) == -1 )
      {
      fprintf(stderr, "Error opening %s for read\7\n", IOdev);
      return(-1);
      }

/* call routine, which sets up serial communication */
tty_raw();
....

/**********************************************************************/

int tty_raw()
{
struct termios            termiosS = {0};
struct termios            *termiosPtr = &termiosS;

if (ioctl(IOdes, TCGETA, &OldT) == -1)
      return(-1);

NewT = OldT;
NewT.c_lflag &= ~ICANON;      /* raw-mode */
NewT.c_lflag &= ~ECHO;            /* Echo off */
NewT.c_lflag &= ~ISIG;            /* no signals checking */

NewT.c_iflag &= ~ISTRIP;      /* do not strip bit 8 */
NewT.c_iflag &= ~ICRNL;            /* don't map CR to NL */
NewT.c_iflag &= ~INPCK;            /* disable parity check */
NewT.c_iflag &= ~IUCLC;            /* don't map upper to lower case */
NewT.c_iflag |= IXON | IXOFF;      /* enable software handskake */

NewT.c_oflag &= ~OPOST;            /* don't postprocess */
NewT.c_oflag &= ~OLCUC;            /* don't map lower to upper case */


NewT.c_cc[VMIN]  = 1;            /* read single characters */
NewT.c_cc[VTIME] = 30;            /* set timeout in units of 1/10s */

NewT.c_cflag &= ~(CSTOPB | PARENB | CLOCAL |CIBAUD | CRTSCTS);
NewT.c_cflag |= CLOCAL | IOspeed | CS8 | CREAD ;


if (ioctl(IOdes, TCSETA, &NewT) == -1) return(-1);

termiosPtr->c_cflag = NewT.c_cflag;

/* set to stopbit=1, parity=NO, bit pro char=8, speed= xxxx baud */
termiosPtr->c_cflag &= ~(CSTOPB | PARENB | CLOCAL | CIBAUD | CRTSCTS);
termiosPtr->c_cflag |= CLOCAL | IOspeed | CS8 | CREAD;

termiosPtr->c_lflag = NewT.c_lflag;
termiosPtr->c_iflag = NewT.c_iflag;
termiosPtr->c_oflag = NewT.c_oflag;
termiosPtr->c_cc[VMIN] = NewT.c_cc[VMIN];
termiosPtr->c_cc[VTIME] = NewT.c_cc[VTIME];

if (tcsetattr(IOdes, TCSANOW, termiosPtr))
      {
      fprintf(stderr, "tty_raw: couldn't set attributes\n");
      return(-1);
      }
}


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

770 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