Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-30-2003, 07:25 AM   #1
LQ Newbie
Registered: Oct 2003
Posts: 1

Rep: Reputation: 0
data transfer bet RH8.0 pc to & fro device thru /dev/ttyS0

i need to issue a command which is of ~20 hex bytes to a device connected to /dev/ttyS0 and in turn capture the ~24 hex bytes of works fine under cygwin and dos.i feel that i ve not done the settings properly. i referred the serial prg howto. but unable to resolve the write operation thru /dev/ttyS0.

i have the same problem what togunter faced long back,
he had posted a message to the same forum with thread (below)
"writing to serial port, /dev/ttyS0"
- but there are no replies.

awaiting ur help
thanks a lot

Registered: Apr 2002
Location: Los Angeles
Distribution: Red Hat 7.0 & 7.2
Posts: 4

Hi all-
I'm trying to write some data to a device on the com1 port on my Redhat 7.2 box, but when I write data to /dev/ttyS0, the characters I write appear to get garbled(and i dont even seem to get the same number of characters).
The same code works fine under windows using cygwin to write to /dev/com1 (after using stty to determine that the serial port is setup the same as in linux).

the code i am using is below.

any ideas?
thanks for any help

// begin code:

#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int fd;
struct termios options;

int i ;
unsigned char data[20] = { 0x40, 0x28, 0x6b, 0xfe,
0x00, 0x00, 0x00, 0x00,
0x00, 0x23, 0x04, 0x00,
0x00, 0x00, 0x00, 0x00,
0xd9, 0xff, 0xff, 0xff} ;

// linux
//fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);

// cygwin
fd = open("/dev/com1", O_RDWR | O_NOCTTY | O_NONBLOCK);
fcntl(fd, F_SETFL, 0);

/* get the current options */
tcgetattr(fd, &options);

cfsetispeed(&options, B57600);
cfsetospeed(&options, B57600);

/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;

options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

options.c_cflag &= ~CRTSCTS;

// options.c_iflag &= ~(IXON | IXOFF | IXANY); /* no flow control */

options.c_oflag &= ~(IXON | IXOFF | IXANY); /* no flow control */

options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST; /* No output processing */
options.c_oflag &= ~ONLCR; /* Don't convert linefeeds */

/* Miscellaneous stuff */
options.c_cflag |= (CLOCAL | CREAD); /* Enable receiver, set local */
/* Linux seems to have problem with the following ??!! */
options.c_cflag |= (IXON | IXOFF); /* Software flow control */
options.c_lflag = 0; /* no local flags */
options.c_cflag |= HUPCL; /* Drop DTR on close */

/* Setup non blocking, return on 1 character */
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 1;

/* Clear the line */

/* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0) {
perror("SetupSerial 3");

write(fd, data, 20 ) ;
return 0 ;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to fix the /dev/ttyS0 device? treotan Linux - Hardware 1 07-10-2005 01:59 AM
reading & interpreting data from /dev/gpmdata Zither Programming 1 11-02-2003 07:08 AM
New Hard drive & transfer data wanna lrn linux Linux - Newbie 1 09-21-2002 04:49 PM
chmod g+w /dev/ttyS0 jISV Linux - General 4 04-13-2002 06:01 PM
/dev/ttyS0 problems rbermude Linux - Software 2 06-27-2001 10:43 AM

All times are GMT -5. The time now is 08:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration