Solved

Serial Comm with C

Posted on 1997-08-09
1
453 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 95
Named range not carried over 10 66
lucky13 challenge 11 159
Use of condition with 'serial' in ansible 2 89
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

696 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