Quantcast

Writing succeeds, but reading fails

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Writing succeeds, but reading fails

Magnus Therning
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: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Uwe Bonnes
>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:

    Magnus> I'm completely new to using libftdi, but I'm running into
    Magnus> problems a little sooner than I'd like.  Hopefully it's
    Magnus> something silly that someone on the list quickly can point me
    Magnus> to.  I've read the API reference, and searched online without
    Magnus> finding anything that can explain the behaviour I see.

If you need plain serial communication, why don't you use /dev/ttyUSBx?
Bye
--
Uwe Bonnes                [hidden email]

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Magnus Therning
On Thu, May 8, 2014 at 10:56 AM, Uwe Bonnes
<[hidden email]> wrote:
>>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:
>
>     Magnus> I'm completely new to using libftdi, but I'm running into
>     Magnus> problems a little sooner than I'd like.  Hopefully it's
>     Magnus> something silly that someone on the list quickly can point me
>     Magnus> to.  I've read the API reference, and searched online without
>     Magnus> finding anything that can explain the behaviour I see.
>
> If you need plain serial communication, why don't you use /dev/ttyUSBx?

The full application I want to port uses ftd2xx to find devices to
talk to, I think doing that discovery is easiest via libftdi.
However, maybe a combination works well enough, as long as I somehow
can tie the items in a `ftdi_device_list` to /dev/ttyUSBx somehow.

Then it's of course also irritating; serial communication via libftdi
ought to work, right? ;)

/M

--
Magnus Therning                      OpenPGP: 0xAB4DFBA4
email: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Uwe Bonnes
>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:

    Magnus> On Thu, May 8, 2014 at 10:56 AM, Uwe Bonnes
    Magnus> <[hidden email]> wrote:
    >>>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:
    >>
    Magnus> I'm completely new to using libftdi, but I'm running into
    Magnus> problems a little sooner than I'd like.  Hopefully it's
    Magnus> something silly that someone on the list quickly can point me
    Magnus> to.  I've read the API reference, and searched online without
    Magnus> finding anything that can explain the behaviour I see.
    >>
    >> If you need plain serial communication, why don't you use
    >> /dev/ttyUSBx?

    Magnus> The full application I want to port uses ftd2xx to find devices
    Magnus> to talk to, I think doing that discovery is easiest via libftdi.
    Magnus> However, maybe a combination works well enough, as long as I
    Magnus> somehow can tie the items in a `ftdi_device_list` to
    Magnus> /dev/ttyUSBx somehow.

To name the kernel device, I use udev. Look at appended udev rule. I program
deviced like:
usb 3-10: new full-speed USB device number 7 using xhci_hcd
usb 3-10: New USB device found, idVendor=0403, idProduct=6001
usb 3-10: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-10: Product: CPS_CONN
usb 3-10: Manufacturer: IKDA
usb 3-10: SerialNumber: 0003

and the udev rule created a link like:
crw-rw----  1 root dialout 188,   0  8. Mai 12:00 ttyUSB0
lrwxrwxrwx  1 root root           7  8. Mai 12:00 CPS_CONN_0003_00 -> ttyUSB0


    Magnus> Then it's of course also irritating; serial communication via
    Magnus> libftdi ought to work, right? ;)

Well, is should. I tested your program on my own hardware with loopback
here, and nothing is read too. Perhaps somebody else has any hints?

Bye
--
Uwe Bonnes                [hidden email]

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
# Files in /etc/udev/rules are called sorted order until first fit is found
#
# So use a low number for own rules
# Other files may also be overwritten
#

#FX2
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="fffe", ATTRS{idProduct}=="0018", GROUP:="users", MODE:="0660"
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="fffe", ATTRS{idProduct}=="001a", GROUP:="users", MODE:="0660"
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="04b4", ATTRS{idProduct}=="8613", RUN+="/sbin/fxload -v -t fx2 -I /usr/share/usrp_main.ihx -D $tempnode "

#FT2232 Adapter
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", GROUP:="users", MODE:="0660"
#FT232 Adapter
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", GROUP:="users", MODE:="0660"
#FT Adapter
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6006", GROUP:="users", MODE:="0660"
#FT230 Adapter
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", GROUP:="users", MODE:="0660"

ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0456", ATTRS{idProduct}=="b403", GROUP:="users", MODE:="0660"

SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{../manufacturer}=="IKDA", SYMLINK="%s{../product}_%s{../serial}_%s{bInterfaceNumber}"


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Magnus Therning
On Thu, May 8, 2014 at 12:51 PM, Uwe Bonnes
<[hidden email]> wrote:

>>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:
>
>     Magnus> The full application I want to port uses ftd2xx to find devices
>     Magnus> to talk to, I think doing that discovery is easiest via libftdi.
>     Magnus> However, maybe a combination works well enough, as long as I
>     Magnus> somehow can tie the items in a `ftdi_device_list` to
>     Magnus> /dev/ttyUSBx somehow.
>
> To name the kernel device, I use udev. Look at appended udev rule. I program
> deviced like:
> usb 3-10: new full-speed USB device number 7 using xhci_hcd
> usb 3-10: New USB device found, idVendor=0403, idProduct=6001
> usb 3-10: New USB device strings: Mfr=1, Product=2, SerialNumber=3
> usb 3-10: Product: CPS_CONN
> usb 3-10: Manufacturer: IKDA
> usb 3-10: SerialNumber: 0003
>
> and the udev rule created a link like:
> crw-rw----  1 root dialout 188,   0  8. Mai 12:00 ttyUSB0
> lrwxrwxrwx  1 root root           7  8. Mai 12:00 CPS_CONN_0003_00 -> ttyUSB0

Yes, I did find some mentions of using udev to generate names for
devices.  It's neat on one level, but I have to say I'm not
enthusiastic about users having to install extra udev rules just to
use the program.  It may very well be the best way though as
everything points to it being impossible to tie a `libusb_device` to a
`/dev/ttyUSBx` in an easy way, apparently one has to go via sysfs :(

Thanks for the udev rule file, the ones I've found have not been as
clear and easy to follow.

>     Magnus> Then it's of course also irritating; serial communication via
>     Magnus> libftdi ought to work, right? ;)
>
> Well, is should. I tested your program on my own hardware with loopback
> here, and nothing is read too. Perhaps somebody else has any hints?

Hopefully, but in any case you have given me a way to proceed now, and
you've even removed the dependency on libftdi ;)

/M

--
Magnus Therning                      OpenPGP: 0xAB4DFBA4
email: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Ryan Tennill

On 5/8/2014 7:15 AM, Magnus Therning wrote:

> On Thu, May 8, 2014 at 12:51 PM, Uwe Bonnes
> <[hidden email]> wrote:
>>>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:
>>      Magnus> The full application I want to port uses ftd2xx to find devices
>>      Magnus> to talk to, I think doing that discovery is easiest via libftdi.
>>      Magnus> However, maybe a combination works well enough, as long as I
>>      Magnus> somehow can tie the items in a `ftdi_device_list` to
>>      Magnus> /dev/ttyUSBx somehow.
>>
>> To name the kernel device, I use udev. Look at appended udev rule. I program
>> deviced like:
>> usb 3-10: new full-speed USB device number 7 using xhci_hcd
>> usb 3-10: New USB device found, idVendor=0403, idProduct=6001
>> usb 3-10: New USB device strings: Mfr=1, Product=2, SerialNumber=3
>> usb 3-10: Product: CPS_CONN
>> usb 3-10: Manufacturer: IKDA
>> usb 3-10: SerialNumber: 0003
>>
>> and the udev rule created a link like:
>> crw-rw----  1 root dialout 188,   0  8. Mai 12:00 ttyUSB0
>> lrwxrwxrwx  1 root root           7  8. Mai 12:00 CPS_CONN_0003_00 -> ttyUSB0
> Yes, I did find some mentions of using udev to generate names for
> devices.  It's neat on one level, but I have to say I'm not
> enthusiastic about users having to install extra udev rules just to
> use the program.  It may very well be the best way though as
> everything points to it being impossible to tie a `libusb_device` to a
> `/dev/ttyUSBx` in an easy way, apparently one has to go via sysfs :(
>
> Thanks for the udev rule file, the ones I've found have not been as
> clear and easy to follow.
>
>>      Magnus> Then it's of course also irritating; serial communication via
>>      Magnus> libftdi ought to work, right? ;)
>>
>> Well, is should. I tested your program on my own hardware with loopback
>> here, and nothing is read too. Perhaps somebody else has any hints?
> Hopefully, but in any case you have given me a way to proceed now, and
> you've even removed the dependency on libftdi ;)
>
> /M
>

In my experience (Linux, same device) I had to explicitly set RTS to get
bidirectional coms. I ran out of time to see if I only needed to do it
once so I set RTS accordingly before and after a block of my code runs.
This appears to be working but at a much slower baud of 115.2k 8N1 in a
system using 60 devices. The target OS is Ubuntu 12.04 but runs on 10.04
and 13.10 as well if that's of any use.
> rv = ftdi_set_rts(ftdi,0);

Ryan

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Jim Paris
In reply to this post by Magnus Therning
Magnus Therning wrote:

> On Thu, May 8, 2014 at 12:51 PM, Uwe Bonnes
> <[hidden email]> wrote:
> >>>>>> "Magnus" == Magnus Therning <[hidden email]> writes:
> >
> >     Magnus> The full application I want to port uses ftd2xx to find devices
> >     Magnus> to talk to, I think doing that discovery is easiest via libftdi.
> >     Magnus> However, maybe a combination works well enough, as long as I
> >     Magnus> somehow can tie the items in a `ftdi_device_list` to
> >     Magnus> /dev/ttyUSBx somehow.
> >
> > To name the kernel device, I use udev. Look at appended udev rule. I program
> > deviced like:
> > usb 3-10: new full-speed USB device number 7 using xhci_hcd
> > usb 3-10: New USB device found, idVendor=0403, idProduct=6001
> > usb 3-10: New USB device strings: Mfr=1, Product=2, SerialNumber=3
> > usb 3-10: Product: CPS_CONN
> > usb 3-10: Manufacturer: IKDA
> > usb 3-10: SerialNumber: 0003
> >
> > and the udev rule created a link like:
> > crw-rw----  1 root dialout 188,   0  8. Mai 12:00 ttyUSB0
> > lrwxrwxrwx  1 root root           7  8. Mai 12:00 CPS_CONN_0003_00 -> ttyUSB0
>
> Yes, I did find some mentions of using udev to generate names for
> devices.  It's neat on one level, but I have to say I'm not
> enthusiastic about users having to install extra udev rules just to
> use the program.

Udev has already included the rules to create persistent names for
serial devices in /dev/serial/by-id since version 136 (released in 2009).

Jim

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Writing succeeds, but reading fails

Magnus Therning
On Thu, May 8, 2014 at 5:30 PM, Jim Paris <[hidden email]> wrote:
> Magnus Therning wrote:
>> Yes, I did find some mentions of using udev to generate names for
>> devices.  It's neat on one level, but I have to say I'm not
>> enthusiastic about users having to install extra udev rules just to
>> use the program.
>
> Udev has already included the rules to create persistent names for
> serial devices in /dev/serial/by-id since version 136 (released in 2009).

Wow look at that!  I didn't know that, thanks for pointing it out.
That puts me in a better mood today :)

/M

--
Magnus Therning                      OpenPGP: 0xAB4DFBA4
email: [hidden email]   jabber: [hidden email]
twitter: magthe               http://therning.org/magnus

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Loading...