LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-24-2005, 09:27 AM   #1
pappsynz
LQ Newbie
 
Registered: Apr 2005
Location: Oxford, UK
Distribution: Red Hat
Posts: 19

Rep: Reputation: 0
Unhappy manually controlling RTS/CTS


Hi there,

I'm trying to write a piece of software to test a UART driver and chip that I had been playing with. The chip is on a PCI card, and I have a cross over cable between the serial port on the card to the 'COM0' serial port on the motherboard. I'm wanting to test the Hardware Flow Control, whilst having the current setup and so am hoping to be able to set RTS through command line calls using maybe stty?

On Unix I have found that stty has some hardware flow control modes such as [-]rtsxoff and [-]ctsxon... however Linux doesn't look to have these. Is there any equivalent??

I'm using Linux 2.6.10, and a Red Hat distro. Any tips would be much appreciated, even if it means a change of tack.

Thanks
Ben
 
Old 05-24-2005, 10:37 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Re: manually controlling RTS/CTS

Quote:
Originally posted by pappsynz
On Unix I have found that stty has some hardware flow control modes such as [-]rtsxoff and [-]ctsxon... however Linux doesn't look to have these. Is there any equivalent??
GNU/Linux's stty does have these:
  • `crtscts'
    Enable RTS/CTS flow control. Non-POSIX. May be negated.
  • `ixon'
    Enable XON/XOFF flow control (that is, `CTRL-S'/`CTRL-Q'). May be
    negated.

Is this what you're looking for?

See "info coreutils stty" for more.
 
Old 05-24-2005, 10:50 AM   #3
pappsynz
LQ Newbie
 
Registered: Apr 2005
Location: Oxford, UK
Distribution: Red Hat
Posts: 19

Original Poster
Rep: Reputation: 0
Hey there ;-)

Actually I've already got this set, however as far as I can tell this (crtscts) only turns on the hardware flow control, I cant use it to stop and start data flow manually.

As I cannot set up a test network to try and force the hardware flow control to have to toggle RTS etc..., I am hoping to be able to write a few lines of code that will set RTS to 0, pause and then reset RTS to 1 as a way of forcing the driver for the other UART to use its flow control mechanism.
 
Old 05-24-2005, 11:26 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Quote:
Originally posted by pappsynz
I am hoping to be able to write a few lines of code that will set RTS to 0, pause and then reset RTS to 1 [...]
I still had some code around that's is able to do that.
I changed it to do what you described above.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


static struct termios oldterminfo;


void closeserial(int fd)
{
    tcsetattr(fd, TCSANOW, &oldterminfo);
    if (close(fd) < 0)
        perror("closeserial()");
}


int openserial(char *devicename)
{
    int fd;
    struct termios attr;

    if ((fd = open(devicename, O_RDWR)) == -1) {
        perror("openserial(): open()");
        return 0;
    }
    if (tcgetattr(fd, &oldterminfo) == -1) {
        perror("openserial(): tcgetattr()");
        return 0;
    }
    attr = oldterminfo;
    attr.c_cflag |= CRTSCTS | CLOCAL;
    attr.c_oflag = 0;
    if (tcflush(fd, TCIOFLUSH) == -1) {
        perror("openserial(): tcflush()");
        return 0;
    }
    if (tcsetattr(fd, TCSANOW, &attr) == -1) {
        perror("initserial(): tcsetattr()");
        return 0;
    }
    return fd;
}


int setRTS(int fd, int 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 main()
{
    int fd;
    char *serialdev = "/dev/ttyS0";

    fd = openserial(serialdev);
    if (!fd) {
        fprintf(stderr, "Error while initializing %s.\n", serialdev);
        return 1;
    }

    setRTS(fd, 0);
    sleep(1);       /* pause 1 second */
    setRTS(fd, 1);

    closeserial(fd);
    return 0;
}
If you saved the code above as "bumpRTS.c" you can compile it the easiest way by entering:
Code:
make bumpRTS
or, more sophisticated, with:
Code:
gcc -Wall -pedantic -O2 -o bumpRTS bumpRTS.c
Either way, the executable will be stored as "bumpRTS.c" in the same directory.
Hope this does what you need.

Last edited by Hko; 05-24-2005 at 11:27 AM.
 
Old 05-25-2005, 04:55 AM   #5
pappsynz
LQ Newbie
 
Registered: Apr 2005
Location: Oxford, UK
Distribution: Red Hat
Posts: 19

Original Poster
Rep: Reputation: 0
Hi Hko,

Thanks a million for the help! The ioctl is exactly what I was looking for.

Have made the necessary adjustments and additions to my code and it great.

Thanks again,

pappsynz
 
  


Reply


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 poll() for CTS-signal? ecsed Linux - Software 1 07-20-2005 08:23 PM
manually controlling cpu speed (not overclocking... well maybe, im not sure) poiuytrewq Linux - General 3 05-14-2005 05:14 AM
Need help to read a signal from the CTS pin of a serial port juan_de_margo Linux - Hardware 0 02-24-2005 03:36 AM
RTS Games linuxgamer09483 Linux - Games 1 02-09-2004 06:19 AM
Controlling serial port RTS pin from 'C' program dcarter Slackware 1 09-26-2003 07:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:21 PM.

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