Solved

Serial Comm with C

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

688 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