Solved

Serial Comm with C

Posted on 1997-08-09
1
443 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
1 Comment
 
LVL 2

Accepted Solution

by:
pxh earned 190 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
configuration management 2 100
GIF file bit manipulation for color change 5 104
repeatEnd java challenge 42 83
Problem to event 3 52
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

12 Experts available now in Live!

Get 1:1 Help Now