libftdi Archives

Subject: Writing succeeds, but reading fails

From: Magnus Therning <magnus@xxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Thu, 8 May 2014 10:01:29 +0200
I'm completely new to using libftdi, but I'm running into problems a
little sooner than I'd like.  Hopefully it's something silly that
someone on the list quickly can point me to.  I've read the API
reference, and searched online without finding anything that can
explain the behaviour I see.

I'm looking at replacing the use of ftd2xx lib with libftdi mainly
because I'm having issues with stability when using ftd2xx on Linux.
The chip I'm communicating with is:

    Manufacturer: FTDI
    Description:  FT232R USB UART
    Serial:       A500EJI0

This is my first little test just to see whether writing and reading
works (minus the error checking):

    int main(void)
    {
        int ret;
        struct ftdi_context *ftdi;

        ftdi = ftdi_new();

        struct ftdi_device_list *dev_list;
        ret = ftdi_usb_find_all(ftdi, &dev_list, 0, 0);
        printf("Found %i devices\n", ret);
        for(struct ftdi_device_list *p = dev_list; p; p = p->next) {
            ftdi_usb_open_dev(ftdi, p->dev);
            ftdi_set_baudrate(ftdi, 921600);
            ftdi_setflowctrl(ftdi, SIO_RTS_CTS_HS);
            ftdi_set_line_property(ftdi, BITS_8, STOP_BIT_1, NONE);
            ftdi_write_data_set_chunksize(ftdi, 128);
            ftdi_read_data_set_chunksize(ftdi, 128);
            ftdi_set_bitmode(ftdi, 0xFF, BITMODE_RESET);

            uint8_t cmd[6] = {0xf5, 0x0, 0x0, 0x0, 0x33, 0xcc};
            ftdi_write_data(ftdi, cmd, 6);

            for(int i = 0; i < 5; i++) {
                uint8_t res[10] = {0};
                ret = ftdi_read_data(ftdi, res, 10);
                printf("Read %i bytes of data\n", ret);
                if(ret > 0) break;
                sleep(2);
            }

            ret = ftdi_usb_close(ftdi);
        }
        ftdi_list_free2(dev_list);
        ftdi_free(ftdi);
        return EXIT_SUCCESS;
    }

This program successfully writes and the expected data is received on
the other end.  I've also verified that the other end sends the
expected 6-byte reply.  The 6 bytes in the reply isn't received
though, and ftdi_read_data() returns 0 on each call.

The equivalent program using ftd2xx (on Windows) works as expected!

Any pointers as to what I'm doing wrong?

/M

-- 
Magnus Therning                      OpenPGP: 0xAB4DFBA4
email: magnus@xxxxxxxxxxxx   jabber: magnus@xxxxxxxxxxxx
twitter: magthe               http://therning.org/magnus

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread