Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-24-2004, 03:15 AM   #1
LQ Newbie
Registered: Aug 2004
Posts: 2

Rep: Reputation: 0
Serial / IOCTL


Hmm I've got a programm showing the status of my Raid-5 with a small device attached to the serial port.

Though serial support is in my kernel (when copying "garbage" to /dev/ttyS0" it "works") my program stopped working.

I'm using the RTS and DTR Lines (high and low) to signal the status of the raid with 2.4 and pre it worked without any problems but now

Maybe someone can help me how to set the RTS/DTR lines high and low with 2.6

Thnx :-) FGordon

This is my code doing the line switching and that worked with all kernels pre 2.6

int Ioctl(int fd, int flag, int *state)
int ret;
if ((ret=ioctl(fd, flag, state)) < 0) err_sys("ioctl() error");
return (ret);

/* Switch the LEDs on or off */
int setled(enum led whichled, int fd, int onoff, int *ledstate)
int bitpat;
int state;
int dummy;

if (whichled == green)
else if (whichled == red)
else bitpat=(TIOCM_RTS | TIOCM_DTR);

if (onoff) *ledstate |= bitpat;
else *ledstate &= ~bitpat;

dummy = Ioctl(fd, TIOCMSET, ledstate);
dummy = Ioctl(fd, TIOCMGET, &state);

return (*ledstate);
Old 08-24-2004, 06:18 AM   #2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
This is what works for me.
Works fine on 2.4 as well as 2.6 kernels.
/* Copyright (C) 2003 Heiko Noordhof <>
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>  
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

/* Function prototypes (belong in a seperate header file) */
int   openserial(char *devicename);
void  closeserial(void);
int   setDTR(unsigned short level);
int   setRTS(unsigned short level); 

/* Two globals for use by this module only */
static int fd;
static struct termios oldterminfo;

void closeserial(void)
     tcsetattr(fd, TCSANOW, &oldterminfo);

int openserial(char *devicename) 
     struct termios attr;

     if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */ 

     if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */
     attr = oldterminfo;
     attr.c_cflag |= CRTSCTS | CLOCAL;
     attr.c_oflag = 0;
     if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */
     if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */ 

     /* Set the lines to a known state, and */
     /* finally return non-zero is successful. */
     return setRTS(0) && setDTR(0);

/* For the two functions below:
 *     level=0 to set line to LOW
 *     level=1 to set line to HIGH

int setRTS(unsigned short level)
     int status;

     if (ioctl(fd, TIOCMGET, &status) == -1) {
	  perror("setRTS(): TIOCMGET");
	  return 0;
     if (level) status |= TIOCM_RTS;
     else status &= ~TIOCM_RTS;
     if (ioctl(fd, TIOCMSET, &status) == -1) {
	  perror("setRTS(): TIOCMSET");
	  return 0;
     return 1;

int setDTR(unsigned short level)
     int status;

     if (ioctl(fd, TIOCMGET, &status) == -1) {
	  perror("setDTR(): TIOCMGET");
	  return 0;
     if (level) status |= TIOCM_DTR;
     else status &= ~TIOCM_DTR;
     if (ioctl(fd, TIOCMSET, &status) == -1) {
	  perror("setDTR: TIOCMSET");
	  return 0;
     return 1;

Last edited by Hko; 08-24-2004 at 06:21 AM.
Old 08-24-2004, 08:51 AM   #3
LQ Newbie
Registered: Aug 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

Many Thanks :-)) :-)) this does work finally :-)


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
RH doesn't detect my Serial modem connected to a PCI Serial card ricky_ds Linux - Hardware 1 08-17-2006 02:37 PM
ioctl.h blackzone Programming 1 07-26-2004 03:30 AM
zoom external serial with usb to serial adapter foustware Linux - Hardware 1 12-07-2003 02:02 AM
ioctl dummyagain Programming 2 10-06-2003 07:22 AM
ioctl unosoft Linux - Software 0 09-12-2003 06:58 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:58 PM.

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
Open Source Consulting | Domain Registration