?
Solved

Serial Comm with C

Posted on 1997-08-09
1
Medium Priority
?
468 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Loops Section Overview
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

809 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