示例#1
0
文件: fld_ctrl.c 项目: avasilje/btcar
void send_over_ftdi(BYTE servo_acc, BYTE servo_whl)
{
    int n_rc;
    BYTE data[3];
    DWORD dw_byte_to_write, dw_bytes_written;
    DWORD n_gle;

    data[0] = 0x17; // Stream - 0x17 servo control
    data[1] = (BYTE)servo_acc;
    data[2] = (BYTE)servo_whl;

    dw_byte_to_write = sizeof(data);

    n_rc = FT_W32_WriteFile(
        gh_dev,
        data, 
        dw_byte_to_write,
        &dw_bytes_written, 
        &gt_dev_tx_overlapped);


    if (!n_rc)
    {
        n_gle = FT_W32_GetLastError(gh_dev);
        if (n_gle != ERROR_IO_PENDING)
        {
            n_rc = FALSE;
            // goto cleanup_dev_tx;
        }
    }

    n_rc = FT_W32_GetOverlappedResult(gh_dev, &gt_dev_tx_overlapped, &dw_bytes_written, TRUE);
    if (!n_rc || (dw_byte_to_write != dw_bytes_written))
    {
        n_rc = FALSE;
    }

}
示例#2
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, dwErrors;
	FT_STATUS	ftStatus;
	FT_HANDLE	ftHandle[MAX_DEVICES];
	int	iNumDevs = 0;
	int	i, j;
	int	iDevicesOpen;
	FTDCB ftDCB, ftnewDCB;
	FTCOMSTAT ftComStat;
	FTTIMEOUTS ftTimouts, ftnewTimouts;
	struct timeval tv;
	struct timezone	tz;
	
	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++) {
	
		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");
		}

		ftTimouts.ReadIntervalTimeout = 0;			// ignored by library
		ftTimouts.ReadTotalTimeoutMultiplier = 0;	// ignored by library
		ftTimouts.ReadTotalTimeoutConstant = 1000;	// 1 second
		ftTimouts.WriteTotalTimeoutMultiplier = 0;	// ignored by library
		ftTimouts.WriteTotalTimeoutConstant = 1000;	// 1 second
		
		// 1 second for both read and write		
		if(FT_W32_SetCommTimeouts(ftHandle[i], &ftTimouts) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_GetCommState = %d)\n", ftStatus);
			return 1;
		}
		
		if(FT_W32_GetCommTimeouts(ftHandle[i], &ftnewTimouts) == FALSE) {
			ftStatus = FT_W32_GetLastError(ftHandle[i]);
			printf("Error FT_W32_GetCommState = %d)\n", ftStatus);
			return 1;
		}
		else {
			printf("ftTimouts.ReadIntervalTimeout = %d\n", ftTimouts.ReadIntervalTimeout);
			printf("ftTimouts.ReadTotalTimeoutMultiplier = %d\n", ftTimouts.ReadTotalTimeoutMultiplier);
			printf("ftTimouts.ReadTotalTimeoutConstant = %d\n", ftTimouts.ReadTotalTimeoutConstant);
			printf("ftTimouts.WriteTotalTimeoutMultiplier = %d\n", ftTimouts.WriteTotalTimeoutMultiplier);
			printf("ftTimouts.WriteTotalTimeoutConstant = %d\n", ftTimouts.WriteTotalTimeoutConstant);
		}
	
		
		for(j = 0 ; j < 10; j++) {	
			if(FT_W32_ReadFile(ftHandle[i], cBufWrite, BUF_SIZE, &dwBytesRead, NULL) == FALSE) {
				ftStatus = FT_W32_GetLastError(ftHandle[i]);
				gettimeofday(&tv, &tz);
				printf("%d secs, %d usecs\n", tv.tv_sec, tv.tv_usec);
//				printf("Error FT_W32_ReadFile = %d)\n", ftStatus);
//				return 1;
			}
			else if(dwBytesRead != BUF_SIZE) {
				gettimeofday(&tv, &tz);
				printf("%d secs, %d usecs\n", tv.tv_sec, tv.tv_usec);
			}
		}
		
		printf("dwBytesRead = %d\n", dwBytesRead);

	}
	iDevicesOpen = i;
	/* Cleanup */
	for(i = 0; i < iDevicesOpen; i++) {
		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]);
	}

	if(pcBufRead)
		free(pcBufRead);
	return 0;
}
示例#3
0
void Widget::initDevice()
{
    wchar_t name[] = L"MD212rev1 #2";
    ui->textEdit->append("<font color = \"blue\">Поиск устройства MD212rev1 #2...</font>");

   ftStatus = FT_ListDevices(0, name, FT_LIST_BY_INDEX | FT_OPEN_BY_DESCRIPTION);
   if(ftStatus != FT_OK)
   {
       ui->textEdit->append("<font color = \"red\">Устройство не найдено!</font>");
       ui->pushButton->setEnabled(false);
       return;
   }
   ui->textEdit->append("Устройство найдено");
   ftHandle = FT_W32_CreateFile(name, GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FT_OPEN_BY_DESCRIPTION, 0);
   if(ftHandle == INVALID_HANDLE_VALUE)
       ui->textEdit->append(tr("<font color = \"red\">Ошибка при открытии устройства: ") + FT_W32_GetLastError(ftHandle));
   else ui->textEdit->append(tr("<font color = \"green\">Готово</font>"));
}
示例#4
0
void Widget::on_pushButton_clicked()
{
    data = ui->spinBox->text();
    DWORD dataWrited;
    char d = data.toUtf8().toInt(0, 16);

    ftStatus = FT_W32_WriteFile(ftHandle, &d, sizeof(d), &dataWrited, NULL);
    ftStatus = FT_W32_WriteFile(ftHandle, &d, sizeof(d), &dataWrited, NULL);
    ftStatus = FT_W32_WriteFile(ftHandle, &d, sizeof(d), &dataWrited, NULL);
    ftStatus = FT_W32_WriteFile(ftHandle, &d, sizeof(d), &dataWrited, NULL);
    ftStatus = FT_W32_WriteFile(ftHandle, &d, sizeof(d), &dataWrited, NULL);
    if(ftStatus != 0)
        ui->textEdit->append(tr("Значение успешно передано: ") + data);
    else ui->textEdit->append(tr("<font color = \"red\">Произошла ошибка во время передачи значнеия: ") + FT_W32_GetLastError(ftHandle));
}
示例#5
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;
}