libftdi Archives

Subject: Re: serial port does not close completely

From: "E.S. Rosenberg" <es.rosenberg+libftdi.i2net@xxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Sun, 18 May 2014 01:30:39 +0300



2014-04-30 16:25 GMT+03:00 Chris <mailingliste.chris@xxxxxxxxx>:
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 libftdi+unsubscribe@developer.intra2net.com  



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


Current Thread