int nifalcon_open(falcon_device *dev, unsigned int device_index) { unsigned int falcon_count = 0, device_count = 0, i = 0; char* pcBufLD[MAX_DEVICES + 1]; char cBufLD[MAX_DEVICES][64]; char serial[64]; //If we're not using windows, we can set PID/VID to filter on. I have no idea why this isn't provided in the windows drivers. #ifndef WIN32 FT_SetVIDPID(0x0403, 0xCB48); #endif for(i = 0; i < MAX_DEVICES; i++) { pcBufLD[i] = cBufLD[i]; } if((dev->falcon_status_code = FT_ListDevices(pcBufLD, &device_count, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION)) != FT_OK) return -dev->falcon_status_code; for(i = 0; (i < device_count) && (falcon_count <= device_index); ++i) { if(!strcmp(cBufLD[i], NIFALCON_DESCRIPTION)) falcon_count++; } if(falcon_count == 0) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "no devices connected to system"); if(device_index > falcon_count) nifalcon_error_return(NIFALCON_DEVICE_INDEX_OUT_OF_RANGE_ERROR, "device index out of range"); //Now that we know the index, get the serial number if((dev->falcon_status_code = FT_ListDevices((PVOID)(i-1), serial, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER))) return -dev->falcon_status_code; //Open and reset device using serial number if((dev->falcon_status_code = FT_OpenEx(serial, FT_OPEN_BY_SERIAL_NUMBER, &(dev->falcon))) != FT_OK) return -dev->falcon_status_code; dev->is_open = 1; return FT_OK; }
void TellStick::aquireTellStick() { char *tempSerial = new char[serial().size()+1]; #ifdef _WINDOWS strcpy_s(tempSerial, serial().size()+1, serial().toLocal8Bit()); #else strcpy(tempSerial, serial().toLocal8Bit()); int pid = 0x0C30; if (type() == 2) { pid = 0x0C31; } FT_SetVIDPID(0x1781, pid); #endif FT_STATUS ftStatus = FT_OpenEx(tempSerial, FT_OPEN_BY_SERIAL_NUMBER, &d->ftHandle); delete tempSerial; if (ftStatus != FT_OK) { return; } //open = true; if (type() == 2) { FT_SetBaudRate(d->ftHandle, 115200); } else { FT_SetBaudRate(d->ftHandle, 9600); } FT_SetFlowControl(d->ftHandle, FT_FLOW_NONE, 0, 0); FT_SetTimeouts(d->ftHandle,1000,0); setUpgradeStep(2); QTimer::singleShot(0, this, SLOT(enterBootloader())); }
FT_STATUS OpenDevice(DeviceParams_t* device) { FT_STATUS status; FT_SetVIDPID(device->vid, device->pid); status = FT_OpenEx((PVOID) device->serial, FT_OPEN_BY_SERIAL_NUMBER, &(device->handle)); if(status == FT_OK) { status = FT_SetDataCharacteristics(device->handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); } if(status == FT_OK) { status = FT_SetBaudRate(device->handle, device->baud); } if(status == FT_OK) { // pthread_mutex_init(&device->rxEvent.eMutex, NULL); // pthread_cond_init(&device->rxEvent.eCondVar, NULL); // status = FT_SetEventNotification(device->handle, FT_EVENT_RXCHAR, (PVOID) &(device->rxEvent)); // pthread_create(&(device->thread), NULL, ReadData, (void*)device); } printf ("Open state [%x]\r\n", status); return status; }
// USB 初期化 int InitUSB() { FT_STATUS ftStatus; // USB をオープン ftStatus = FT_OpenEx("USBCONEMU", FT_OPEN_BY_DESCRIPTION, &ftHandle); if (ftStatus != FT_OK) return 1; // Output mode 設定 ftStatus = FT_SetBitMode(ftHandle, 0x00, 0x40); if (ftStatus != FT_OK) return 1; // バッファサイズ設定 ftStatus = FT_SetUSBParameters(ftHandle, MAX_BUFSIZE, 0); if (ftStatus != FT_OK) return 1; // デバイスリセット ftStatus = FT_ResetDevice(ftHandle); if (ftStatus != FT_OK) return 1; // バッファクリア ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); if (ftStatus != FT_OK) return 1; // Time Out 設定 ftStatus = FT_SetTimeouts(ftHandle, NULL, NULL); if (ftStatus != FT_OK) return 1; Sleep(150); return 0; }
/* * Class: kinetic_Kinetic1090Puck * Method: puckOpen * Signature: ()I */ JNIEXPORT jint JNICALL Java_kinetic_Kinetic1090Puck_puckOpen(JNIEnv *env, jobject obj) { // open the device FT_STATUS ftStatus = FT_OpenEx("Kinetic 1090 Puck Beavis 3A B", FT_OPEN_BY_DESCRIPTION, &ftHandle); if (ftStatus != FT_OK) { return -1; } // set the baud rate ftStatus = FT_SetBaudRate(ftHandle, 3000000); if (ftStatus != FT_OK) { return -1; } // set the data characteristics ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ftStatus != FT_OK) { return -1; } // set flow control ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0x11, 0x13); if (ftStatus != FT_OK) { return -1; } // set read and write timeouts ftStatus = FT_SetTimeouts(ftHandle, 5000, 1000); if (ftStatus != FT_OK) { return -1; } return 0; }
FT_STATUS FTDISerial::Connect(DWORD baudRate) { FT_STATUS status; status = FT_OpenEx("Nova Power Input Module", FT_OPEN_BY_DESCRIPTION, &m_handle); if(status == FT_OK) { m_isConnected = true; FT_SetBaudRate(m_handle, baudRate); } return status; }
static BOOL OpenUSB (void) { baseport = 0; ftStatus = FT_OpenEx("USB CopyNES A",FT_OPEN_BY_DESCRIPTION,&ftHandleA); // open data bus if (ftStatus != FT_OK) { // failure - one or both of the devices has not been opened MessageBox(topHWnd, "USB Error: Failed to open CopyNES data bus!", "OpenUSB", MB_OK | MB_ICONERROR); return FALSE; } ftStatus = FT_OpenEx("USB CopyNES B",FT_OPEN_BY_DESCRIPTION,&ftHandleB); //open control bus if (ftStatus != FT_OK) { // failure - one or both of the devices has not been opened MessageBox(topHWnd, "USB Error: Failed to open CopyNES control bus!", "OpenUSB", MB_OK | MB_ICONERROR); return FALSE; } return TRUE; }
bool D2xxSerial::open(string who, int baudRate) { FT_STATUS err = FT_OpenEx((void*)who.c_str(), FT_OPEN_BY_SERIAL_NUMBER, &handle); if (err != FT_OK) { // FT_OpenEx failed printf("Error connecting to '%s': %s\n", who.c_str(), getError(err)); return false; } setBaudRate(baudRate); return true; }
/** Open a power meter probe by a given serial number \param pm a pointer to a pm_context \param serial the serial number \retval 0 - all fine \retval -1 - open failed (wrong serial number?) \retval -2 - setting baudrate failed \retval -3 - setting data characteristics failed \retval -4 - setting flow control failed \retval -5 - setting timeouts failed \retval -6 - purging buffers failed \retval -7 - resetting device failed */ PM600X_EXPORT int pm_open(struct pm_context *pm, unsigned long serial) { FT_STATUS ftstat; // convert the serial into a 8 digit string with leading zeroes char serial_string[12]; snprintf(serial_string, 12, "%06lu", serial); // open the device by a given serial number ftstat = FT_OpenEx((void *)serial_string, FT_OPEN_BY_SERIAL_NUMBER, &pm->handle); if (ftstat != FT_OK) pm_error_return(-1, "open failed (wrong serial number?)"); // get the device info to obtain the power meter type unsigned long id; ftstat = FT_GetDeviceInfo(pm->handle, NULL, &id, NULL, NULL, NULL); if (ftstat != FT_OK) pm_error_return(-8, "can not retrieve device info!"); pm->type = id; // set baud rate to 115200 ftstat = FT_SetBaudRate(pm->handle, FT_BAUD_115200); if (ftstat != FT_OK) pm_error_return(-2, "setting baudrate failed"); // set data characteristics to 8n1 ftstat = FT_SetDataCharacteristics(pm->handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ftstat != FT_OK) pm_error_return(-3, "setting data characteristics failed"); // set flow control to NONE ftstat = FT_SetFlowControl(pm->handle, FT_FLOW_NONE, 0, 0); if (ftstat != FT_OK) pm_error_return(-4, "setting flow control failed"); // set timeouts to 1 second for writes and 3.5 seconds for reads. This should be enough when measuring with averaging == 10000 ftstat = FT_SetTimeouts(pm->handle, 3500, 1000); if (ftstat != FT_OK) pm_error_return(-5, "setting timeouts failed"); // purge bufffers just in case ftstat = FT_Purge(pm->handle, FT_PURGE_RX | FT_PURGE_TX); if (ftstat != FT_OK) pm_error_return(-6, "purging buffers failed"); // reset the device via *RST return pm_reset(pm); }
bool CUSB::Open(char serialNumber[]) { if (isUSB_open) { ftStatus = FT_DEVICE_NOT_OPENED; return false; } m_posR = m_sizeR = m_posW = 0; ftStatus = FT_OpenEx(serialNumber, FT_OPEN_BY_SERIAL_NUMBER, &ftHandle); if (ftStatus != FT_OK) return false; ftStatus = FT_SetBitMode(ftHandle, 0xFF, 0x40); if (ftStatus != FT_OK) return false; FT_SetTimeouts(ftHandle,1000,1000); isUSB_open = true; return true; }
int gecko_opendevice() { // Open by Serial Number status = FT_OpenEx("GECKUSB0", FT_OPEN_BY_SERIAL_NUMBER, &fthandle); if(status != FT_OK){ eprintf("Error: Couldn't connect to USB Gecko. Please check Installation\n"); return 0; } // Reset the Device status = FT_ResetDevice(fthandle); if(status != FT_OK){ eprintf("Error: Couldnt Reset Device %d\n",status); status = FT_Close(fthandle); return 0; } // Set a 3 second timeout for this example status = FT_SetTimeouts(fthandle,3000,3000); if(status != FT_OK){ eprintf("Error: Timeouts failed to set %d\n",status); status = FT_Close(fthandle); return 0; } // Purge RX buffer status = FT_Purge(fthandle,FT_PURGE_RX); if(status != FT_OK){ eprintf("Error: Problem clearing buffers %d\n",status); status = FT_Close(fthandle); return 0; } // Purge TX buffer status = FT_Purge(fthandle,FT_PURGE_TX); if(status != FT_OK){ eprintf("Error: Problem clearing buffers %d\n",status); status = FT_Close(fthandle); return 0; } // Set packet size in bytes - 65536 packet is maximum packet size (USB 2.0) status = FT_SetUSBParameters(fthandle,65536,0); if(status != FT_OK){ eprintf("Error: Couldnt Set USB Parameters %d\n",status); status = FT_Close(fthandle); return 0; } // take breath sleep(1); return 1; }
bool CUsb3003DF2ETInterface::OpenDevice(void) { FT_STATUS ftStatus; int baudrate = 921600; //sets serial VID/PID for a Standard Device NOTE: This is for legacy purposes only. This can be ommitted. ftStatus = FT_SetVIDPID(m_uiVid, m_uiPid); if (ftStatus != FT_OK) {FTDI_Error((char *)"FT_SetVIDPID", ftStatus ); return false; } ftStatus = FT_OpenEx((PVOID)m_szDeviceSerial, FT_OPEN_BY_SERIAL_NUMBER, &m_FtdiHandle); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_OpenEx", ftStatus ); return false; } CTimePoint::TaskSleepFor(50); FT_Purge(m_FtdiHandle, FT_PURGE_RX | FT_PURGE_TX ); CTimePoint::TaskSleepFor(50); ftStatus = FT_SetDataCharacteristics(m_FtdiHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if ( ftStatus != FT_OK ) { FTDI_Error((char *)"FT_SetDataCharacteristics", ftStatus ); return false; } ftStatus = FT_SetFlowControl(m_FtdiHandle, FT_FLOW_RTS_CTS, 0x11, 0x13); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetFlowControl", ftStatus ); return false; } ftStatus = FT_SetRts (m_FtdiHandle); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetRts", ftStatus ); return false; } // for DF2ET-3003 interface pull DTR low to take AMBE3003 out of reset. ftStatus = FT_SetDtr( m_FtdiHandle ); CTimePoint::TaskSleepFor(50); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetDtr", ftStatus); return false; } ftStatus = FT_SetBaudRate(m_FtdiHandle, baudrate ); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetBaudRate", ftStatus ); return false; } ftStatus = FT_SetLatencyTimer(m_FtdiHandle, 4); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetLatencyTimer", ftStatus ); return false; } ftStatus = FT_SetUSBParameters(m_FtdiHandle, USB3XXX_MAXPACKETSIZE, 0); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetUSBParameters", ftStatus ); return false; } ftStatus = FT_SetTimeouts(m_FtdiHandle, 200, 200 ); if (ftStatus != FT_OK) { FTDI_Error((char *)"FT_SetTimeouts", ftStatus ); return false; } // done return true; }
bool CMmcUsbHndlBase::Open(unsigned int ulLocationId, CStdString p_SerialNumber) { if( !AreLibraryFunctionsLoaded() ) { perror("Library not loaded"); return false; } FT_STATUS ftStatus = FT_OpenEx((void*)p_SerialNumber.GetBuffer(), FT_OPEN_BY_SERIAL_NUMBER, &m_Handle); p_SerialNumber.ReleaseBuffer(); if( ftStatus != FT_OK ) { std::string errormsg = GetFtStatusDescription(ftStatus); errormsg += ":FT_OpenEx"; perror(errormsg.c_str()); } return (FT_OK == ftStatus); }
void Dynamixel::connect(){ // Initiallizes the motor and generates the ftHandleDYNA FT_STATUS ftStatus = FT_OpenEx(serialNumber_,FT_OPEN_BY_SERIAL_NUMBER,&ftHandleDYNA_); if (ftStatus != FT_OK) { qDebug()<<"failed to open dyna"; emit failedToOpen(); Sleep(2000); return; } //Initialize the USB2Dynamixel initialized_=DYNA_initialize(ftHandleDYNA_); if(!initialized_){ qDebug()<<"failed to initialize"; emit failedToOpen(); return; } Sleep(50); qDebug()<< "Opend Dynamixel!"; }
static FT_HANDLE OpenUSBDevice( int nType, int nId ) { FT_HANDLE hndl = 0; unsigned i; DWORD dwDevs; FT_DEVICE_LIST_INFO_NODE *pNodes = 0; if( FT_CreateDeviceInfoList( &dwDevs) == FT_OK ) { pNodes = (FT_DEVICE_LIST_INFO_NODE *)calloc( sizeof(FT_DEVICE_LIST_INFO_NODE) , dwDevs ); if( FT_GetDeviceInfoList( pNodes, &dwDevs ) == FT_OK ) { for( i = 0; i < dwDevs; i++ ) { if( pNodes[i].Type == nType && pNodes[i].ID == nId ) { if( FT_OpenEx( pNodes[i].Description, FT_OPEN_BY_DESCRIPTION, &hndl ) != FT_OK ) { hndl = 0; goto cleanup; } /* if */ if( FT_SetLatencyTimer( hndl, s_nUsbLatency ) != FT_OK || FT_SetTimeouts( hndl, s_nReadTimeout, s_nWriteTimeout ) != FT_OK ) { FT_Close( s_hndl ); hndl = 0; goto cleanup; } break; } /* if */ } /* for */ } /* if */ } /* if */ cleanup: if( pNodes ) free( pNodes ); return hndl; }
void serial_init (char *port, int baud) { char *chipname; switch (board_type) { case BT_powermeter: chipname = "FT232R PowerMeter"; break; case BT_powermeterproto: chipname = "FT232R PowerMeter"; break; default: chipname = "FT232R - R8C"; break; } Fail (FT_OpenEx (chipname, FT_OPEN_BY_DESCRIPTION, &handle)); if (verbose > 2) printf("handle: 0x%x\n", (int)handle); atexit (serial_close); ProgramMode (1); Reset (1); /* >3 mSec reset pulse */ usleep(3*1000); Reset (0); Fail (FT_SetBaudRate (handle, 9600)); Fail (FT_SetDataCharacteristics (handle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE)); FT_SetFlowControl (handle, FT_FLOW_NONE, 0, 0); /* 3 mSec delay to let board "wake up" after reset */ usleep(3*1000); }
bool CKMotionIO::RequestedDeviceAvail(char *Reason) { FT_STATUS ftStatus; FT_DEVICE ftDevice; DWORD deviceID; int i, numDevs, list[MAX_BOARDS]; char SerialNumber[16]; char Description[64]; Mutex->Lock(); // fill the list with -1 because driver // leaves the entries for open drivers unchanged for (i=0; i<16; i++) list[i]=-1; numDevs=-1; ftStatus = FT_ListDevices(list,&numDevs,FT_LIST_ALL|FT_OPEN_BY_LOCATION); if (numDevs==0) { Mutex->Unlock(); if (Reason) strcpy(Reason,"No KMotion devices available"); return false; } if (numDevs < 1 || numDevs >= MAX_BOARDS) { Mutex->Unlock(); if (Reason) strcpy(Reason,"No KMotion devices available"); return false; } // go through the list and remove any non-dynomotion boards for (i=0; i<numDevs; i++) { if (list[i] != -1) { ftStatus = FT_OpenEx((void *)list[i], FT_OPEN_BY_LOCATION, &ftHandle); if(ftStatus != FT_OK) { // FT_Open failed list[i] = -1; // mark as unusable } else { ftStatus = FT_GetDeviceInfo( ftHandle, &ftDevice, &deviceID, SerialNumber, Description, NULL ); if (ftStatus == FT_OK) { FT_Close(ftHandle); if (strstr(Description,"KFLOP")== NULL && strstr(Description,"KMotion")== NULL && strstr(Description,"Dynomotion")== NULL) { // remove it from the list for (int k=i+1; k<numDevs; k++) list[k-1] = list[k]; // shift up numDevs--; i--; // redo this slot since it was deleted and things shifted up } } } } } // if USB Location is undefined select the first from // the list that is not already taken if (!BoardIDAssigned) { for (i=0; i<numDevs && !BoardIDAssigned; i++) { if (list[i] != -1) { int k; // make sure nobody is already using this one for (k=0; k<MAX_BOARDS; k++) { if (list[i]==KMotionLocal.KMotionIO[k].USB_Loc_ID) break; } if (k==MAX_BOARDS) { BoardIDAssigned=true; USB_Loc_ID=list[i]; // assign it } } } if (!BoardIDAssigned) { Mutex->Unlock(); if (Reason) strcpy(Reason,"No KMotion devices available"); return false; } } // user wants a specific usb location // so see if it is available for (i=0; i<numDevs; i++) { if (list[i]==USB_Loc_ID) break; } if (i==numDevs) { Mutex->Unlock(); if (Reason) sprintf(Reason,"KMotion not found on USB Location %08X\r\r" "Unable to open device",USB_Loc_ID); return false; } Mutex->Unlock(); return true; }
mraa_result_t mraa_ftdi_ft4222_init() { mraa_result_t mraaStatus = MRAA_SUCCESS; FT_STATUS ftStatus; FT_DEVICE_LIST_INFO_NODE* devInfo = NULL; DWORD numDevs = 0; int i; int retCode = 0; ftStatus = FT_CreateDeviceInfoList(&numDevs); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_CreateDeviceInfoList failed: error code %d\n", ftStatus); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } if (numDevs == 0) { syslog(LOG_ERR, "No FT4222 devices connected.\n"); goto init_exit; } devInfo = calloc((size_t) numDevs, sizeof(FT_DEVICE_LIST_INFO_NODE)); if (devInfo == NULL) { syslog(LOG_ERR, "FT4222 allocation failure.\n"); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_GetDeviceInfoList failed (error code %d)\n", (int) ftStatus); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } /* FT4222_Version ft4222Version; FT4222_STATUS ft4222Status = FT4222_GetVersion(ftHandle, &ft4222Version); if (FT4222_OK == ft4222Status){ syslog(LOG_NOTICE, "FT4222_GetVersion %08X %08X\n", ft4222Version.chipVersion, ft4222Version.dllVersion); } else syslog(LOG_ERR, "FT4222_GetVersion failed with code %d", ft4222Status); */ syslog(LOG_NOTICE, "FT_GetDeviceInfoList returned %d devices\n", numDevs); DWORD locationId = 0; for (i = 0; i < numDevs && locationId == 0; ++i) { // printf("%d: type = %d, location = %d\n", i, devInfo[i].Type, devInfo[i].LocId); if (devInfo[i].Type == FT_DEVICE_4222H_0 || devInfo[i].Type == FT_DEVICE_4222H_3) locationId = devInfo[i].LocId; } if (locationId == 0) { syslog(LOG_ERR, "FT_GetDeviceInfoList contains no valid devices\n"); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } ftStatus = FT_OpenEx((PVOID)(uintptr_t) locationId, FT_OPEN_BY_LOCATION, &ftHandle); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int) ftStatus); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } // Tell the FT4222 to be an I2C Master. FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandle, bus_speed); if (FT4222_OK != ft4222Status) { syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } // Reset the I2CM registers to a known state. ft4222Status = FT4222_I2CMaster_Reset(ftHandle); if (FT4222_OK != ft4222Status) { syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } init_exit: if (devInfo != NULL) ; free(devInfo); if (mraaStatus == MRAA_SUCCESS) syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n"); return mraaStatus; }
mraa_result_t mraa_ftdi_ft4222_init() { mraa_result_t mraaStatus = MRAA_ERROR_NO_RESOURCES; FT_DEVICE_LIST_INFO_NODE* devInfo = NULL; FT_STATUS ftStatus; DWORD numDevs = 0; int i; int retCode = 0; ftStatus = FT_CreateDeviceInfoList(&numDevs); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_CreateDeviceInfoList failed: error code %d\n", ftStatus); goto init_exit; } devInfo = calloc((size_t) numDevs, sizeof(FT_DEVICE_LIST_INFO_NODE)); if (devInfo == NULL) { syslog(LOG_ERR, "FT4222 allocation failure.\n"); goto init_exit; } ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs); syslog(LOG_NOTICE, "FT_GetDeviceInfoList returned %d devices\n", numDevs); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_GetDeviceInfoList failed (error code %d)\n", (int) ftStatus); goto init_exit; } if (numDevs < 2) { syslog(LOG_ERR, "No FT4222 devices connected.\n"); goto init_exit; } if(numDevs > 2) { syslog(LOG_ERR, "CNFMODE not supported. Valid modes are 0 or 3.\n"); goto init_exit; } // FIXME: Assumes just one physical FTDI device present DWORD locationIdI2c = 0; DWORD locationIdGpio = 0; if (devInfo[0].Type == FT_DEVICE_4222H_0) locationIdI2c = devInfo[0].LocId; if (devInfo[1].Type == FT_DEVICE_4222H_0) locationIdGpio = devInfo[1].LocId; if (locationIdI2c == 0) { syslog(LOG_ERR, "FT_GetDeviceInfoList contains no I2C controllers\n"); goto init_exit; } if (locationIdGpio == 0) { syslog(LOG_ERR, "FT_GetDeviceInfoList contains no GPIO controllers\n"); goto init_exit; } ftStatus = FT_OpenEx((PVOID)(uintptr_t) locationIdI2c, FT_OPEN_BY_LOCATION, &ftHandleI2c); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int) ftStatus); goto init_exit; } ftStatus = FT_OpenEx((PVOID)(uintptr_t) locationIdGpio, FT_OPEN_BY_LOCATION, &ftHandleGpio); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT_OpenEx failed (error %d)\n", (int) ftStatus); goto init_exit; } FT4222_SetSuspendOut(ftHandleGpio, 0); FT4222_SetWakeUpInterrupt(ftHandleGpio, 0); ftStatus = FT4222_GPIO_Init(ftHandleGpio, pinDirection); if (ftStatus != FT_OK) { syslog(LOG_ERR, "FT4222_GPIO_Init failed (error %d)\n", (int) ftStatus); mraaStatus = MRAA_ERROR_NO_RESOURCES; goto init_exit; } // Tell the FT4222 to be an I2C Master by default on init. FT4222_STATUS ft4222Status = FT4222_I2CMaster_Init(ftHandleI2c, bus_speed); if (FT4222_OK != ft4222Status) { syslog(LOG_ERR, "FT4222_I2CMaster_Init failed (error %d)!\n", ft4222Status); goto init_exit; } ft4222Status = FT4222_I2CMaster_Reset(ftHandleI2c); if (FT4222_OK != ft4222Status) { syslog(LOG_ERR, "FT4222_I2CMaster_Reset failed (error %d)!\n", ft4222Status); goto init_exit; } mraaStatus = MRAA_SUCCESS; init_exit: if (devInfo != NULL) free(devInfo); if (mraaStatus == MRAA_SUCCESS) syslog(LOG_NOTICE, "mraa_ftdi_ft4222_init completed successfully\n"); return mraaStatus; }
int main() { unsigned char cBufWrite[BUF_SIZE]; unsigned char * pcBufRead = NULL; char * pcBufLD[MAX_DEVICES + 1]; char cBufLD[MAX_DEVICES][64]; DWORD dwRxSize = 0, dwRxSizeTemp = 0; DWORD dwBytesWritten, dwBytesRead; FT_STATUS ftStatus; FT_HANDLE ftHandle[MAX_DEVICES]; int iNumDevs = 0; int i, j; int iDevicesOpen = 0; printf("Program start.\n"); for (i = 0; i < MAX_DEVICES; i++) { pcBufLD[i] = cBufLD[i]; } pcBufLD[MAX_DEVICES] = NULL; FT_SetVIDPID(1027, 59530); // use our VID and PID ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER); if (ftStatus != FT_OK) { printf("Error: FT_ListDevices(%d)\n", (int) ftStatus); return 1; } for (i = 0; ((i < MAX_DEVICES) && (i < iNumDevs)); i++) { printf("Device %d Serial Number - %s\n", i, cBufLD[i]); } for (j = 0; j < BUF_SIZE; j++) { cBufWrite[j] = j; } printf("Number of devices: %d.\n", iNumDevs); for (i = 0; ((i < MAX_DEVICES) && (i < iNumDevs)); i++) { /* Setup */ if ((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK) { /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("Error FT_OpenEx(%d), device %d\n", (int) ftStatus, i); printf("Use lsmod to check if ftdi_sio (and usbserial) are present.\n"); printf("If so, unload them using rmmod, as they conflict with ftd2xx.\n"); return 1; } printf("Opened device %s\n", cBufLD[i]); iDevicesOpen++; if ((ftStatus = FT_SetBaudRate(ftHandle[i], 9600)) != FT_OK) { printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", (int) ftStatus, cBufLD[i]); break; } // printf("Calling FT_Write with this write-buffer:\n"); // dumpBuffer(cBufWrite, BUF_SIZE, 1); /* Write */ /* ftStatus = FT_Write(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesWritten); if (ftStatus != FT_OK) { printf("Error FT_Write(%d)\n", (int)ftStatus); break; } if (dwBytesWritten != (DWORD)BUF_SIZE) { printf("FT_Write only wrote %d (of %d) bytes\n", (int)dwBytesWritten, BUF_SIZE); break; } sleep(1); */ /* Read */ // load buffer dwRxSize = 0; dwRxSizeTemp = 0; printf("Fill Buffer (Size: %d)...\n", BUF_SIZE); while ((dwRxSize < BUF_SIZE) && (ftStatus == FT_OK)) { ftStatus = FT_GetQueueStatus(ftHandle[i], &dwRxSize); // show progress if (dwRxSize > dwRxSizeTemp) { dwRxSizeTemp = dwRxSize; printf("Buffer %d/%d\n", dwRxSize, BUF_SIZE); } } // buffer is filled if (ftStatus == FT_OK) { // preset buffer pcBufRead = realloc(pcBufRead, dwRxSize); memset(pcBufRead, 0xFF, dwRxSize); /* printf("Calling FT_Read with this read-buffer (prefilled, initializied):\n"); dumpBuffer(pcBufRead, dwRxSize, 1); */ ftStatus = FT_Read(ftHandle[i], pcBufRead, dwRxSize, &dwBytesRead); if (ftStatus != FT_OK) { printf("Error FT_Read(%d)\n", (int) ftStatus); break; } if (dwBytesRead != dwRxSize) { printf("FT_Read only read %d (of %d) bytes\n", (int) dwBytesRead, (int) dwRxSize); break; } printf("FT_Read read %d bytes. Read-buffer is now:\n", (int) dwBytesRead); printf("list in hex"); dumpBuffer(pcBufRead, (int) dwBytesRead, 1); printf("list in hex"); dumpBuffer(pcBufRead, (int) dwBytesRead, 2); printf("list in dec"); dumpBuffer(pcBufRead, (int) dwBytesRead, 3); printf("list in combined"); dumpBuffer(pcBufRead, (int) dwBytesRead, 4); /* if (0 != memcmp(cBufWrite, pcBufRead, BUF_SIZE)) { printf("Error: read-buffer does not match write-buffer.\n"); break; } printf("%s test passed.\n", cBufLD[i]); */ } else { printf("Error FT_GetQueueStatus(%d)\n", (int) ftStatus); } } iDevicesOpen = i; /* Cleanup */ for (i = 0; i < iDevicesOpen; i++) { FT_Close(ftHandle[i]); printf("Closed device %s\n", cBufLD[i]); } if (pcBufRead) free(pcBufRead); printf("Program end.\n"); return 0; }
int main() { char cBufWrite[BUF_SIZE]; char * pcBufRead = NULL; char * pcBufLD[MAX_DEVICES + 1]; char cBufLD[MAX_DEVICES][64]; DWORD dwRxSize = 0; DWORD dwBytesWritten, dwBytesRead; FT_STATUS ftStatus; FT_HANDLE ftHandle[MAX_DEVICES]; int iNumDevs = 0; int i, j; int iDevicesOpen; FT_DEVICE_LIST_INFO_NODE *devInfo; // // create the device information list // ftStatus = FT_CreateDeviceInfoList(&iNumDevs); if (ftStatus == FT_OK) { printf("Number of devices is %d\n",iNumDevs); } // // allocate storage for list based on numDevs // devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*iNumDevs); // // get the device information list // ftStatus = FT_GetDeviceInfoList(devInfo,&iNumDevs); if (ftStatus == FT_OK) { for (i = 0; i < iNumDevs; i++) { printf("Dev %d:\n",i); printf(" Flags=0x%x\n",devInfo[i].Flags); printf(" Type=0x%x\n",devInfo[i].Type); printf(" ID=0x%x\n",devInfo[i].ID); printf(" LocId=0x%x\n",devInfo[i].LocId); printf(" SerialNumber=%s\n",devInfo[i].SerialNumber); printf(" Description=%s\n",devInfo[i].Description); printf(" ftHandle=0x%x\n",devInfo[i].ftHandle); } } for(i = 0; i < MAX_DEVICES; i++) { pcBufLD[i] = cBufLD[i]; } pcBufLD[MAX_DEVICES] = NULL; ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER); // ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); if(ftStatus != FT_OK) { printf("Error: FT_ListDevices(%d)\n", ftStatus); return 1; } for(j = 0; j < BUF_SIZE; j++) { cBufWrite[j] = j; } for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ); i++) { printf("Device %d Serial Number - %s\n", i, cBufLD[i]); } for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ) ; i++) { /* Setup */ if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK){ // if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_DESCRIPTION, &ftHandle[i])) != FT_OK){ /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("Error FT_OpenEx(%d), device\n", ftStatus, i); return 1; } // FT_SetDeadmanTimeout(ftHandle[i], 10000); iDevicesOpen++; if((ftStatus = FT_SetBaudRate(ftHandle[i], 9600)) != FT_OK) { printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", ftStatus, cBufLD[i]); break; } for(j = 0; j < BUF_SIZE; j++) { printf("cBufWrite[%d] = 0x%02X\n", j, cBufWrite[j]); } /* Write */ if((ftStatus = FT_Write(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesWritten)) != FT_OK) { printf("Error FT_Write(%d)\n", ftStatus); break; } sleep(1); /* Read */ dwRxSize = 0; while ((dwRxSize < BUF_SIZE) && (ftStatus == FT_OK)) { ftStatus = FT_GetQueueStatus(ftHandle[i], &dwRxSize); } if(ftStatus == FT_OK) { pcBufRead = (char*)realloc(pcBufRead, dwRxSize); memset(pcBufRead, 0xFF, dwRxSize); for(j = 0; j < dwRxSize; j++) { printf("b4 pcBufRead[%d] = 0x%02X\n", j, pcBufRead[j]); } if((ftStatus = FT_Read(ftHandle[i], pcBufRead, dwRxSize, &dwBytesRead)) != FT_OK) { printf("Error FT_Read(%d)\n", ftStatus); } else { printf("FT_Read = %d\n", dwBytesRead); for(j = 0; j < dwBytesRead; j++) { printf("aftr pcBufRead[%d] = 0x%02X\n", j, pcBufRead[j]); } } } else { printf("Error FT_GetQueueStatus(%d)\n", ftStatus); } } iDevicesOpen = i; /* Cleanup */ for(i = 0; i < iDevicesOpen; i++) { FT_Close(ftHandle[i]); printf("Closed device %s\n", cBufLD[i]); } if(pcBufRead) free(pcBufRead); return 0; }
int dev_open_uart (int n_dev_indx, FT_HANDLE *ph_device) { FT_STATUS ft_status; DWORD dw_num_devs; LONG devLocation; ft_status = FT_ListDevices(&dw_num_devs, NULL, FT_LIST_NUMBER_ONLY); if (ft_status != FT_OK) return FALSE; if (dw_num_devs == 0){ // No devices were found return FALSE; } ft_status = FT_ListDevices((void*)n_dev_indx, &devLocation, FT_LIST_BY_INDEX | FT_OPEN_BY_LOCATION); if (ft_status != FT_OK) { return FALSE; } ft_status |= FT_ListDevices((void*)n_dev_indx, &devDescriptor, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION); ft_status |= FT_ListDevices((void*)n_dev_indx, &devSerial, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); if (ft_status != FT_OK){ return FALSE; } #define FT_Classic 0 #if FT_Classic ft_status |= FT_OpenEx((void*)devLocation, FT_OPEN_BY_LOCATION, ph_device); ft_status |= FT_SetTimeouts(*ph_device, 500, 500); ft_status |= FT_SetLatencyTimer(*ph_device, 2); // Divisor selection // BAUD = 3000000 / Divisor // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875) // Divisor = 24 ==> Baud 125000 ft_status |= FT_SetDivisor(*ph_device, 3000000 / 125000); // Set UART format 8N1 ft_status |= FT_SetDataCharacteristics(*ph_device, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ft_status != FT_OK){ return FALSE; } // Just in case FT_Purge(*ph_device, FT_PURGE_TX | FT_PURGE_RX); #else // Open a device for overlapped I/O using its serial number *ph_device = FT_W32_CreateFile( (LPCTSTR)devLocation, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FT_OPEN_BY_LOCATION, 0); if (*ph_device == INVALID_HANDLE_VALUE) { // FT_W32_CreateDevice failed return FALSE; } // ---------------------------------------- // --- Set comm parameters // ---------------------------------------- FTDCB ftDCB; FTTIMEOUTS ftTimeouts; FTCOMSTAT ftPortStatus; DWORD dw_port_error; if (!FT_W32_GetCommState(*ph_device, &ftDCB)) { return FALSE; } // Divisor selection // BAUD = 3000000 / Divisor // Divisor = (N + 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875) // Divisor = 24 ==> Baud 125000 ftDCB.BaudRate = 38400; ftDCB.fBinary = TRUE; /* Binary Mode (skip EOF check) */ ftDCB.fParity = FALSE; /* Enable parity checking */ ftDCB.fOutxCtsFlow = FALSE; /* CTS handshaking on output */ ftDCB.fOutxDsrFlow = FALSE; /* DSR handshaking on output */ ftDCB.fDtrControl = DTR_CONTROL_DISABLE; /* DTR Flow control */ ftDCB.fTXContinueOnXoff = FALSE; ftDCB.fErrorChar = FALSE; // enable error replacement ftDCB.fNull = FALSE; // enable null stripping ftDCB.fRtsControl = RTS_CONTROL_DISABLE; // RTS flow control ftDCB.fAbortOnError = TRUE; // abort reads/writes on error ftDCB.fOutX = FALSE; /* Enable output X-ON/X-OFF */ ftDCB.fInX = FALSE; /* Enable input X-ON/X-OFF */ ftDCB.fNull = FALSE; /* Enable Null stripping */ ftDCB.fRtsControl = RTS_CONTROL_DISABLE; /* Rts Flow control */ ftDCB.fAbortOnError = TRUE; /* Abort all reads and writes on Error */ // 8N1 ftDCB.ByteSize = 8; /* Number of bits/byte, 4-8 */ ftDCB.Parity = NOPARITY; /* 0-4=None,Odd,Even,Mark,Space */ ftDCB.StopBits = ONESTOPBIT; /* 0,1,2 = 1, 1.5, 2 */ if (!FT_W32_SetCommState(*ph_device, &ftDCB)) { return FALSE; } FT_W32_GetCommState(*ph_device, &ftDCB); // Set serial port Timeout values FT_W32_GetCommTimeouts(*ph_device, &ftTimeouts); ftTimeouts.ReadIntervalTimeout = 0; ftTimeouts.ReadTotalTimeoutMultiplier = 0; ftTimeouts.ReadTotalTimeoutConstant = 200; ftTimeouts.WriteTotalTimeoutConstant = 0; ftTimeouts.WriteTotalTimeoutMultiplier = 0; FT_W32_SetCommTimeouts(*ph_device, &ftTimeouts); FT_W32_ClearCommError(*ph_device, &dw_port_error, &ftPortStatus); FT_W32_PurgeComm(*ph_device, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_RXABORT | PURGE_TXABORT); #endif // End of W32 device init return TRUE; }
ULONG DapiOpenModuleGetFT_HANDLE(ULONG moduleID, ULONG moduleNr, ULONG BusType, ULONG retry_max, ULONG subdevice, FT_HANDLE* handle) { unsigned long NumDevices = 0; ULONG LocID = 0, ChipID = 0; FT_STATUS dStatus; // FT_HANDLE ftHandle; FT_STATUS ftStatus; DWORD iNumDevs; long dwLoc; DWORD EEUA_Size; ULONG sn; char text_dev_nr[10]; unsigned char buffer[64]; DWORD BytesRead; ULONG i; FT_DEVICE_LIST_INFO_NODE *devInfo; ULONG check_device; char msg[200]; DWORD dwDriverVer; FT_STATUS status; ULONG ModuleLocation; ULONG baudrate; ULONG ok; if(subdevice==0) { sprintf(text_dev_nr , "DT00%4dA", (int) moduleID); } else if(subdevice==1) { sprintf(text_dev_nr , "DT00%4dB", (int) moduleID); } else { // Ohne A oder B sprintf(text_dev_nr , "DT00%4d", (int) moduleID); } // Linux KEIN A oder B //sprintf(text_dev_nr , "DT00%4d", (int) moduleID); sprintf(msg,"|DELIB|---------------OpenModuleGetFT_HANDLE(0x%x, 0x%x, 0x%x, 0x%x) Normal\n", (unsigned int) moduleID, (unsigned int) moduleNr, (unsigned int) retry_max, (unsigned int) subdevice); debug_print(msg); for(i=4;i!=8;++i) { if(text_dev_nr[i]<'0' || text_dev_nr[i]>'9') text_dev_nr[i]='0'; } text_dev_nr[9]=0; sprintf(msg, "|DELIB|OpenModuleGetFT_HANDLE - Searching for the following S/N = %s\n", text_dev_nr); debug_print(msg); // ---------------------------------------------------- // 6.12.2011 Geändert auf -1 ModuleLocation = -1; // ModuleLocation = 0; ftStatus = FT_OpenEx(text_dev_nr,FT_OPEN_BY_SERIAL_NUMBER, handle); if (ftStatus == FT_OK) { sprintf(msg, "|DELIB|OpenModuleGetFT_HANDLE - FTDI-Handle = %x\n", (unsigned int) *handle); debug_print(msg); } else { sprintf(msg, "|DELIB|OpenModuleGetFT_HANDLE - FTDI OPEN ERROR \n"); debug_print(msg); *handle = (FT_HANDLE) -1; } if(*handle != ((FT_HANDLE) -1)) { if((BusType == bus_SeriellFTDI) || (BusType == bus_FTDI_SINGLE)) { // dStatus = FT_SetTimeouts(ftHandle, 100*5, 100*5); sprintf(msg,"|DELIB|OpenModuleGetFT_HANDLE - Timeout ok\n"); debug_print(msg); if((moduleID != USB_MINI_STICK) && (moduleID != USB_LOGI_18) && (moduleID != USB_SPI_MON) && (moduleID != USB_WATCHDOG) && (moduleID != USB_OPTOIN_8)) { baudrate = 115200; } else { baudrate = (unsigned long) (250000/1.666); // 150 KBaud beim R8C1A } // printf("write ftdic = 0x%x\n", handle); // ftStatus=FT_SetBaudRate(ftHandle, baudrate); if(ftdi_set_baudrate (*handle, baudrate) != 0) // if (ftStatus != FT_OK) { sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetBaudRate"); debug_print(msg); //return -1; } else { sprintf(msg,"|DELIB|DapiHandle * Serial Baudrate = %d", (unsigned int) baudrate); debug_print(msg); } //printf("XX\n"); // ---------------------------- // ftStatus=FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ftStatus != FT_OK) { sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetDataCharacteristics"); debug_print(msg); //return -1; } // ---------------------------- // ftStatus=FT_SetTimeouts(ftHandle,1,1); if (ftStatus != FT_OK) { sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetTimeouts"); debug_print(msg); //return -1; } // ---------------------------- // ftStatus=FT_SetLatencyTimer(ftHandle,2); if(ftdi_set_latency_timer(*handle, 2) != 0) //if (ftStatus != FT_OK) { sprintf(msg,"|DELIB|DapiHandle ftStatus != FT_OK -> FT_SetLatencyTimer"); debug_print(msg); //return -1; } // ---------------------------- } } return (ULONG) *handle; }
bool FTDIDMXDevice::open() { // Change QString to char* (not const char* note) char *serial; QByteArray a = m_path.toLatin1(); serial = (char*)malloc(sizeof(char) * (a.count() + 1)); memcpy(serial, a.constData(), a.count()); serial[a.count()] = 0; #ifndef WIN32 // Windows users cannot dynamiccaly set VID/PID of harward if (FT_SetVIDPID(m_vid, m_pid) == FT_OK && FT_OpenEx(serial, FT_OPEN_BY_SERIAL_NUMBER, &m_handle) == FT_OK) { #else if (FT_OpenEx(serial, FT_OPEN_BY_SERIAL_NUMBER, &m_handle) == FT_OK) { #endif free(serial); if (!FT_SUCCESS(FT_ResetDevice(m_handle))) { qWarning() << "Unable to reset FTDI device" << m_path; return false; } // Set the baud rate 12 will give us 250Kbits if (!FT_SUCCESS(FT_SetDivisor(m_handle, 12))) { qWarning() << "Unable to set divisor on FTDI device" << m_path; return false; } // Set the data characteristics if (!FT_SUCCESS(FT_SetDataCharacteristics(m_handle, FT_BITS_8, FT_STOP_BITS_2, FT_PARITY_NONE))) { qWarning() << "Unable to set data characteristics on" << "FTDI device" << m_path; return false; } // Set flow control if (!FT_SUCCESS(FT_SetFlowControl(m_handle, FT_FLOW_NONE, 0, 0))) { qWarning() << "Unable to set flow control on" << "FTDI device" << m_path; return false; } // set RS485 for sendin FT_ClrRts(m_handle); // Clear TX RX buffers FT_Purge(m_handle,FT_PURGE_TX | FT_PURGE_RX); m_threadRunning = true; start(QThread::TimeCriticalPriority); return true; } else { qWarning() << "Unable to open FTDIDMX" << m_output << ":" << serial; free(serial); return false; } } bool FTDIDMXDevice::close() { // Kill thread m_threadRunning = false; wait(500); FT_Close(m_handle); return true; }
int main(int argc, char *argv[]) { char cBufRead[BUF_SIZE]; DWORD dwBytesRead; FT_STATUS ftStatus; FT_HANDLE ftHandle; int iport; int i; char c; if(argc > 1) { sscanf(argv[1], "%d", &iport); } else { iport = 0; } // Note! // The second version of open should work from version 0.4.9 it may be prefered // in many situations. On Fedora Core 4, kernal 2.6.15 it fails however. //ftStatus = FT_Open(iport, &ftHandle); ftStatus = FT_OpenEx( "LWO65RKA", FT_OPEN_BY_SERIAL_NUMBER, &ftHandle); //ftStatus = FT_OpenEx( NULL, FT_OPEN_BY_SERIAL_NUMBER, &ftHandle); // First found if(ftStatus != FT_OK) { /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("FT_Open(%d) failed. rv=%d\n", iport, ftStatus); return 1; } FT_SetTimeouts(ftHandle, 3000, 3000 ); // 3 second read timeout while ( 1 ) { printf("\n\n\n"); printf("=====================================================================\n"); printf(" Lawicel AB -- CANUSB test application\n"); printf("=====================================================================\n\n"); printf("S - Send test frames.\n"); printf("R - Read five frames.\n"); printf("N - Get Serial number.\n"); printf("V - Get Version Information.\n"); printf("Q - Quit application.\n"); while ( 0x0a == ( c = getchar() )); if ( 'q' == c || 'Q' == c ) break; else if ( 'v' == c || 'V' == c ){ getVersionInfo( ftHandle ); } else if ( 'n' == c || 'N' == c ){ getSerialNumber( ftHandle ); } else if ( 's' == c || 'S' == c ){ SendTestFrames( ftHandle ); } else if ( 'r' == c || 'R' == c ){ GetTestFrames( ftHandle ); } } printf("Bye,bye....\n"); FT_Close(ftHandle); return 0; }
int openSerialPorts(int baud) { char * pcBufRead = NULL; char * pcBufLD[MAX_DEVICES + 1]; DWORD dwRxSize = 0; DWORD dwBytesWritten, dwBytesRead; FT_STATUS ftStatus; int iNumDevs = 0; int i, j; int iDevicesOpen; unsigned char ucMode = 0x00; printf( "warning: opening up to %d ports and assuming all are Safecast devices.\n", MAX_DEVICES ); printf( "todo: make this more selective and safer.\n" ); for(i = 0; i < MAX_DEVICES; i++) { pcBufLD[i] = cBufLD[i]; } pcBufLD[MAX_DEVICES] = NULL; ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER); if(ftStatus != FT_OK) { printf("Error: FT_ListDevices(%d)\n", ftStatus); return -1; } for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ); i++) { printf("Device %d Serial Number - %s\n", i, cBufLD[i]); } for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ) ; i++) { /* Setup */ if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK){ /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("Error FT_OpenEx(%d), device\n", ftStatus); return -1; } printf("Opened device %s\n", cBufLD[i]); // if(getandcheckCBUS(ftHandle[i]) ) { // printf( "getandcheckCBUS failed, exiting.\n" ); // return -1; // } iDevicesOpen++; if((ftStatus = FT_SetBaudRate(ftHandle[i], baud)) != FT_OK) { printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", ftStatus, cBufLD[i]); break; } if((ftStatus = FT_SetDataCharacteristics(ftHandle[i], FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_EVEN)) != FT_OK) { printf("Error FT_SetDataCharacteristics(%d) = %s\n", ftStatus, cBufLD[i]); break; } FT_SetTimeouts(ftHandle[i], 500, 500); // 500 ms read/write timeout } iDevicesOpen = i; foundDevices = i; // record this in a global if(pcBufRead) free(pcBufRead); return 0; // we always use the 0th device for now }
int CKMotionIO::Connect() { char reason[256]; FT_STATUS ftStatus; if (NonRespondingCount==2) return 1; m_SaveChars[0]=0; // start anew Mutex->Lock(); if (!RequestedDeviceAvail(reason)) { ErrorMessageBox(reason); Mutex->Unlock(); return 1; } #define TIME_TO_TRY_TO_OPEN 3000 // FT_ListDevices OK, number of devices connected is in numDevs // usually during boot the board comes and goes, since it appeared // to be there, try for a while to open it DWORD t0=timeGetTime(); for (;;) { ftStatus = FT_OpenEx((void *)USB_Loc_ID,FT_OPEN_BY_LOCATION,&ftHandle); if (ftStatus == FT_OK) { // FT_Open OK, use ftHandle to access device if (SetLatency(2)) { Mutex->Unlock(); return 1; } if (FlushInputBuffer()) { FT_Close(ftHandle); Mutex->Unlock(); return 1; } m_Connected=true; // All set Mutex->Unlock(); return 0; } else { // FT_Open failed if (timeGetTime()-t0 > TIME_TO_TRY_TO_OPEN) { ErrorMessageBox("Unable to open KMotion device"); Mutex->Unlock(); return 1; } Sleep(100); // delay a bit then loop back and try again } } Mutex->Unlock(); return 0; }
int main() { char cBufWrite[BUF_SIZE]; char * pcBufRead = NULL; char * pcBufLD[MAX_DEVICES + 1]; char cBufLD[MAX_DEVICES][64]; DWORD dwRxSize = 0; DWORD dwBytesWritten, dwBytesRead; FT_STATUS ftStatus; FT_HANDLE ftHandle[MAX_DEVICES]; int iNumDevs = 0; int i, j; int iDevicesOpen; for(i = 0; i < MAX_DEVICES; i++) { pcBufLD[i] = cBufLD[i]; } pcBufLD[MAX_DEVICES] = NULL; ftStatus = FT_ListDevices(pcBufLD, &iNumDevs, FT_LIST_ALL | FT_OPEN_BY_SERIAL_NUMBER); if(ftStatus != FT_OK) { printf("Error: FT_ListDevices(%d)\n", ftStatus); return 1; } for(j = 0; j < BUF_SIZE; j++) { cBufWrite[j] = j; } for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ); i++) { printf("Device %d Serial Number - %s\n", i, cBufLD[i]); } for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ) ; i++) { /* Setup */ if((ftStatus = FT_OpenEx(cBufLD[i], FT_OPEN_BY_SERIAL_NUMBER, &ftHandle[i])) != FT_OK){ /* This can fail if the ftdi_sio driver is loaded use lsmod to check this and rmmod ftdi_sio to remove also rmmod usbserial */ printf("Error FT_OpenEx(%d), device\n", ftStatus, i); return 1; } printf("Opened device %s\n", cBufLD[i]); iDevicesOpen++; if((ftStatus = FT_SetBaudRate(ftHandle[i], 9600)) != FT_OK) { printf("Error FT_SetBaudRate(%d), cBufLD[i] = %s\n", ftStatus, cBufLD[i]); break; } for(j = 0; j < BUF_SIZE; j++) { printf("cBufWrite[%d] = 0x%02X\n", j, cBufWrite[j]); } /* Write */ if((ftStatus = FT_Write(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesWritten)) != FT_OK) { printf("Error FT_Write(%d)\n", ftStatus); break; } sleep(1); /* Read */ dwRxSize = 0; while ((dwRxSize < BUF_SIZE) && (ftStatus == FT_OK)) { ftStatus = FT_GetQueueStatus(ftHandle[i], &dwRxSize); } if(ftStatus == FT_OK) { pcBufRead = (char*)realloc(pcBufRead, dwRxSize); memset(pcBufRead, 0xFF, dwRxSize); for(j = 0; j < dwRxSize; j++) { printf("pcBufWrite[%d] = 0x%02X\n", j, pcBufRead[j]); } if((ftStatus = FT_Read(ftHandle[i], pcBufRead, dwRxSize, &dwBytesRead)) != FT_OK) { printf("Error FT_Read(%d)\n", ftStatus); } else { printf("FT_Read = %d\n", dwBytesRead); for(j = 0; j < dwBytesRead; j++) { printf("pcBufWrite[%d] = 0x%02X\n", j, pcBufRead[j]); } } } else { printf("Error FT_GetQueueStatus(%d)\n", ftStatus); } } iDevicesOpen = i; /* Cleanup */ for(i = 0; i < iDevicesOpen; i++) { FT_Close(ftHandle[i]); printf("Closed device %s\n", cBufLD[i]); } if(pcBufRead) free(pcBufRead); return 0; }
static int ublast_ftd2xx_init(struct ublast_lowlevel *low) { FT_STATUS status; FT_HANDLE *ftdih = ublast_getftdih(low); uint8_t latency_timer; LOG_INFO("usb blaster interface using FTD2XX"); /* Open by device description */ if (low->ublast_device_desc == NULL) { LOG_WARNING("no usb blaster device description specified, " "using default 'USB-Blaster'"); low->ublast_device_desc = "USB-Blaster"; } #if IS_WIN32 == 0 /* Add non-standard Vid/Pid to the linux driver */ status = FT_SetVIDPID(low->ublast_vid, low->ublast_pid); if (status != FT_OK) { LOG_WARNING("couldn't add %4.4x:%4.4x", low->ublast_vid, low->ublast_pid); } #endif status = FT_OpenEx(low->ublast_device_desc, FT_OPEN_BY_DESCRIPTION, ftdih); if (status != FT_OK) { DWORD num_devices; LOG_ERROR("unable to open ftdi device: %s", ftd2xx_status_string(status)); status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY); if (status == FT_OK) { char **desc_array = malloc(sizeof(char *) * (num_devices + 1)); unsigned int i; for (i = 0; i < num_devices; i++) desc_array[i] = malloc(64); desc_array[num_devices] = NULL; status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION); if (status == FT_OK) { LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices); for (i = 0; i < num_devices; i++) LOG_ERROR("%i: %s", i, desc_array[i]); } for (i = 0; i < num_devices; i++) free(desc_array[i]); free(desc_array); } else { printf("ListDevices: NONE\n"); } return ERROR_JTAG_INIT_FAILED; } status = FT_SetLatencyTimer(*ftdih, 2); if (status != FT_OK) { LOG_ERROR("unable to set latency timer: %s", ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } status = FT_GetLatencyTimer(*ftdih, &latency_timer); if (status != FT_OK) { LOG_ERROR("unable to get latency timer: %s", ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } LOG_DEBUG("current latency timer: %i", latency_timer); status = FT_SetBitMode(*ftdih, 0x00, 0); if (status != FT_OK) { LOG_ERROR("unable to disable bit i/o mode: %s", ftd2xx_status_string(status)); return ERROR_JTAG_INIT_FAILED; } return ERROR_OK; }
int main(int argc, char ** argv) { DWORD devIndex = 0; char Buffer[255]; ftdi_pins.pins = 0xF1; ftdi_pins.clk = HIGH; DWORD p = 0; byte e1 = 0xff; byte e2 = 0; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, 7); if (argc < 2) { printf("Please specify the hex file\n"); return 1; } FT_STATUS ftStatus = FT_ListDevices((PVOID)devIndex, Buffer, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); if (ftStatus == FT_OK) { int bytes = load_file(argv[1]); ftStatus = FT_OpenEx(Buffer, FT_OPEN_BY_SERIAL_NUMBER, &ftdi_chip); FT_SetBitMode(ftdi_chip, PIN_TX | PIN_RX | PIN_CTS, 1); // BIT BANG MODE FT_SetBaudRate(ftdi_chip, BAUD_RATE); setPin(RST_8051, LOW); setPin(MOSI_8051, LOW); setPin(CLK_8051, LOW); setPin(RST_8051, HIGH); Sleep(500); printf("%x\n", progEnable()); Sleep(1100); eraseChip(); for (int i = from_addr; i < to_addr + 1; i++) { printf("%02X", readProgmem(0, i)); } printf("\n"); for (int i = from_addr; i < to_addr + 1; i++) { printf("%02X", memory[i]); } printf("\n"); // write memory for (int i = from_addr; i < to_addr+1; i++) { writeProgmem(0, i, memory[i]); printf("%02X", readProgmem(0, i)); } printf("\n"); Sleep(1000); setPin(RST_8051, LOW); Sleep(500); FT_Close(ftdi_chip); getchar(); } else { printf("Can't open FTDI chip\n"); return 1; } return 0; }