Ejemplo n.º 1
0
void EnttecDMXUSBOpen::run()
{
	ftdi_usb_purge_buffers(&m_context);

	m_running = true;
	while (m_running == true)
	{
		if (isOpen() == false)
			continue;

		if (ftdi_set_line_property2(&m_context, BITS_8, STOP_BIT_2,
					    NONE, BREAK_ON) < 0)
		{
			qWarning() << "Unable to toggle BREAK_ON for"
				   << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			goto framesleep;
		}

		sleep(88);

		if (ftdi_set_line_property2(&m_context, BITS_8, STOP_BIT_2,
					    NONE, BREAK_OFF) < 0)
		{
			qWarning() << "Unable to toggle BREAK_OFF for"
				   << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
			goto framesleep;
		}

		sleep(8);

		m_mutex.lock();
		if (ftdi_write_data(&m_context, (unsigned char*) m_universe.data(),
				    m_universe.size()) < 0)
		{
			qWarning() << "Unable to write DMX data to"
				   << uniqueName()
				   << ":" << ftdi_get_error_string(&m_context);
		}
		m_mutex.unlock();

framesleep:
		sleep(22754);
	}
}
Ejemplo n.º 2
0
int FtdiDevice::setLineProperties( FTDI_DATABITS_TYPE dataBits, FTDI_STOPBITS_TYPE stopBits,
											 FTDI_PARITY_TYPE parity, FTDI_BREAK_TYPE breakType ) const
{
	if ( context_ == 0 ) return RV_DEVICE_NOT_OPEN;
	int r = ftdi_set_line_property2( context_, (ftdi_bits_type)dataBits,
																	(ftdi_stopbits_type)stopBits, (ftdi_parity_type)parity,
																	(ftdi_break_type)breakType );
	return ( r < 0 ) ? false : true;
}
Ejemplo n.º 3
0
static int do_dmx_break(struct ftdi_context* ftdic)
{
    int ret;
    
	if ((ret = ftdi_set_line_property2(ftdic, BITS_8, STOP_BIT_2, NONE, BREAK_ON)) < 0)
	{
        fprintf(stderr, "unable to set BREAK ON: %d (%s)\n", ret, ftdi_get_error_string(ftdic));
        return EXIT_FAILURE;
	}
    usleep(DMX_BREAK);
	if ((ret = ftdi_set_line_property2(ftdic, BITS_8, STOP_BIT_2, NONE, BREAK_OFF)) < 0)
	{
        fprintf(stderr, "unable to set BREAK OFF: %d (%s)\n", ret, ftdi_get_error_string(ftdic));
        return EXIT_FAILURE;
	}
    usleep(DMX_MAB);
	return EXIT_SUCCESS;
}
Ejemplo n.º 4
0
static dc_status_t serial_ftdi_set_break (void *io, unsigned int level)
{
	ftdi_serial_t *device = io;

	if (device == NULL)
		return DC_STATUS_INVALIDARGS;

	INFO (device->context, "Break: value=%i", level);

	if (ftdi_set_line_property2(device->ftdi_ctx, device->databits, device->stopbits, device->parity, level)) {
		ERROR (device->context, "%s", ftdi_get_error_string(device->ftdi_ctx));
		return DC_STATUS_IO;
	}

	return DC_STATUS_UNSUPPORTED;
}
Ejemplo n.º 5
0
bool QLCFTDI::setBreak(bool on)
{
    ftdi_break_type type;
    if (on == true)
        type = BREAK_ON;
    else
        type = BREAK_OFF;

    if (ftdi_set_line_property2(&m_handle, BITS_8, STOP_BIT_2, NONE, type) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}
Ejemplo n.º 6
0
QList<DMXInterface *> LibFTDIInterface::interfaces(QList<DMXInterface *> discoveredList)
{
    QList <DMXInterface*> interfacesList;
    int id = 0;

    struct ftdi_context ftdi;

    ftdi_init(&ftdi);

#ifdef LIBFTDI1
    libusb_device *dev;
    libusb_device **devs;
    struct libusb_device_descriptor dev_descriptor;
    int i = 0;

    if (libusb_get_device_list(ftdi.usb_ctx, &devs) < 0)
    {
        qDebug() << "usb_find_devices() failed";
        return interfacesList;
    }

    while ((dev = devs[i++]) != NULL)
    {
        libusb_get_device_descriptor(dev, &dev_descriptor);
#else
    struct usb_bus *bus;
    struct usb_device *dev;
    struct usb_device_descriptor dev_descriptor;

    usb_init();

    if (usb_find_busses() < 0)
    {
        qDebug() << "usb_find_busses() failed";
        return interfacesList;
    }
    if (usb_find_devices() < 0)
    {
        qDebug() << "usb_find_devices() failed";
        return interfacesList;
    }

    for (bus = usb_get_busses(); bus; bus = bus->next)
    {
      for (dev = bus->devices; dev; dev = dev->next)
      {
        dev_descriptor = dev->descriptor;
#endif
        Q_ASSERT(dev != NULL);

        // Skip non wanted devices
        if (validInterface(dev_descriptor.idVendor, dev_descriptor.idProduct) == false)
            continue;

        char ser[256];
        memset(ser, 0, 256);
        char nme[256];
        char vend[256];

        ftdi_usb_get_strings(&ftdi, dev, vend, 256, nme, 256, ser, 256);

        QString serial(ser);
        QString name(nme);
        QString vendor(vend);

        qDebug() << Q_FUNC_INFO << "DMX USB VID:" << QString::number(dev_descriptor.idVendor, 16) <<
                    "PID:" << QString::number(dev_descriptor.idProduct, 16);
        qDebug() << Q_FUNC_INFO << "DMX USB serial: " << serial << "name:" << name << "vendor:" << vendor;

        bool found = false;
        for (int c = 0; c < discoveredList.count(); c++)
        {
            if (discoveredList.at(c)->checkInfo(serial, name, vendor) == true)
            {
                found = true;
                break;
            }
        }
        if (found == false)
        {
            LibFTDIInterface *iface = new LibFTDIInterface(serial, name, vendor, dev_descriptor.idVendor,
                                                           dev_descriptor.idProduct, id++);
#ifdef LIBFTDI1
            iface->setBusLocation(libusb_get_port_number(dev));
#else
            iface->setBusLocation(dev->bus->location);
#endif
            interfacesList << iface;
        }

#ifndef LIBFTDI1
      }
#endif
    }

#ifdef LIBFTDI1
    libusb_free_device_list(devs, 1);
#endif

    ftdi_deinit(&ftdi);

    return interfacesList;
}

bool LibFTDIInterface::open()
{
    if (isOpen() == true)
        return true;

    QByteArray sba = serial().toLatin1();
    const char *ser = NULL;
    if (serial().isEmpty() == false)
        ser = (const char *)sba.data();

    if (ftdi_usb_open_desc(&m_handle, vendorID(), productID(),
                           name().toLatin1(), ser) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::openByPID(const int PID)
{
    if (isOpen() == true)
        return true;

    if (ftdi_usb_open(&m_handle, DMXInterface::FTDIVID, PID) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::close()
{
    if (ftdi_usb_close(&m_handle) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::isOpen() const
{
    return (m_handle.usb_dev != NULL) ? true : false;
}

bool LibFTDIInterface::reset()
{
    if (ftdi_usb_reset(&m_handle) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::setLineProperties()
{
    if (ftdi_set_line_property(&m_handle, BITS_8, STOP_BIT_2, NONE) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::setBaudRate()
{
    if (ftdi_set_baudrate(&m_handle, 250000) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::setFlowControl()
{
    if (ftdi_setflowctrl(&m_handle, SIO_DISABLE_FLOW_CTRL) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::clearRts()
{
    if (ftdi_setrts(&m_handle, 0) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::purgeBuffers()
{
    if (ftdi_usb_purge_buffers(&m_handle) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool LibFTDIInterface::setBreak(bool on)
{
    ftdi_break_type type;
    if (on == true)
        type = BREAK_ON;
    else
        type = BREAK_OFF;

    if (ftdi_set_line_property2(&m_handle, BITS_8, STOP_BIT_2, NONE, type) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}
Ejemplo n.º 7
0
int Context::set_line_property(enum ftdi_bits_type bits, enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity, enum ftdi_break_type break_type)
{
    return ftdi_set_line_property2(d->ftdi, bits, sbit, parity, break_type);
}
Ejemplo n.º 8
0
QList <DMXUSBWidget*> QLCFTDI::widgets()
{
    QList <DMXUSBWidget*> widgetList;
    quint32 input_id = 0;

    struct ftdi_context ftdi;

    ftdi_init(&ftdi);

#ifdef LIBFTDI1
    libusb_device *dev;
    libusb_device **devs;
    int i = 0;

    if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
    {
        qDebug() << "usb_find_devices() failed";
        ftdi_error_return(-5, "libusb_get_device_list() failed");
    }

    while ((dev = devs[i++]) != NULL)
    {
#else
    struct usb_bus *bus;
    struct usb_device *dev;

    usb_init();

    if (usb_find_busses() < 0)
    {
        qDebug() << "usb_find_busses() failed";
        return widgetList;
    }
    if (usb_find_devices() < 0)
    {
        qDebug() << "usb_find_devices() failed";
        return widgetList;
    }

    for (bus = usb_get_busses(); bus; bus = bus->next)
    {
      for (dev = bus->devices; dev; dev = dev->next)
      {
#endif
        Q_ASSERT(dev != NULL);

        // Skip non wanted devices
        if (dev->descriptor.idVendor != QLCFTDI::FTDIVID &&
            dev->descriptor.idVendor != QLCFTDI::ATMELVID)
                continue;

        if (dev->descriptor.idProduct != QLCFTDI::FTDIPID &&
            dev->descriptor.idProduct != QLCFTDI::DMX4ALLPID &&
            dev->descriptor.idProduct != QLCFTDI::NANODMXPID)
                continue;

        char ser[256];
        char nme[256];
        char vend[256];

        ftdi_usb_get_strings(&ftdi, dev, vend, 256, nme, 256, ser, 256);

        QString serial(ser);
        QString name(nme);
        QString vendor(vend);

        QMap <QString,QVariant> types(typeMap());

        qDebug() << Q_FUNC_INFO << "DMX USB VID:" << QString::number(dev->descriptor.idVendor, 16) <<
                    "PID:" << QString::number(dev->descriptor.idProduct, 16);
        qDebug() << Q_FUNC_INFO << "DMX USB serial: " << serial << "name:" << name << "vendor:" << vendor;

        if (types.contains(serial) == true)
        {
            // Force a widget with a specific serial to either type
            DMXUSBWidget::Type type = (DMXUSBWidget::Type) types[serial].toInt();
            switch (type)
            {
            case DMXUSBWidget::OpenTX:
                widgetList << new EnttecDMXUSBOpen(serial, name, vendor);
                break;
            case DMXUSBWidget::ProRX:
            {
                EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++);
                widgetList << prorx;
                break;
            }
            case DMXUSBWidget::ProMk2:
            {
                EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor, 1);
                widgetList << protx;
                widgetList << new EnttecDMXUSBProTX(serial, name, vendor, 2, protx->ftdi());
                EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi());
                widgetList << prorx;
                break;
            }
            case DMXUSBWidget::UltraProTx:
            {
                UltraDMXUSBProTx* protx = new UltraDMXUSBProTx(serial, name, vendor, 1);
                widgetList << protx;
                widgetList << new UltraDMXUSBProTx(serial, name, vendor, 2, protx->ftdi());
                EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi());
                widgetList << prorx;
                break;
            }
            case DMXUSBWidget::VinceTX:
                widgetList << new VinceUSBDMX512TX(serial, name, vendor);
                break;
            default:
            case DMXUSBWidget::ProTX:
                widgetList << new EnttecDMXUSBProTX(serial, name, vendor);
                break;
            }
        }
        else if (name.toUpper().contains("PRO MK2") == true)
        {
            EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor, 1);
            widgetList << protx;
            widgetList << new EnttecDMXUSBProTX(serial, name, vendor, 2, protx->ftdi());
            EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi());
            widgetList << prorx;
        }
        else if (name.toUpper().contains("DMX USB PRO"))
        {
            /** Check if the device responds to label 77 and 78, so it might be a DMXking adapter */
            int ESTAID = 0;
            int DEVID = 0;
            QString manName = readLabel(&ftdi, name.toLatin1().data(), serial.toLatin1().data(),
                                        USB_DEVICE_MANUFACTURER, &ESTAID);
            qDebug() << "--------> Device Manufacturer: " << manName;
            QString devName = readLabel(&ftdi, name.toLatin1().data(), serial.toLatin1().data(),
                                        USB_DEVICE_NAME, &DEVID);
            qDebug() << "--------> Device Name: " << devName;
            qDebug() << "--------> ESTA Code: " << QString::number(ESTAID, 16) << ", Device ID: " << QString::number(DEVID, 16);
            if (ESTAID == DMXKING_ESTA_ID)
            {
                if (DEVID == ULTRADMX_PRO_DEV_ID)
                {
                    UltraDMXUSBProTx* protxP1 = new UltraDMXUSBProTx(serial, name, vendor, 1);
                    protxP1->setRealName(devName);
                    widgetList << protxP1;
                    UltraDMXUSBProTx* protxP2 = new UltraDMXUSBProTx(serial, name, vendor, 2, protxP1->ftdi());
                    protxP2->setRealName(devName);
                    widgetList << protxP2;
                    EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protxP1->ftdi());
                    prorx->setRealName(devName);
                    widgetList << prorx;
                }
                else
                {
                    EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor);
                    protx->setRealName(devName);
                    widgetList << protx;
                }
            }
            else
            {
                /* This is probably a Enttec DMX USB Pro widget */
                EnttecDMXUSBProTX* protx = new EnttecDMXUSBProTX(serial, name, vendor);
                widgetList << protx;
                EnttecDMXUSBProRX* prorx = new EnttecDMXUSBProRX(serial, name, vendor, input_id++, protx->ftdi());
                widgetList << prorx;
            }
        }
        else if (name.toUpper().contains("USB-DMX512 CONVERTER") == true)
        {
            widgetList << new VinceUSBDMX512TX(serial, name, vendor);
        }
        else if (dev->descriptor.idVendor == QLCFTDI::FTDIVID &&
                 dev->descriptor.idProduct == QLCFTDI::DMX4ALLPID)
        {
            widgetList << new Stageprofi(serial, name, vendor);
        }
#if defined(Q_WS_X11) || defined(Q_OS_LINUX)
        else if (dev->descriptor.idVendor == QLCFTDI::ATMELVID &&
                 dev->descriptor.idProduct == QLCFTDI::NANODMXPID)
        {
            widgetList << new NanoDMX(serial, name, vendor);
        }
#endif
        else
        {
            /* This is probably an Open DMX USB widget */
            widgetList << new EnttecDMXUSBOpen(serial, name, vendor, 0);
        }
#ifndef LIBFTDI1
      }
#endif
    }

#ifdef LIBFTDI1
    libusb_free_device_list(devs, 1);
#endif

    ftdi_deinit(&ftdi);
    return widgetList;
}

bool QLCFTDI::open()
{
    if (m_openCount < m_refCount)
        m_openCount++;

    if (isOpen() == true)
        return true;

    if (ftdi_usb_open_desc(&m_handle, QLCFTDI::FTDIVID, QLCFTDI::FTDIPID,
                           name().toLatin1(), serial().toLatin1()) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::openByPID(const int PID)
{
    if (m_openCount < m_refCount)
        m_openCount++;

    if (isOpen() == true)
        return true;

    if (ftdi_usb_open(&m_handle, QLCFTDI::FTDIVID, PID) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::close()
{
    if (m_openCount > 1)
    {
        m_openCount--;
        return true;
    }

    if (ftdi_usb_close(&m_handle) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::isOpen() const
{
    return (m_handle.usb_dev != NULL) ? true : false;
}

bool QLCFTDI::reset()
{
    if (ftdi_usb_reset(&m_handle) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::setLineProperties()
{
    if (ftdi_set_line_property(&m_handle, BITS_8, STOP_BIT_2, NONE) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::setBaudRate()
{
    if (ftdi_set_baudrate(&m_handle, 250000) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::setFlowControl()
{
    if (ftdi_setflowctrl(&m_handle, SIO_DISABLE_FLOW_CTRL) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::clearRts()
{
    if (ftdi_setrts(&m_handle, 0) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::purgeBuffers()
{
    if (ftdi_usb_purge_buffers(&m_handle) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}

bool QLCFTDI::setBreak(bool on)
{
    ftdi_break_type type;
    if (on == true)
        type = BREAK_ON;
    else
        type = BREAK_OFF;

    if (ftdi_set_line_property2(&m_handle, BITS_8, STOP_BIT_2, NONE, type) < 0)
    {
        qWarning() << Q_FUNC_INFO << name() << ftdi_get_error_string(&m_handle);
        return false;
    }
    else
    {
        return true;
    }
}
Ejemplo n.º 9
0
// init serial communication device
bool CPlatform::initSerial(char*cPort, unsigned int baudrate)
{
// Windows
#ifdef WIN32
    if(hSerialPort != INVALID_HANDLE_VALUE) {
        CloseHandle(hSerialPort);
    }

    if(cPort == NULL) {
        cPort = STDSERIALWINDOWS;
    }

    hSerialPort = ::CreateFile(cPort, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

    if(hSerialPort == INVALID_HANDLE_VALUE) {
        CUtil::cout("initSerial: CreateFile() failed.\n", TEXT_ERROR);
        return false;
    }
    else {
        CUtil::cout("initSerial: Connected!\n", TEXT_ERROR);
    }

    COMMTIMEOUTS timeouts;
    GetCommTimeouts(hSerialPort, &timeouts);

    int a = 40; //40
    timeouts.ReadIntervalTimeout = a;
    timeouts.ReadTotalTimeoutMultiplier = 20;
    timeouts.ReadTotalTimeoutConstant = a;
    timeouts.WriteTotalTimeoutMultiplier = 20;
    timeouts.WriteTotalTimeoutConstant = a;

    SetCommTimeouts(hSerialPort, &timeouts);

    DCB dcb = {0};
    dcb.DCBlength = sizeof(DCB);

    if(!::GetCommState(hSerialPort, &dcb) ) {
        CUtil::cout("initSerial: GetCommState() failed.\n", TEXT_ERROR);
    }
    else {
        // 8bit, no parity, one stopbit
        dcb.BaudRate  = baudrate;
        dcb.ByteSize = 8;
        dcb.Parity = 0;
        dcb.StopBits = 0;

        if(!::SetCommState(hSerialPort, &dcb) ) {
            CUtil::cout("initSerial: SetCommState() failed.\n", TEXT_ERROR);
        }
    }

    return true;

// LINUX
#else
    if(cPort == NULL) {
        cPort = (char *)STDSERIALLINUX;
    }

#ifdef USE_FTDI

    /* Still testing */

    unsigned char  buf [1000];

    int a;
    int rsize;
    if (ftdi_init(&ftdi) < 0 )
        return false;
    int ret;

    if((ret = ftdi_usb_open(&ftdi, 0x0403, 0x6001)) < 0) {
        printf( "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdi));
        return EXIT_FAILURE;
    }

    // Read out FTDIChip-ID of R type chips
    if (ftdi.type == TYPE_R) {
        unsigned int chipid;
        printf("ftdi_read_chipid: %d\n", ftdi_read_chipid(&ftdi, &chipid));
        printf("FTDI chipid: %X\n", chipid);
    }
    ftdi_read_data_set_chunksize(&ftdi, 4096);
    ftdi_write_data_set_chunksize(&ftdi, 4096);
    ftdi_set_line_property2(&ftdi, BITS_8, STOP_BIT_1, NONE, BREAK_OFF);
    ftdi_usb_reset (&ftdi);
    printf("baudrate: %d\n", ftdi_set_baudrate (&ftdi, baudrate));
    ftdi_setflowctrl(&ftdi, SIO_DISABLE_FLOW_CTRL);
    ftdi_set_latency_timer(&ftdi, 1);
    ftdi_usb_purge_buffers (&ftdi);

    printf (" Err : %s \n", ftdi.error_str);

    unsigned char nix[150];
    int j = 0;
    int cr;
    for (j = 0; j < 10; j++) {
        cr = ftdi_read_data (&ftdi, &nix[0], 100);
        printf (" %i \n", cr);
    }


    printf ("Typ: %d\n", ftdi.type);
    printf ("usb_read_timeout: %d\n", ftdi.usb_read_timeout);
    printf ("usb_write_timeout: %d\n",
            ftdi.usb_write_timeout);
    printf ("baudrate: %d\n",  ftdi.baudrate);
    printf ("bitbang_enabled: %x\n", ftdi.bitbang_enabled);
    printf ("bitbang_mode: %x\n", ftdi.bitbang_mode);

    return true;

    /* close handles */
    ftdi_usb_close(&ftdi);
    ftdi_deinit(&ftdi);
    return false;
#endif
    //system("stty -F /dev/ttyS0 speed 115200 raw cs8");

    fdSerialPort = open(cPort, O_RDWR | O_NOCTTY | O_NDELAY);// | O_NONBLOCK | O_NDELAY);
    if(fdSerialPort == -1) {
        CUtil::cout("initSerial: open() failed.\n", TEXT_ERROR);
        return false;
    }
    else {
        CUtil::cout("initSerial: Connected.\n", TEXT_ERROR);
    }

    clearLine();


#ifndef SERIALDONTINIT


    //fcntl(fdSerialPort, F_SETFL, FNDELAY);

    struct termios options;

    if(tcgetattr(fdSerialPort, &options) == -1) {
        CUtil::cout("initSerial: tvgetattr() failed.\n", TEXT_ERROR);
    }
    else {
        options.c_iflag = 0;
        options.c_oflag = 0;
        options.c_cflag = 0;
        options.c_lflag = 0;

        cfsetispeed(&options, getSpeedConstant(baudrate) );
        cfsetospeed(&options, getSpeedConstant(baudrate) );
        cfmakeraw(&options);

        options.c_cflag |= (CLOCAL | CREAD);

        // 8 bit, no parity, 1 stopbit
        options.c_cflag |= CS8;

        options.c_cflag &= ~CRTSCTS;
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);


        // wait for 1 characters
        options.c_cc[VMIN]  = 1;
        // timeout 1 seconds
        options.c_cc[VTIME] = 1;

        /*
           cfsetispeed(&options, getSpeedConstant(baudrate));
           cfsetospeed(&options, getSpeedConstant(baudrate));

           //options.c_cflag = 0;
           options.c_cflag |= (getSpeedConstant(baudrate) | CLOCAL | CREAD);

           // 8 bit, no parity, 1 stopbit
           options.c_cflag |= CS8;

           options.c_cflag &= ~CRTSCTS;

           //options.c_lflag = 0;
           options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);*/
        // testing -->

        // <-- testing

        /*
           // baudrate >> 1152000

           struct serial_struct nuts;
           int arby;
           if(ioctl(fdSerialPort, TIOCGSERIAL, &nuts) == -1)
           CUtil::cout( "Serial: ioctl(*,TIOCGSERIAL,*) failed.\n");

           nuts.custom_divisor = nuts.baud_base / 500000;

           if (!(nuts.custom_divisor))
           nuts.custom_divisor = 1;

           arby = nuts.baud_base / nuts.custom_divisor;
           nuts.flags &= ~ASYNC_SPD_MASK;
           nuts.flags |= ASYNC_SPD_CUST;

           if (ioctl(fdSerialPort, TIOCSSERIAL, &nuts) == -1)
           CUtil::cout("Serial: ioctl(*, TIOCSSERIAL, *) failed.\n");
           options.c_cflag |= B38400;

           // wait for 1 characters
           options.c_cc[VMIN]  = 1;
           // timeout 1 seconds
           options.c_cc[VTIME] = 1;
         */


        if(tcsetattr(fdSerialPort, TCSANOW, &options) != 0) {
            CUtil::cout("initSerial: tcsetattr() failed.\n", TEXT_ERROR);
        }
    }
#endif
    return true;

#endif
}
Ejemplo n.º 10
0
int ftdi_device_setup(ftdi_device_t* dev, int baud_rate, int data_bits, int
    stop_bits, ftdi_parity_t parity, ftdi_flow_ctrl_t flow_ctrl, ftdi_break_t
    break_type, double timeout, double latency) {
  struct ftdi_context* libftdi_context = dev->libftdi_context;
  enum ftdi_bits_type libftdi_data_bits;
  enum ftdi_stopbits_type libftdi_stop_bits;
  enum ftdi_parity_type libftdi_parity;
  int libftdi_flow_ctrl;
  enum ftdi_break_type libftdi_break;
  int error;
  
  error_clear(&dev->error);
  
  switch (data_bits) {
    case 7:
      libftdi_data_bits = BITS_7;
      break;
    case 8:
      libftdi_data_bits = BITS_8;
      break;
    default:
      error_setf(&dev->error, FTDI_ERROR_INVALID_DATA_BITS, "%d", data_bits);
      return error_get(&dev->error);
  }
  dev->data_bits = data_bits;

  switch (stop_bits) {
    case 1:
      libftdi_stop_bits = STOP_BIT_1;
      break;
    case 2 :
      libftdi_stop_bits = STOP_BIT_2;
      break;
    case 15:
      libftdi_stop_bits = STOP_BIT_15;
      break;
    default:
      error_setf(&dev->error, FTDI_ERROR_INVALID_STOP_BITS, "%d", stop_bits);
      return error_get(&dev->error);
  }
  dev->stop_bits = stop_bits;

  switch (parity) {
    case ftdi_parity_none:
      libftdi_parity = NONE;
      break;
    case ftdi_parity_odd:
      libftdi_parity = ODD;
      break;
    case ftdi_parity_even:
      libftdi_parity = EVEN;
      break;
    case ftdi_parity_mark:
      libftdi_parity = MARK;
      break;
    case ftdi_parity_space:
      libftdi_parity = SPACE;
      break;
    default:
      error_set(&dev->error, FTDI_ERROR_INVALID_PARITY);
      return error_get(&dev->error);
  }
  dev->parity = parity;

  switch (flow_ctrl) {
    case ftdi_flow_ctrl_off:
      libftdi_flow_ctrl = SIO_DISABLE_FLOW_CTRL;
      break;
    case ftdi_flow_ctrl_xon_xoff:
      libftdi_flow_ctrl = SIO_XON_XOFF_HS;
      break;
    case ftdi_flow_ctrl_rts_cts:
      libftdi_flow_ctrl = SIO_RTS_CTS_HS;
      break;
    case ftdi_flow_ctrl_dtr_dsr:
      libftdi_flow_ctrl = SIO_DTR_DSR_HS;
      break;
    default:
      error_set(&dev->error, FTDI_ERROR_INVALID_FLOW_CTRL);
      return error_get(&dev->error);
  }
  dev->flow_ctrl = flow_ctrl;
  
  switch (break_type) {
    case ftdi_break_off:
      libftdi_break = BREAK_OFF;
      break;
    case ftdi_break_on:
      libftdi_break = BREAK_ON;
      break;
    default:
      error_set(&dev->error, FTDI_ERROR_INVALID_BREAK);
      return error_get(&dev->error);
  }
  dev->break_type = break_type;

  if (ftdi_set_line_property2(libftdi_context, libftdi_data_bits,
      libftdi_stop_bits, libftdi_parity, libftdi_break)) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }
  
  if (ftdi_setflowctrl(libftdi_context, libftdi_flow_ctrl)) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }
  
  error = ftdi_set_baudrate(libftdi_context, baud_rate);
  if (error == -1) {
    error_setf(&dev->error, FTDI_ERROR_INVALID_BAUD_RATE, "%d", baud_rate);
    return error_get(&dev->error);
  }
  dev->baud_rate = baud_rate;
  if (error) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }

  libftdi_context->usb_read_timeout = timeout*1e6;
  libftdi_context->usb_write_timeout = timeout*1e6;
  dev->timeout = timeout;
  
  error = ftdi_set_latency_timer(libftdi_context, latency*1e3);
  if (error == -1) {
    error_setf(&dev->error, FTDI_ERROR_INVALID_LATENCY, "%f", latency);
    return error_get(&dev->error);
  }
  dev->latency = latency;
  if (error) {
    error_setf(&dev->error, FTDI_ERROR_SETUP, "%03:%03", dev->bus,
      dev->address);
    return error_get(&dev->error);
  }
  
  return error_get(&dev->error);
}