Thread: Talking to device over Serial: Write works, Read doesn't

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Talking to device over Serial: Write works, Read doesn't

    I'm trying to talk to a device over a USB serial device (/dev/ttyUSB0). I can send commands to it with write(), but I'm having problems with read().

    When I set VMIN and VTIME, read fails with "Resource Temporarily Unavailable". Otherwise, it "succeeds" with 0 Bytes returned.

    I have configured the serial device as such:
    Code:
    int fd, ret;
    struct termios options;
    
    /*fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);*/
    fd = open(device, O_RDWR | O_NOCTTY);
    
    cfsetispeed(&options, B1200);
    cfsetospeed(&options, B1200);
    options.c_cflag |= (CREAD | CLOCAL);
    options.c_cflag &= ~CSIZE;
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_cflag |= CS8;
    options.c_oflag &= ~OPOST;
    options.c_iflag &= ~(IXON | IXOFF | IXANY);
    options.c_iflag |= (IGNPAR);
    
    /*
    options.c_cc[VMIN] = 0;
    options.c_cc[VTIME] = 1;
    */
    
    ret = tcsetattr(fd, TCSANOW, &options);
    Is this sufficient to read() input? Someone suggested I use poll(), but I haven't tried it yet.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Have you checked that there isn't a getty process also trying to use that device.

    If you use "cat /dev/ttyUSB0", can you read from it that way?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Quote Originally Posted by matsp View Post
    Have you checked that there isn't a getty process also trying to use that device.

    If you use "cat /dev/ttyUSB0", can you read from it that way?

    --
    Mats
    How can I check? I doubt any other process is trying to use it unless a previous instance of my own app didn't close it properly.

    I don't see any input from cat, but it does close after a few seconds.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to write multiports serial application
    By pansuriya.amit in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-25-2007, 07:29 AM
  2. Please help with serial communication problem - Long
    By spdylude in forum Windows Programming
    Replies: 3
    Last Post: 04-06-2005, 09:41 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 10:25 PM
  5. InferRed Serial COMM, cannot write?
    By Xei in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2003, 01:40 PM