Read/write serial port with Linux

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
I want to read data off a serial device on /dev/ttyS1. What I need to do
is send one ASCII character to the device, upon which it spits out 7
multi-character values, see below. The comm parameters are 1200Bd 8N1,
no flow control.
I have tried various approaches based on the serial programming HowTo
and a few code fragments written for serial multimeters found on the
net. However, I don't seem to be able to make any of this work.
I can get the expected result by using a terminal emulator, but what I
really want is a program or script that can do this.
This has been bugging me for weeks now, but I don't seem to be able to
make anything of the HowTo and progs.
Any advice on how to approach this or a link to a more in-depth description?

Michael


Sent: F
Rec'd: R:12,60;- 1,3;15;11,22;12,61;- 1,6;- 1,3;


Re: Read/write serial port with Linux
Quoted text here. Click to load it

Try initializing like this:

This structure was obtained by using the 'stty raw' command, and then
dumping the results.

struct termios tio_raw = {
/*c_iflag*/ 0x0,
/*c_oflag*/ 0x0,
/*c_cflag*/ 0x18b2,
/*c_lflag*/ 0x0,
/*c_line*/ 0x0,
  {
   /*c_cc[0]*/ 0x0,
   /*c_cc[1]*/ 0x0,
   /*c_cc[2]*/ 0x0,
   /*c_cc[3]*/ 0x0,
   /*c_cc[4]*/ 0x0,
   /*c_cc[5]*/ 0x0,
   /*c_cc[6]*/ 0x1,
   /*c_cc[7]*/ 0x0,
   /*c_cc[8]*/ 0x0,
   /*c_cc[9]*/ 0x0,
   /*c_cc[10]*/ 0x0,
   /*c_cc[11]*/ 0x0,
   /*c_cc[12]*/ 0x62,
   /*c_cc[13]*/ 0x61,
   /*c_cc[14]*/ 0x69,
   /*c_cc[15]*/ 0xe,
   /*c_cc[16]*/ 0x7b,
   /*c_cc[17]*/ 0x8,
   /*c_cc[18]*/ 0x40,
   /*c_cc[19]*/ 0x0,
   /*c_cc[20]*/ 0x0,
   /*c_cc[21]*/ 0x0,
   /*c_cc[22]*/ 0x0,
   /*c_cc[23]*/ 0x0,
   /*c_cc[24]*/ 0x0,
   /*c_cc[25]*/ 0x0,
   /*c_cc[26]*/ 0x0,
   /*c_cc[27]*/ 0x0,
   /*c_cc[28]*/ 0x0,
   /*c_cc[29]*/ 0x0,
   /*c_cc[30]*/ 0x0,
   /*c_cc[31]*/ 0x0,
  },
};

void Serial::init( ) {

    tio_raw.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
    tcsetattr( fd, TCSANOW, &tio_raw);

}

Re: Read/write serial port with Linux

I have found the serial howto difficult to work with.  There is another
serial programming guide, by a guy named Michael Sweet, and that guide
IMHO is far superior.  I did a search on google and found it at:

http://www.easysw.com/~mike/serial/index.html

HTH

Paul.

--
Remove _rem_ before replying by email.

On Wed, 05 May 2004 15:51:41 +0200, Michael Hofmann
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline