コード例 #1
0
ファイル: serial_ftdi.cpp プロジェクト: new299/onyxloader
// Read up to the specified number of bytes, return bytes actually read
u32 ser_read( int id, u8* dest, u32 maxsize )
{
  DWORD dwRxSize;
  FT_STATUS	ftStatus;
  DWORD dwBytesRead;

  if( ser_timeout == SER_INF_TIMEOUT ) {
    printf( "infinite timeout selected.\n" );
    FT_SetTimeouts(ftHandle[id], 10000000, 10000000); // a couple hours to timeout...
    dwRxSize = 0;
    ftStatus = FT_OK;
    while ((dwRxSize < maxsize) && (ftStatus == FT_OK)) {
      ftStatus = FT_GetQueueStatus(ftHandle[id], &dwRxSize);
    }

    if(ftStatus == FT_OK) {
      if((ftStatus = FT_Read(ftHandle[id], dest, maxsize, &dwBytesRead)) != FT_OK) {
	printf("Error FT_Read(%d)\n", ftStatus);
      }
      else {
	if( ser_dbg )
	  printf("FT_Read = %d\n", dwBytesRead);
      }
    }
    else {
      printf("Error FT_GetQueueStatus(%d)\n", ftStatus);
      return 0;
    }
    return (u32) dwBytesRead;
  }
  else
  {
  //  fd_set readfs;
  //  struct timeval tv;
  //  int retval;

    if( ser_timeout == 0 )
      ser_timeout = 1;
    //fprintf(stderr,"setting timeout to %u\n", ser_timeout );
    //fprintf(stderr,"read size: %d\n",maxsize);
    //fprintf(stderr,"id: %d\n",id);
    FT_SetTimeouts(ftHandle[id], ser_timeout, ser_timeout);

    if((ftStatus = FT_Read(ftHandle[id], dest, maxsize, &dwBytesRead)) != FT_OK) {
     // fprintf(stderr,"Error FT_Read(%d)\n", ftStatus);
    }
    else {
      ftStatus = FT_GetQueueStatus(ftHandle[id], &dwRxSize);

  	//  fprintf(stderr,"  - ser_read [%d | %02x] . %d\n", dwBytesRead, *dest & 0xFF, dwRxSize);
    }

    //fprintf(stderr,"read: %u\n",dwBytesRead);
    return (u32) dwBytesRead;
  }
}
コード例 #2
0
ファイル: ftdi_unplug.cpp プロジェクト: chrisl8/node-ftdi
void* ReadData(void* ptr)
{
	DeviceParams_t* device = (DeviceParams_t*)ptr;
	DWORD RxBytes;
	FT_STATUS status;

	printf("Thread Started\r\n");
	
	while(true)
	{
		WaitForData(device);

		status = FT_GetQueueStatus(device->handle, &RxBytes);
		if(status != FT_OK)
        {
            fprintf(stderr, "Can't read from ftdi device: %d\n", status);
            return NULL;
        }

        //printf("RX %s [%d]\r\n", device->serial, RxBytes);
        if(RxBytes > 0)
        {
        	DWORD BytesReceived;
        	char* data = new char[RxBytes];
        	
        	FT_Read(device->handle, data, RxBytes, &BytesReceived);
			
			fprintf(stderr, "%d Bytes Read\n", BytesReceived);
			delete[] data;
        }
	}

	return NULL;
} 
コード例 #3
0
bool FTDISerial::Available() {
	if(!m_isConnected)
		return false;

	DWORD RxBytes;
	FT_GetQueueStatus(m_handle, &RxBytes);
	if(RxBytes > 0)
		return true;
	else
		return false;
}
コード例 #4
0
ファイル: CONEMU_Win.cpp プロジェクト: NibblesLab/ConEmu_Win
// USB からデータを得る
// 別スレッドにて実行
DWORD CALLBACK GetUSB(void *param)
{
	BYTE buff[MAX_BUFSIZE];
	DWORD RxQueSize = 0;
	DWORD bytesRead = 0;
	FT_STATUS ftStatus = FT_OK;

	while (true)
	{
		//オブジェクトがシグナル状態となるまで待機
		WaitForSingleObject(hEvent, INFINITE);
		//ResetEvent(hEvent);

		FT_GetQueueStatus(ftHandle, &RxQueSize);
		while (RxQueSize)
		{
			ftStatus = FT_Read(ftHandle, buff, RxQueSize, &bytesRead);

			// 手抜きリングバッファ(読み出し側には配慮せず)
			if (wptr + bytesRead > MAX_RINGBUF)
			{
				memcpy(&RxBuff[wptr], buff, MAX_RINGBUF - wptr);
				memcpy(RxBuff, &buff[MAX_RINGBUF - wptr], bytesRead - (MAX_RINGBUF - wptr));
			}
			else
			{
				memcpy(&RxBuff[wptr], buff, bytesRead);
			}
			wptr += bytesRead;
			if (wptr >= MAX_RINGBUF)
			{
				wptr -= MAX_RINGBUF;
			}
			FT_GetQueueStatus(ftHandle, &RxQueSize);
		}
		//if (!RxQueSize) continue;
	}
	return 0;
}
コード例 #5
0
ファイル: main.c プロジェクト: 0x6a77/JD2XX
int main(int argc, char *argv[])
{
	DWORD dwBytesInQueue = 0;
	EVENT_HANDLE eh;
	FT_STATUS	ftStatus;
	FT_HANDLE	ftHandle;
	int iport;
	
	if(argc > 1) {
		sscanf(argv[1], "%d", &iport);
	}
	else {
		iport = 0;
	}
	
	pthread_mutex_init(&eh.eMutex, NULL);
	pthread_cond_init(&eh.eCondVar, NULL);
	
	ftStatus = FT_Open(iport, &ftHandle);
	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\n", iport);
		return 1;
	}
		
	ftStatus = FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0);
	if(ftStatus != FT_OK) {
		printf("Failed to set flow control\n");	
	}

	ftStatus = FT_SetEventNotification(ftHandle, FT_EVENT_RXCHAR, (PVOID)&eh);
	if(ftStatus != FT_OK) {
		printf("Failed to set events\n");
		return 1;
	}
	
	pthread_mutex_lock(&eh.eMutex);
	pthread_cond_wait(&eh.eCondVar, &eh.eMutex);
	pthread_mutex_unlock(&eh.eMutex);
	
	FT_GetQueueStatus(ftHandle, &dwBytesInQueue);
	printf("Received chars %d bytes in queue\n", (int)dwBytesInQueue);
	
	FT_Close(ftHandle);
	
	return 0;
}
コード例 #6
0
ファイル: ft245r.c プロジェクト: arnonh/myelin-matrix-display
static int ft245r_drain(PROGRAMMER * pgm, int display)
{
  FT_STATUS r;
  DWORD n;
  r = FT_SetBitMode(handle, 0, 0x0); 	// reset 
  if (r != FT_OK) return -1;
  r = FT_SetBitMode(handle, ft245r_ddr, 0x4); // set Synchronuse BitBang
  if (r != FT_OK) return -1;

  r = FT_GetQueueStatus(handle, &n);
  if (r != FT_OK) return -1;
  if (n) {
	fprintf(stderr, "ft245r_drain called but queue is not empty %d \n",
		(int)n);
  }
  return 0;
}
コード例 #7
0
int dxl_hal_rx( unsigned char *pPacket, int numPacket )
{
	FT_STATUS ft_status;
	DWORD dwNumToRead;
	DWORD dwNumRead = 0;

	ft_status = FT_GetQueueStatus( ghFt_Handle, &dwNumToRead );
	if( ft_status != FT_OK )
		return 0;

	if( dwNumToRead > 0 )
	{
		ft_status = FT_Read( ghFt_Handle, (LPVOID)pPacket, dwNumToRead, &dwNumRead );
		if( ft_status == FT_IO_ERROR )
			return 0;
	}

	return (int)dwNumRead;
}
コード例 #8
0
ファイル: ftdichip.cpp プロジェクト: tuzhikov/testCountDown
/* Read buffer USB*/
quint16 ftdiChip::Read(QByteArray &buff)
{
FT_STATUS ftStatus;
DWORD  numout,RxBytes;// RxBuf=NULL, TxBuf=NULL, EventStat;

//ftStatus=FT_GetStatus(hdUSB,&RxBuf,&TxBuf, &EventStat);
//do{
  ftStatus=FT_GetQueueStatus(hdUSB,&RxBytes);
  if(ftStatus==FT_OK){
    if(RxBytes>0){ //чтение только если в приёмном буфере что то есть
      QByteArray tmpbuff(RxBytes,0x00);
      ftStatus=FT_Read(hdUSB,tmpbuff.data(),RxBytes,&numout);
      if(ftStatus!=FT_OK)return retErr;
      buff.append(tmpbuff);
      }
      else return retBusyDevice;
    }else return retErr;
  //}while(RxBytes);
return retOk;
}
コード例 #9
0
ファイル: MmcUsbHndlLinux.cpp プロジェクト: RIVeR-Lab/eposcmd
bool CMmcUsbHndlBase::GetQueueStatus(unsigned int *pdAmountRxQueue)
{

	if( !AreLibraryFunctionsLoaded() )
    {
		perror("Library not loaded");
        return false;
    }

	FT_STATUS ftStatus = FT_GetQueueStatus(m_Handle, (DWORD*)pdAmountRxQueue);

	if( ftStatus != FT_OK )
	{
		std::string errormsg = GetFtStatusDescription(ftStatus);
		errormsg += ":FT_GetQueueStatus";
		perror(errormsg.c_str());
	}

	return (FT_OK == ftStatus);
}
コード例 #10
0
//Just wraps the FT_Read function in a QueueStatus call so we don't block on reads
int nifalcon_read(falcon_device* dev, unsigned char* str, unsigned int size, unsigned int timeout_ms)
{
	unsigned long bytes_rx, bytes_read = 0;
	clock_t timeout = (clock_t)(((double)timeout_ms * .001) * (double)CLOCKS_PER_SEC) + clock();		
	
	if(!dev->is_open) nifalcon_error_return(NIFALCON_DEVICE_NOT_FOUND_ERROR, "tried to read from an unopened device");
	
	while(bytes_read < size)
	{
		if((dev->falcon_status_code = FT_GetQueueStatus(dev->falcon, &bytes_rx)) != FT_OK) return -dev->falcon_status_code;
		if(bytes_rx > size) bytes_rx = size - bytes_read;
		if(bytes_rx > 0)
		{
			if((dev->falcon_status_code = FT_Read(dev->falcon, str, bytes_rx, &bytes_read)) != FT_OK) return -dev->falcon_status_code;
			bytes_read += bytes_rx;
		}
		if (clock() > timeout) return bytes_read;
	}
	return bytes_read;
}
コード例 #11
0
ファイル: usb.cpp プロジェクト: meierb/psi46test
bool CUSB::FillBuffer(unsigned long minBytesToRead)
{
	if (!isUSB_open) return false;

	DWORD bytesAvailable, bytesToRead;

	ftStatus = FT_GetQueueStatus(ftHandle, &bytesAvailable);
	if (ftStatus != FT_OK) return false;

	if (m_posR<m_sizeR) return false;

	bytesToRead = (bytesAvailable>minBytesToRead)? bytesAvailable : minBytesToRead;
	if (bytesToRead>USBREADBUFFERSIZE) bytesToRead = USBREADBUFFERSIZE;

	ftStatus = FT_Read(ftHandle, m_bufferR, bytesToRead, &m_sizeR);
	m_posR = 0;
	if (ftStatus != FT_OK)
	{
		m_sizeR = 0;
		return false;
	}
	return true;
}
コード例 #12
0
ファイル: main.c プロジェクト: mikanradojevic/sdkpub
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;
}
コード例 #13
0
ファイル: main.c プロジェクト: Enny1991/libusb_android_test
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;
}
コード例 #14
0
ファイル: loop3.cpp プロジェクト: robacklin/ftdichip
int main(int argc, char* argv[])
{
    FT_HANDLE fthandle;
    FT_STATUS status;
    DWORD numdev = 0;

    // Open the first device connected to the system (which has index 0). You can use the various other
    // functions such as open_by_description to make this much more flexible and user friendly to let the user
    // choose which device to open. See the D2xx Programmers Guide for more information

    // Check how many FTDI devices are connected and installed. If one or more connected, open the first one
    status = FT_CreateDeviceInfoList(&numdev);

    if 	((status == FT_OK) && (numdev > 0) )
    {
        // Open the device now
        status = FT_Open(0, &fthandle);
        if(status != FT_OK)
            printf("status not ok %d\n", status);

        // Set the In transfer size. You can set up to 64K if required. Ideally, use a small value like this for
        // receiving a few bytes at a time or a larger value if you will be transferring large amounts of data
        status = FT_SetUSBParameters(fthandle, 256, 0);
        if(status != FT_OK)
            printf("status not ok %d\n", status);

        // Reset the device
        status = FT_ResetDevice(fthandle);
        if(status != FT_OK)
            printf("status not ok %d\n", status);

        // Set the handshaking mode in the driver, for I2C chips this has no affect on the external I2C interface
        // since it does not have handshake lines but this enables internal handshake in the driver
        status = FT_SetFlowControl(fthandle, FT_FLOW_RTS_CTS, FT_STOP_BITS_1, FT_PARITY_NONE);
        if(status != FT_OK)
            printf("status not ok %d\n", status);

        // Set Timeouts to ensure a Read or Write will return if unable to be completed
        // Setting both read and write timeouts to 5 seconds
        status = FT_SetTimeouts(fthandle, 5000, 5000);
        if(status != FT_OK)
            printf("status not ok %d\n", status);

        // Set Latency Timer (keeping it at default of 16ms here)
        status = FT_SetLatencyTimer(fthandle, 16);
        if(status != FT_OK)
            printf("status not ok %d\n", status);

        // Now write some data to the chips buffer
        char data_out[12] = "HELLO WORLD";
        DWORD w_data_len = 12;
        DWORD data_written;

        status = FT_Write(fthandle, data_out, w_data_len, &data_written);
        if(status != FT_OK)
            printf("status not ok %d\n", status);
        else
            printf("12 Bytes Sent, waiting for bytes to come back\n");
        /**********************************************************************/
// The I2C Master should now be able to read these 12 bytes from the FT-X over I2C. This example expects the
// I2C Master to send the bytes back over I2C to the FT-X
        /**********************************************************************/

        // Now read the data which the I2C master has written to the FT-X
        char data_in[12];
        DWORD data_read;
        DWORD MyBytesReceived = 0;
        DWORD SoftwareTimeout = 0;

        // Wait for the FT-X to send our 12 bytes back to the PC
        while((MyBytesReceived <12) && (SoftwareTimeout < 500))
        {
            FT_GetQueueStatus(fthandle, &MyBytesReceived);
            Sleep(1);
            SoftwareTimeout ++;
        }

        // Check if the loop exited due to timing out or receiving 12 bytes
        if(SoftwareTimeout == 500)
        {
            printf("Timed out waiting for data\n");
        }
        else
        {
            // Now read the received bytes
            status = FT_Read(fthandle, data_in, MyBytesReceived, &data_read);
            if(status != FT_OK)
                printf("status not ok %d\n", status);
            else
                printf("data read %s\n", data_in);
        }
        // Close the device
        status = FT_Close(fthandle);
    }
    else
    {
        printf("No FTDI devices connected to the computer \n");
    }

    printf("Press Return To End Program");
    getchar();
    printf("closed \n");
    return 0;
}
コード例 #15
0
ファイル: main.c プロジェクト: MrJack91/controlledClock
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;
}
コード例 #16
0
int read_encoder(FT_HANDLE ftHandleDYNA, int motorNum)
{
    unsigned char InstructionPacket[160] = {0};
    unsigned char StatusPacket[70]= {0};
    FT_STATUS ft_status;

    //make the packet
    InstructionPacket[0] = 0xff;
    InstructionPacket[1] = 0xff;
    InstructionPacket[ID] = (unsigned char)motorNum;
    InstructionPacket[LENGTH] = 4;
    InstructionPacket[INSTRUCTION] = INST_READ;
    InstructionPacket[PARAMETER] = (unsigned char)36;//address for encoder
    InstructionPacket[PARAMETER+1] = 2;

    unsigned char checksum = 0;
    for(unsigned char i=0; i<(InstructionPacket[LENGTH]+1); i++ )
        checksum += InstructionPacket[i+2];
    InstructionPacket[InstructionPacket[LENGTH]+3] = ~checksum;

    //send the packet
    unsigned char *pPacket = InstructionPacket;
    int numPacket = 8; //length plus 4
    DWORD dwNumToWrite = (DWORD)numPacket;
    DWORD dwNumWritten;

    ft_status = FT_Write(ftHandleDYNA, (LPVOID)pPacket, dwNumToWrite, &dwNumWritten );
    if( ft_status == FT_IO_ERROR )
    {
        qDebug() << "\n\nError sending encoder packet to motor!";
        return -1;
    }
    if(numPacket != dwNumWritten)
    {
        qDebug() << "\n\nAll Bytes of encoder packet were not written to device!";
        return -1;
    }

    //get status packet
    unsigned char *ppPacket = StatusPacket;
    DWORD dwNumToRead;
    DWORD dwNumRead = 0;

    ft_status = FT_GetQueueStatus(ftHandleDYNA, &dwNumToRead );
    if( ft_status != FT_OK )
    {
        qDebug() << "\n\nError receiving encoder status packet, Queue not empty!";
        return -1;
    }
    if( dwNumToRead > 0 )
    {
        ft_status = FT_Read(ftHandleDYNA, (LPVOID)ppPacket, dwNumToRead, &dwNumRead );
        if( ft_status == FT_IO_ERROR )
        {
            qDebug() << "\n\nError reading encoder value!";
            return -1;
        }
    } else {
        qDebug()<<"\n\nERROR: dwNumToRead = 0";
        return -1;
    }

    return makeword((int)StatusPacket[PARAMETER], (int)StatusPacket[PARAMETER+1]);

}
コード例 #17
0
ファイル: main.c プロジェクト: 0x6a77/JD2XX
int main()
{
	char 	cBufWrite[BUF_SIZE];
	char * 	pcBufLD[MAX_DEVICES + 1];
	char 	cBufLD[MAX_DEVICES][64];
	DWORD	dwRxSize = 0;
	DWORD 	dwBytesWritten, dwBytesRead, dwErrors, dwRead;
	FT_STATUS	ftStatus;
	int	iNumDevs = 0;
	int	i, j;
	int	iDevicesOpen = 0;
	FTDCB ftDCB, ftnewDCB;
	FTCOMSTAT ftComStat;
		
	for(i = 0; i < MAX_DEVICES; i++) {
		pcBufLD[i] = cBufLD[i];
		ftHandle[i] = NULL;
	}

	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]);
	}

    signal(SIGINT, quit);		// trap ctrl-c call quit fn 
		
	for(i = 0; ( (i <MAX_DEVICES) && (i < iNumDevs) ) ; i++) {
	
		memset(&ftDCB, 0, sizeof(FTDCB));
		
		ftHandle[i] = FT_W32_CreateFile(
						cBufLD[i],
						0,		// GENERIC_READ|GENERIC_WRITE ignored
						0,		// ignored
						0, 		// ignored
						0,		// OPEN_EXISTING ignored
						FT_OPEN_BY_SERIAL_NUMBER, // ignored FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FT_OPEN_BY_SERIAL_NUMBER,
						0		// ignored
						);

		/* Setup */
		if(ftHandle[i] == (FT_HANDLE)INVALID_HANDLE_VALUE){
			/* 
				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_W32_CreateFile(%d), device\n", i);
			return 1;
		}
	
		printf("Opened device %s\n", cBufLD[i]);
		
		ftDCB.BaudRate = 9600;		
		
		if(FT_W32_SetCommState(ftHandle[i], &ftDCB) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_SetCommState = %d)\n", ftStatus);
			return 1;
		}
		
		if(FT_W32_GetCommState(ftHandle[i], &ftnewDCB) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_GetCommState = %d)\n", ftStatus);
			return 1;
		}
		
		if(ftnewDCB.BaudRate != ftDCB.BaudRate) {
			printf("New baud rate does not match baud rate set\n");
		}
				
		iDevicesOpen++;		
		
		cBufWrite[0] = 'h';
		cBufWrite[1] = 'e';
		cBufWrite[2] = 'l';
		cBufWrite[3] = 'l';
		cBufWrite[4] = 'o';
		cBufWrite[5] = ' ';
		cBufWrite[6] = 'f';
		cBufWrite[7] = 't';
		cBufWrite[8] = 'd';
		cBufWrite[9] = 'i';
		cBufWrite[10] = 'w';
		cBufWrite[11] = 'o';
		cBufWrite[12] = 'r';
		cBufWrite[13] = 'l';
		cBufWrite[14] = 'd';
		cBufWrite[15] = '\n';
		cBufWrite[16] = '\0';
		
		if(FT_W32_WriteFile(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesWritten, NULL) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_WriteFile = %d)\n", ftStatus);
			return 1;
		}
	
		sleep(1);	// give it a chance to be received in a loopback test
		
		if(FT_W32_ClearCommError(ftHandle[i], &dwErrors, &ftComStat) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_ClearCommError = %d)\n", ftStatus);
			return 1;
		}

		printf("ftComStat.cbInQue = %d\n", ftComStat.cbInQue);

		
		FT_GetQueueStatus(ftHandle[i], &dwErrors);
		dwRead = ftComStat.cbInQue;
		if(ftComStat.cbInQue > BUF_SIZE) {
			dwRead = BUF_SIZE;
		}
		else {
			dwRead = ftComStat.cbInQue;
		
		}
		
		if(FT_W32_ReadFile(ftHandle[i], cBufWrite, dwRead, &dwBytesRead, NULL) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_ReadFile = %d)\n", ftStatus);
			return 1;
		}
		
		printf("dwBytesRead = %d\n", dwBytesRead);

	}
	
	printf("i = %d\n", i);
	iDevicesOpen = i;
	printf("iDevicesOpen = %d\n", iDevicesOpen);
	/* Cleanup */
	for(i = 0; i < iDevicesOpen; i++) {
		if(ftHandle[i] != NULL) {
			if(FT_W32_CloseHandle(ftHandle[i]) == FALSE) {
				ftStatus = FT_W32_GetLastError(ftHandle[i]);
				printf("Error FT_W32_CloseHandle = %d, device = %d)\n", ftStatus, i);
			}
			printf("Closed device %s\n", cBufLD[i]);
			ftHandle[i] = NULL;
		}
	}

	return 0;
}
コード例 #18
0
ファイル: main.c プロジェクト: mmorrison24/Gateway
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;
}