Re: Regarding USB-to-serial device driver

From: Raju Rameshwar Uprade
Date: Mon Oct 04 2010 - 00:34:10 EST



Hi Greg,

> > Is it possible to control RTS line from user space.
>
> Yes.

It's possible to control RTS line for standard UART serial port like ttyS0 and ttyS1
from userspace.

But for USB-to-serial port like ttyUSB0, the program is not able to control RTS line
from userspace.

I have attached the userspace program for enable/disable of RTS line.

Thanks-

Raj
#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/ttyUSB0";

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

setRTS(fd,1);
usleep(1);
setRTS(fd, 0);

closeserial(fd);
return 0;
}