serial port does not close completely

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

serial port does not close completely

Chris
hello folks,

I have some trouble closing the serial port with libftdi. After
connecting the ftdi to the computer, it is available via /dev/ttyUSB0 (
Linux ;) )

#include <stdio.h>
#include <ftdi.h>

#define PIN 0x08  /* CTS (brown wire on FTDI cable) */

int main()
{
     unsigned char c = 0;
     struct ftdi_context ftdic;
     ftdi_init(&ftdic);

     if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
         puts("Can't open device");
         return 1;
     }

     if(ftdi_set_bitmode(&ftdic, PIN,BITMODE_BITBANG) < 0) {
             puts("Can't open device");
             return 1;
     }

        puts("1");
     c ^= PIN;
     ftdi_write_data(&ftdic, &c, 1);

     ftdi_usb_reset(&ftdic);
     puts("2");
     if ((ftdi_usb_close(&ftdic)) < 0){
         fprintf(stderr, "unable to close ftdi device: (%s)\n",
ftdi_get_error_string(&ftdic));
         ftdi_free(&ftdic);
     }
     puts("3");
     ftdi_free(&ftdic);

     //ftdi_deinit(&ftdic);
     puts("4");
     return 0;
}


after execute this program I get this in the terminal:
sudo ./ftdi_bitbang_test
1
2
3
*** Error in `./ftdi_bitbang_test': double free or corruption (out):
0xbfcbe478 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb76727e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb7673530]
/usr/lib/i386-linux-gnu/libftdi.so.1(ftdi_free+0x2a)[0xb77b0a9a]
./ftdi_bitbang_test[0x8048888]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7615935]
./ftdi_bitbang_test[0x8048681]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 429381
/home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
08049000-0804a000 r--p 00000000 08:01 429381
/home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
0804a000-0804b000 rw-p 00001000 08:01 429381
/home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
09f15000-09f36000 rw-p 00000000 00:00 0          [heap]
b75c0000-b75db000 r-xp 00000000 08:01 918367
/lib/i386-linux-gnu/libgcc_s.so.1
b75db000-b75dc000 r--p 0001a000 08:01 918367
/lib/i386-linux-gnu/libgcc_s.so.1
b75dc000-b75dd000 rw-p 0001b000 08:01 918367
/lib/i386-linux-gnu/libgcc_s.so.1
b75f0000-b75f1000 rw-p 00000000 00:00 0
b75f1000-b75f8000 r-xp 00000000 08:01 918484
/lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75f8000-b75f9000 r--p 00006000 08:01 918484
/lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75f9000-b75fa000 rw-p 00007000 08:01 918484
/lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75fa000-b75fc000 rw-p 00000000 00:00 0
b75fc000-b77a9000 r-xp 00000000 08:01 918342
/lib/i386-linux-gnu/libc-2.17.so
b77a9000-b77ab000 r--p 001ad000 08:01 918342
/lib/i386-linux-gnu/libc-2.17.so
b77ab000-b77ac000 rw-p 001af000 08:01 918342
/lib/i386-linux-gnu/libc-2.17.so
b77ac000-b77af000 rw-p 00000000 00:00 0
b77af000-b77b6000 r-xp 00000000 08:01 429415
/usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77b6000-b77b7000 r--p 00006000 08:01 429415
/usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77b7000-b77b8000 rw-p 00007000 08:01 429415
/usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77c9000-b77cd000 rw-p 00000000 00:00 0
b77cd000-b77ce000 r-xp 00000000 00:00 0          [vdso]
b77ce000-b77ee000 r-xp 00000000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
b77ee000-b77ef000 r--p 0001f000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
b77ef000-b77f0000 rw-p 00020000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
bfc9e000-bfcbf000 rw-p 00000000 00:00 0          [stack]

if I use ftdi_deinit instead of ftdi_free the program runs fine, but the
ftdi is not longer available trough /dev/ttyUSB0 for other applications.
also the combination of ftdi_disable_bitbang, ftdi_usb_close and
ftdi_deinit will not work for me.

--
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: serial port does not close completely

Thomas Jarosch
Hi Chris,

On Wednesday, 30. April 2014 15:25:58 Chris wrote:
> if I use ftdi_deinit instead of ftdi_free the program runs fine, but the
> ftdi is not longer available trough /dev/ttyUSB0 for other applications.
> also the combination of ftdi_disable_bitbang, ftdi_usb_close and
> ftdi_deinit will not work for me.

I'm not sure I can follow your here, but the ftdi_sio kernel
driver is currently not re-attached after use by libftdi.

So /dev/ttyUSB0 is not available after using libftdi on the device,
at least for now.

Cheers,
Thomas


--
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: serial port does not close completely

E.S. Rosenberg
In reply to this post by Chris



2014-04-30 16:25 GMT+03:00 Chris <[hidden email]>:
hello folks,

I have some trouble closing the serial port with libftdi. After connecting the ftdi to the computer, it is available via /dev/ttyUSB0 ( Linux ;) )

#include <stdio.h>
#include <ftdi.h>

#define PIN 0x08  /* CTS (brown wire on FTDI cable) */

int main()
{
    unsigned char c = 0;
    struct ftdi_context ftdic;
    ftdi_init(&ftdic);

    if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
        puts("Can't open device");
        return 1;
    }

    if(ftdi_set_bitmode(&ftdic, PIN,BITMODE_BITBANG) < 0) {
            puts("Can't open device");
            return 1;
    }

       puts("1");
    c ^= PIN;
    ftdi_write_data(&ftdic, &c, 1);

    ftdi_usb_reset(&ftdic);
    puts("2");
    if ((ftdi_usb_close(&ftdic)) < 0){
        fprintf(stderr, "unable to close ftdi device: (%s)\n", ftdi_get_error_string(&ftdic));
        ftdi_free(&ftdic);
    }
    puts("3");
    ftdi_free(&ftdic);

    //ftdi_deinit(&ftdic);
    puts("4");
    return 0;
}


after execute this program I get this in the terminal:
sudo ./ftdi_bitbang_test
1
2
3
*** Error in `./ftdi_bitbang_test': double free or corruption (out): 0xbfcbe478 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb76727e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb7673530]
/usr/lib/i386-linux-gnu/libftdi.so.1(ftdi_free+0x2a)[0xb77b0a9a]
./ftdi_bitbang_test[0x8048888]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7615935]
./ftdi_bitbang_test[0x8048681]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 429381 /home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
08049000-0804a000 r--p 00000000 08:01 429381 /home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
0804a000-0804b000 rw-p 00001000 08:01 429381 /home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
09f15000-09f36000 rw-p 00000000 00:00 0          [heap]
b75c0000-b75db000 r-xp 00000000 08:01 918367 /lib/i386-linux-gnu/libgcc_s.so.1
b75db000-b75dc000 r--p 0001a000 08:01 918367 /lib/i386-linux-gnu/libgcc_s.so.1
b75dc000-b75dd000 rw-p 0001b000 08:01 918367 /lib/i386-linux-gnu/libgcc_s.so.1
b75f0000-b75f1000 rw-p 00000000 00:00 0
b75f1000-b75f8000 r-xp 00000000 08:01 918484 /lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75f8000-b75f9000 r--p 00006000 08:01 918484 /lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75f9000-b75fa000 rw-p 00007000 08:01 918484 /lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75fa000-b75fc000 rw-p 00000000 00:00 0
b75fc000-b77a9000 r-xp 00000000 08:01 918342 /lib/i386-linux-gnu/libc-2.17.so
b77a9000-b77ab000 r--p 001ad000 08:01 918342 /lib/i386-linux-gnu/libc-2.17.so
b77ab000-b77ac000 rw-p 001af000 08:01 918342 /lib/i386-linux-gnu/libc-2.17.so
b77ac000-b77af000 rw-p 00000000 00:00 0
b77af000-b77b6000 r-xp 00000000 08:01 429415 /usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77b6000-b77b7000 r--p 00006000 08:01 429415 /usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77b7000-b77b8000 rw-p 00007000 08:01 429415 /usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77c9000-b77cd000 rw-p 00000000 00:00 0
b77cd000-b77ce000 r-xp 00000000 00:00 0          [vdso]
b77ce000-b77ee000 r-xp 00000000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
b77ee000-b77ef000 r--p 0001f000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
b77ef000-b77f0000 rw-p 00020000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
bfc9e000-bfcbf000 rw-p 00000000 00:00 0          [stack]

if I use ftdi_deinit instead of ftdi_free the program runs fine, but the ftdi is not longer available trough /dev/ttyUSB0 for other applications.
ftdi_deinit() de-initialized/frees all the different structures pointed to inside the ftdi_context and exits if the passed context pointer is null.
ftdi_free() runs ftdi_deinit() and then free() on the ftdi_context, so if you run that twice you are trying to release memory to the OS that you already released.

The moment you start something that uses libftdi it is supposed to unload the ftdi_sio kernel driver for the device, afaik it doesn't get automatically reloaded after the program terminates so you would have to do modprobe ftdi_sio usbserial to again have a ttyUSBx.
This is a setting as far as I can tell so you may be able to change it and leave ftdi_sio loaded but I doubt that that will result in anything good since that would just mean two different programs trying to access the same device concurrently...

also the combination of ftdi_disable_bitbang, ftdi_usb_close and ftdi_deinit will not work for me.
To do what?
Regards,
Eli

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



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: serial port does not close completely

Ryan Tennill

On 5/17/2014 5:30 PM, E.S. Rosenberg wrote:



2014-04-30 16:25 GMT+03:00 Chris <[hidden email]>:
hello folks,

I have some trouble closing the serial port with libftdi. After connecting the ftdi to the computer, it is available via /dev/ttyUSB0 ( Linux ;) )

#include <stdio.h>
#include <ftdi.h>

#define PIN 0x08  /* CTS (brown wire on FTDI cable) */

int main()
{
    unsigned char c = 0;
    struct ftdi_context ftdic;
    ftdi_init(&ftdic);

    if(ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0) {
        puts("Can't open device");
        return 1;
    }

    if(ftdi_set_bitmode(&ftdic, PIN,BITMODE_BITBANG) < 0) {
            puts("Can't open device");
            return 1;
    }

       puts("1");
    c ^= PIN;
    ftdi_write_data(&ftdic, &c, 1);

    ftdi_usb_reset(&ftdic);
    puts("2");
    if ((ftdi_usb_close(&ftdic)) < 0){
        fprintf(stderr, "unable to close ftdi device: (%s)\n", ftdi_get_error_string(&ftdic));
        ftdi_free(&ftdic);
    }
    puts("3");
    ftdi_free(&ftdic);

    //ftdi_deinit(&ftdic);
    puts("4");
    return 0;
}


after execute this program I get this in the terminal:
sudo ./ftdi_bitbang_test
1
2
3
*** Error in `./ftdi_bitbang_test': double free or corruption (out): 0xbfcbe478 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb76727e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb7673530]
/usr/lib/i386-linux-gnu/libftdi.so.1(ftdi_free+0x2a)[0xb77b0a9a]
./ftdi_bitbang_test[0x8048888]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7615935]
./ftdi_bitbang_test[0x8048681]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 429381 /home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
08049000-0804a000 r--p 00000000 08:01 429381 /home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
0804a000-0804b000 rw-p 00001000 08:01 429381 /home/user/workspace/ftdi_bitbang_test/Debug/ftdi_bitbang_test
09f15000-09f36000 rw-p 00000000 00:00 0          [heap]
b75c0000-b75db000 r-xp 00000000 08:01 918367 /lib/i386-linux-gnu/libgcc_s.so.1
b75db000-b75dc000 r--p 0001a000 08:01 918367 /lib/i386-linux-gnu/libgcc_s.so.1
b75dc000-b75dd000 rw-p 0001b000 08:01 918367 /lib/i386-linux-gnu/libgcc_s.so.1
b75f0000-b75f1000 rw-p 00000000 00:00 0
b75f1000-b75f8000 r-xp 00000000 08:01 918484 /lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75f8000-b75f9000 r--p 00006000 08:01 918484 /lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75f9000-b75fa000 rw-p 00007000 08:01 918484 /lib/i386-linux-gnu/libusb-0.1.so.4.4.4
b75fa000-b75fc000 rw-p 00000000 00:00 0
b75fc000-b77a9000 r-xp 00000000 08:01 918342 /lib/i386-linux-gnu/libc-2.17.so
b77a9000-b77ab000 r--p 001ad000 08:01 918342 /lib/i386-linux-gnu/libc-2.17.so
b77ab000-b77ac000 rw-p 001af000 08:01 918342 /lib/i386-linux-gnu/libc-2.17.so
b77ac000-b77af000 rw-p 00000000 00:00 0
b77af000-b77b6000 r-xp 00000000 08:01 429415 /usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77b6000-b77b7000 r--p 00006000 08:01 429415 /usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77b7000-b77b8000 rw-p 00007000 08:01 429415 /usr/lib/i386-linux-gnu/libftdi.so.1.20.0
b77c9000-b77cd000 rw-p 00000000 00:00 0
b77cd000-b77ce000 r-xp 00000000 00:00 0          [vdso]
b77ce000-b77ee000 r-xp 00000000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
b77ee000-b77ef000 r--p 0001f000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
b77ef000-b77f0000 rw-p 00020000 08:01 918318 /lib/i386-linux-gnu/ld-2.17.so
bfc9e000-bfcbf000 rw-p 00000000 00:00 0          [stack]

if I use ftdi_deinit instead of ftdi_free the program runs fine, but the ftdi is not longer available trough /dev/ttyUSB0 for other applications.
ftdi_deinit() de-initialized/frees all the different structures pointed to inside the ftdi_context and exits if the passed context pointer is null.
ftdi_free() runs ftdi_deinit() and then free() on the ftdi_context, so if you run that twice you are trying to release memory to the OS that you already released.

The moment you start something that uses libftdi it is supposed to unload the ftdi_sio kernel driver for the device, afaik it doesn't get automatically reloaded after the program terminates so you would have to do modprobe ftdi_sio usbserial to again have a ttyUSBx.
This is a setting as far as I can tell so you may be able to change it and leave ftdi_sio loaded but I doubt that that will result in anything good since that would just mean two different programs trying to access the same device concurrently...

also the combination of ftdi_disable_bitbang, ftdi_usb_close and ftdi_deinit will not work for me.
To do what?
Regards,
Eli

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



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



You can reattach the kernel driver by libusb call but it could be useful to make this part of releasing the device when the library/context is cleaned up. I would probably favor a second flag to auto-reattach similar to the auto-detach rather than using the same flag for two purposes.

libusb_attach_kernel_driver(ftdi->usb_dev, INTERFACE_ANY).


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: serial port does not close completely

Chris
In reply to this post by Thomas Jarosch
Yes, this is the reason. Thank you.
I found the detach function, is a retach function planned?

Best regards,
Chris

Am 16.05.2014 17:44, schrieb Thomas Jarosch:

> Hi Chris,
>
> On Wednesday, 30. April 2014 15:25:58 Chris wrote:
>> if I use ftdi_deinit instead of ftdi_free the program runs fine, but the
>> ftdi is not longer available trough /dev/ttyUSB0 for other applications.
>> also the combination of ftdi_disable_bitbang, ftdi_usb_close and
>> ftdi_deinit will not work for me.
> I'm not sure I can follow your here, but the ftdi_sio kernel
> driver is currently not re-attached after use by libftdi.
>
> So /dev/ttyUSB0 is not available after using libftdi on the device,
> at least for now.
>
> Cheers,
> Thomas
>


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

Loading...