6

I am trying to fetch data from the USB device (say pendrive) connected to the USB port of a system. Here, I am able to open the device file and read some random raw data. But I want to fetch data like minicom/teraterm.

Please let me know what methods and libraries I can use to do it successfully and how can it be done.

#include <stdio.h> 
#include <stdio.h>   
#include <string.h>  
#include <unistd.h>  
#include <fcntl.h>   
#include <errno.h>   
#include <termios.h> 
#include <signal.h>
#include <sys/time.h>

int main()
{
    short portfd=-1;
    int n,f,len;
    char buf[256],*s;
    alarm(2);
#if defined(O_NDELAY) && defined(F_SETFL)
    portfd = open("/dev/ttyUSB0", O_RDWR|O_NDELAY);
    if (portfd >= 0){
        /* Cancel the O_NDELAY flag. */
        printf("port openend\n");
        n = fcntl(portfd, F_GETFL, 0);
        (void) fcntl(portfd, F_SETFL, n & ~O_NDELAY);
    }
#else
    portfd = open("/dev/ttyUSB0", O_RDWR);
#endif
    if (portfd >= 0) 
    {
        if (len == 0) len = strlen(s);
        for(f = 0; f < len && f <100; f++)
            buf[f] = *s++ | 0x80;
        write(portfd, buf, f);
        printf("Do write\n");
        while(portfd>=0){
            printf("%s\n",buf);
        }
    }

    alarm(0);
    signal(SIGALRM, SIG_IGN);
    if (portfd < 0) {
        printf("cannot open %s. Sorry.\n", "/dev/ttyUSB0");
    }
}

Log of the output:

���������鉀�������������������鍀���������������������������������������������������������������2
����������鉀�������������������鍀���������������������������������������������������������������2
Dave
  • 44,275
  • 12
  • 65
  • 105
Appie
  • 129
  • 1
  • 1
  • 9
  • 2
    Have you checked out [libusb](http://www.libusb.org/)? And if the connected device is a pendrive, and it's mounted into the filesystem, then read the files normally from it. – Some programmer dude Sep 26 '13 at 09:43
  • I tried to include it, but its throwing error as 'No such file' for #include – Appie Sep 26 '13 at 09:46
  • 1
    which distro ? if under debian like have you installed the libusb-dev package ? aka `apt get install libusb-dev` – Mali Sep 26 '13 at 09:52
  • 1
    There is no need for `libusb` or any other third-party library, `/dev/ttyUSB*` is a mere serial line which is handled directly by the kernel. The only thing required is to correctly configure the serial parameters (speed, parity, ...) as with any other serial line. – syam Sep 26 '13 at 09:56
  • 1
    @user2818819 A pen drive is a very different thing from a serial line. /dev/ttyUSB0 is an USB serial line. Different USB devices have _very_ different means of operating. For a simple serial line, there's numerous tutorials - they work the same whether it's an USB serial line, or an old RS232. What are you actually trying to do and what kind of device have you connected ? – nos Sep 26 '13 at 10:01
  • its actually VNC2 eval board. And it is detected as ttyUSB0 in my system. – Appie Sep 26 '13 at 10:18
  • I shouldn't have used "write(portfd, buf, f)" as I am reading from the ttyUSB0. So I used read(), 'read(portfd, buf, f)'. But after executing I'm getting blank screen – Appie Sep 26 '13 at 10:26
  • @syam As u suggested, I configured the serial parameters as follows. But still I'm getting junk values t = tcsetattr(portfd, TCSANOW, &options);tcgetattr(portfd, &options); cfsetispeed(&options,B9600);cfsetospeed(&options,B9600); options.c_cflag |=(CLOCAL | CREAD);options.c_cflag &= ~PARENB; options.c_cflag &=~CSTOPB;options.c_cflag &= CSIZE;options.c_cflag |= CS8; options.c_cflag &= ~( ICANON | ECHO | ECHOE |ISIG ); options.c_iflag &= ~(IXON | IXOFF | IXANY );options.c_oflag &= ~OPOST;tcsetattr(portfd, TCSANOW, &options); options.c_lflag &= 0; options.c_iflag &= 0; options.c_oflag &= 0; – Appie Sep 26 '13 at 11:02

1 Answers1

4

you will need to set the correct port configuration...

struct termios oldtio,newtio;

// open port...
// save existing attributes
tcgetattr(fd,&oldtio);  

// set attributes - these flags may change for your device
#define BAUDRATE B9600 
memset(&newtio, 0x00, sizeof(newtio));  
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;   
newtio.c_iflag = IGNPAR | ICRNL;          
newtio.c_oflag = 0;  

tcflush(fd, TCIFLUSH);  
tcsetattr(fd,TCSANOW,&newtio); 

//reset attributes
tcsetattr(fd,TCSANOW,&oldtio); 

I have a rough working example here... http://file-hub.com/cmd:thread/142300

Trudysfun
  • 181
  • 5