BOOL CDriverInterface::GetDriverVersion(LPDWORD pDriverVersion) { FT_STATUS ftResult; ftResult = FT_GetDriverVersion(m_FTHandle, pDriverVersion); if (FT_FAILED(ftResult)) { return FALSE; } return TRUE; }
bool NxDeviceDmx::ConnectDevice() { int RTimeout =120; int WTimeout =100; int VersionMSB =0; int VersionLSB =0; unsigned char temp[4]; long version; unsigned char major_ver,minor_ver,build_ver; int recvd =0; unsigned char byte = 0; int size = 0; int res = 0; int tries =0; unsigned char latencyTimer; FT_STATUS ftStatus; int BreakTime; int MABTime; mDevice->mUsePro = false; // to do if Log("Waiting for GET_WIDGET_PARAMS_REPLY packet... "); is noresponse if( mDevice->mUsePro ) { int device_num = 0; FT_STATUS ftStatus = FT_Open(device_num,&mDevice->device_handle); if( ftStatus == FT_OK ) { Log(" opened Pro enttec Device"); } else { Log(" Cant open Pro enttec Device"); } // GET D2XX Driver Version ftStatus = FT_GetDriverVersion(mDevice->device_handle,(LPDWORD)&version); if (ftStatus == FT_OK) { major_ver = (unsigned char) version >> 16; minor_ver = (unsigned char) version >> 8; build_ver = (unsigned char) version & 0xFF; //Log("\nD2XX Driver Version:: %02X.%02X.%02X ",major_ver,minor_ver,build_ver); Log("Found Driver Version"); }
FT_STATUS GetComPort( LONG * comPort ) { //FTD2xx: // FT_GetDriverVersion , FT_GetLibraryVersion // FT_GetDeviceInfo , FT_CreateDeviceInfoList , FT_GetDeviceInfoDetail // FT_GetComPortNumber // FT_CyclePort , FT_ResetPort , FT_ResetDevice , FT_GetStatus FT_HANDLE ftHandle; FT_STATUS ftStatus; uint8_t * version; #define VER_BUILD 0 #define VER_MINOR 1 #define VER_MAJOR 2 #define VER_UNUSED 3 DWORD dwLibraryVer; // Get DLL version ftStatus = FT_GetLibraryVersion(&dwLibraryVer); if (ftStatus == FT_OK) { version = (uint8_t *)&dwLibraryVer; //ASSUMPTION: dwLibraryVer is little-endian std::cout << "FTD2xx Library " << int(version[VER_MAJOR]) << "." << int(version[VER_MINOR]) << " Build " << int(version[VER_BUILD]) << std::endl; } else { std::cerr << "ERROR: FT_GetLibraryVersion()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } DWORD dwDriverVer; // Get driver version ftStatus = FT_Open(0, &ftHandle); if (ftStatus == FT_OK) { ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer); if (ftStatus == FT_OK) { version = (uint8_t *)&dwDriverVer; //ASSUMPTION: dwLibraryVer is little-endian std::cout << "FTD2xx Driver " << int(version[VER_MAJOR]) << "." << int(version[VER_MINOR]) << " Build " << int(version[VER_BUILD]) << std::dec << std::endl; } else { std::cerr << "ERROR: FT_GetDriverVersion()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } FT_Close(ftHandle); } else { std::cerr << "ERROR: FT_Open()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } DWORD numDevs; ftStatus = FT_ListDevices(&numDevs, NULL, FT_LIST_NUMBER_ONLY); if (ftStatus == FT_OK) { // FT_ListDevices OK, number of devices connected is in numDevs } else { // FT_ListDevices failed std::cerr << "ERROR: FT_ListDevices()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } DWORD numDevs2; // create the device information list ftStatus = FT_CreateDeviceInfoList(&numDevs2); if (ftStatus == FT_OK) { } else { // FT_CreateDeviceInfoList failed std::cerr << "ERROR: FT_CreateDeviceInfoList()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } if (numDevs != numDevs2) { std::cerr << "ERROR: number of devices from FT_ListDevices() and FT_CreateDeviceInfoList() are different" << std::endl; return ftStatus; } if (numDevs == 0) { std::cerr << "ERROR: numDevs==0 ; ftStatus==FT_DEVICE_NOT_FOUND (2) ???" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } if (numDevs > 1) { std::cerr << "ERROR: numDevs > 1" << std::endl; std::cerr << "ERROR: returning numDevs" << std::endl; return numDevs; } std::cout << "1 FTDI device found" << std::endl; /* DWORD devIndex = 0; // first device char buffer[64]; // more than enough room! ftStatus = FT_ListDevices((PVOID)devIndex, buffer, FT_LIST_BY_INDEX | FT_OPEN_BY_SERIAL_NUMBER); if (ftStatus == FT_OK) { // FT_ListDevices OK, serial number is in Buffer } else { // FT_ListDevices failed std::cerr << "ERROR: FT_ListDevices()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } std::cout << buffer << std::endl; */ //FT_DEVICE_LIST_INFO_NODE * devInfo; // allocate storage for list based on numDevs //devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs); FT_DEVICE_LIST_INFO_NODE devInfo[1]; // get the device information list ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs); if (ftStatus == FT_OK) { //for (unsigned int i = 0; i < numDevs; i++) //{ unsigned int i = 0; //std::cout << "Dev " << i << ":" << std::endl; std::cout << std::hex; std::cout << " Flags=0x" << devInfo[i].Flags << std::endl; std::cout << " Type=0x" << devInfo[i].Type << std::endl; std::cout << " ID=0x" << devInfo[i].ID << std::endl; std::cout << " LocId=0x" << devInfo[i].LocId << std::endl; std::cout << " ftHandle=0x" << devInfo[i].ftHandle << std::endl; std::cout << std::dec; std::cout << " SerialNumber=" << devInfo[i].SerialNumber << std::endl; std::cout << " Desc=" << devInfo[i].Description << std::endl; //} } else { std::cerr << "ERROR: FT_GetDeviceInfoList()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } ftStatus = FT_Open(0, &ftHandle); if (ftStatus == FT_OK) { ftStatus = FT_GetComPortNumber(ftHandle, comPort); if (ftStatus == FT_OK) { if(*comPort == -1) { // No COM port assigned } else { // COM port assigned with number held in *comPort } } else { // FT_GetComPortNumber FAILED! std::cerr << "ERROR: FT_GetComPortNumber()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } FT_Close(ftHandle); if (ftStatus == FT_OK) { } else { std::cerr << "ERROR: FT_Close()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } } else { std::cerr << "ERROR: FT_Open()" << std::endl; std::cerr << "ERROR: returning ftStatus" << std::endl; return ftStatus; } return FT_OK; }
int main(int argc, char *argv[]) { int retCode = -1; // Assume failure int f = 0; DWORD driverVersion = 0; FT_STATUS ftStatus = FT_OK; FT_HANDLE ftHandle = NULL; int portNum = 0; // First device found size_t bufferSize = 64 * 1024; DWORD bytesToWrite; DWORD bytesWritten = 0; DWORD bytesReceived = 0; DWORD bytesRead = 0; struct timeval startTime; int journeyDuration; unsigned char *writeBuffer = NULL; unsigned char *readBuffer = NULL; int queueChecks = 0; ULONG rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 576000, 921600, 1500000, 2000000, 3000000}; // TODO: detect high-speed device and use 8240000 UNUSED_PARAMETER(argc); UNUSED_PARAMETER(argv); // Make printfs immediate (no buffer) setvbuf(stdout, NULL, _IONBF, 0); writeBuffer = (unsigned char *)malloc((size_t)bufferSize); if (writeBuffer == NULL) goto exit; // Fill write buffer with consecutive values for (f = 0; f < (int)bufferSize; f++) { writeBuffer[f] = (unsigned char)f + 64; } printf("Opening FTDI device %d.\n", portNum); ftStatus = FT_Open(portNum, &ftHandle); if (ftStatus != FT_OK) { printf("FT_Open(%d) failed, with error %d.\n", portNum, (int)ftStatus); printf("On Linux, lsmod can check if ftdi_sio (and usbserial) are present.\n"); printf("If so, unload them using rmmod, as they conflict with ftd2xx.\n"); goto exit; } assert(ftHandle != NULL); ftStatus = FT_GetDriverVersion(ftHandle, &driverVersion); if (ftStatus != FT_OK) { printf("Failure. FT_GetDriverVersion returned %d.\n", (int)ftStatus); goto exit; } printf("Using D2XX version %08x\n", driverVersion); ftStatus = FT_ResetDevice(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_ResetDevice returned %d.\n", (int)ftStatus); goto exit; } // Flow control is needed for higher baud rates ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0, 0); if (ftStatus != FT_OK) { printf("Failure. FT_SetFlowControl returned %d.\n", (int)ftStatus); goto exit; } ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE); if (ftStatus != FT_OK) { printf("Failure. FT_SetDataCharacteristics returned %d.\n", (int)ftStatus); goto exit; } for (f = 0; f < (int)ARRAY_SIZE(rates); f++) { ftStatus = FT_SetBaudRate(ftHandle, rates[f]); if (ftStatus != FT_OK) { printf("Failure. FT_SetBaudRate(%d) returned %d.\n", (int)rates[f], (int)ftStatus); goto exit; } // Assert Request-To-Send to prepare receiver ftStatus = FT_SetRts(ftHandle); if (ftStatus != FT_OK) { printf("Failure. FT_SetRts returned %d.\n", (int)ftStatus); goto exit; } if (rates[f] < 57600) { // Keep test duration reasonable by transferring fewer // bytes at low baud rates. bytesToWrite = rates[f] / 4; } else { bytesToWrite = bufferSize; } printf("\nBaud rate %d. Writing %d bytes to loopback device...\n", (int)rates[f], (int)bytesToWrite); ftStatus = FT_Write(ftHandle, writeBuffer, bytesToWrite, &bytesWritten); if (ftStatus != FT_OK) { printf("Failure. FT_Write returned %d\n", (int)ftStatus); goto exit; } if (bytesWritten != bytesToWrite) { printf("Failure. FT_Write wrote %d bytes instead of %d.\n", (int)bytesWritten, (int)bytesToWrite); goto exit; } printf("%d bytes written.\n", (int)bytesWritten); // Keep checking queue until D2XX has received all the bytes we wrote. // Estimate total time to write and read, so we can time-out. // Each byte has 8 data bits plus a stop bit and perhaps a 1-bit gap. journeyDuration = bytesWritten * (8 + 1 + 1) / (int)rates[f]; journeyDuration += 1; // Round up journeyDuration *= 2; // It's a return journey printf("Estimate %d seconds remain.\n", journeyDuration); gettimeofday(&startTime, NULL); for (bytesReceived = 0, queueChecks = 0; bytesReceived < bytesWritten; queueChecks++) { // Periodically check for time-out if (queueChecks % 32 == 0) { struct timeval now; struct timeval elapsed; gettimeofday(&now, NULL); timersub(&now, &startTime, &elapsed); if (elapsed.tv_sec > (long int)journeyDuration) { // We've waited too long. Give up. printf("\nTimed out after %ld seconds\n", elapsed.tv_sec); break; } // Display number of bytes D2XX has received printf("%s%d", queueChecks == 0 ? "Number of bytes in D2XX receive-queue: " : ", ", (int)bytesReceived); } ftStatus = FT_GetQueueStatus(ftHandle, &bytesReceived); if (ftStatus != FT_OK) { printf("\nFailure. FT_GetQueueStatus returned %d.\n", (int)ftStatus); goto exit; } } printf("\nGot %d (of %d) bytes.\n", (int)bytesReceived, (int)bytesWritten); // Even if D2XX has the wrong number of bytes, create our // own buffer so we can read and display them. free(readBuffer); // Free previous iteration's buffer. readBuffer = (unsigned char *)calloc(bytesReceived, sizeof(unsigned char)); if (readBuffer == NULL) { printf("Failed to allocate %d bytes.\n", bytesReceived); goto exit; } // Then copy D2XX's buffer to ours. ftStatus = FT_Read(ftHandle, readBuffer, bytesReceived, &bytesRead); if (ftStatus != FT_OK) { printf("Failure. FT_Read returned %d.\n", (int)ftStatus); goto exit; } if (bytesRead != bytesReceived) { printf("Failure. FT_Read only read %d (of %d) bytes.\n", (int)bytesRead, (int)bytesReceived); goto exit; } if (0 != memcmp(writeBuffer, readBuffer, bytesRead)) { printf("Failure. Read-buffer does not match write-buffer.\n"); printf("Write buffer:\n"); dumpBuffer(writeBuffer, bytesReceived); printf("Read buffer:\n"); dumpBuffer(readBuffer, bytesReceived); goto exit; } // Fail if D2XX's queue lacked (or had surplus) bytes. if (bytesReceived != bytesWritten) { printf("Failure. D2XX received %d bytes but we expected %d.\n", (int)bytesReceived, (int)bytesWritten); dumpBuffer(readBuffer, bytesReceived); goto exit; } // Check that queue hasn't gathered any additional unexpected bytes bytesReceived = 4242; // deliberately junk ftStatus = FT_GetQueueStatus(ftHandle, &bytesReceived); if (ftStatus != FT_OK) { printf("Failure. FT_GetQueueStatus returned %d.\n", (int)ftStatus); goto exit; } if (bytesReceived != 0) { printf("Failure. %d bytes in input queue -- expected none.\n", (int)bytesReceived); goto exit; } } // Success printf("\nTest PASSED.\n"); retCode = 0; exit: free(readBuffer); free(writeBuffer); if (ftHandle != NULL) FT_Close(ftHandle); return retCode; }