For*_*vin 5 c++ serial-port visual-c++ uart ftdi
我刚刚购买了UM232R USB串行UART开发模块,该模块使用FT232RL芯片通过USB模拟类似UART的接口.
我实际上只是为了一个非常简单的目的而买了这个复杂的模块:触发一个我自己构建的非常简单的LED电路.所以我想要的是"咬一下"模块的第一个可以进位的引脚"CB0"(引脚23)[参见数据表中的第8/9页].使用C++或AHK(或者Python,即使我真的不知道它),它并不重要.它需要在Windows上运行.
到目前为止我所尝试的内容:
我找到了一个关于如何对FTDI设备进行bit-bang的精彩教程.
但首先,我安装了VCP驱动程序,或者更准确地说明了表格右侧的"设置可执行文件".这不仅安装了VCP驱动程序,还安装了D2XX驱动程序.然后我下载了D2XX驱动程序作为zip(一个用于Windows).
好吧:
(我实际上只是添加了windows.h,stdafx.h并修改为#include <ftd2xx.h> #include "ftd2xx.h")
/* 8-bit PWM on 4 LEDs using FTDI cable or breakout.
This example uses the D2XX API.
Minimal error checking; written for brevity, not durability. */
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "ftd2xx.h"
#define LED1 0x08 /* CTS (brown wire on FTDI cable) */
#define LED2 0x01 /* TX (orange) */
#define LED3 0x02 /* RX (yellow) */
#define LED4 0x14 /* RTS (green on FTDI) + DTR (on SparkFun breakout) */
int _tmain(int argc, _TCHAR* argv[])
{
int i,n;
unsigned char data[255 * 256];
FT_HANDLE handle;
DWORD bytes;
/* Generate data for a single PWM 'throb' cycle */
memset(data, 0, sizeof(data));
for(i=1; i<128; i++) {
/* Apply gamma correction to PWM brightness */
n = (int)(pow((double)i / 127.0, 2.5) * 255.0);
memset(&data[i * 255], LED1, n); /* Ramp up */
memset(&data[(256 - i) * 255], LED1, n); /* Ramp down */
}
/* Copy data from first LED to others, offset as appropriate */
n = sizeof(data) / 4;
for(i=0; i<sizeof(data); i++)
{
if(data[i] & LED1) {
data[(i + n ) % sizeof(data)] |= LED2;
data[(i + n * 2) % sizeof(data)] |= LED3;
data[(i + n * 3) % sizeof(data)] |= LED4;
}
}
/* Initialize, open device, set bitbang mode w/5 outputs */
if(FT_Open(0, &handle) != FT_OK) {
puts("Can't open device");
return 1;
}
FT_SetBitMode(handle, LED1 | LED2 | LED3 | LED4, 1);
FT_SetBaudRate(handle, 9600); /* Actually 9600 * 16 */
/* Endless loop: dump precomputed PWM data to the device */
for(;;) FT_Write(handle, &data, (DWORD)sizeof(data), &bytes);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
(如果我没有弄错的话,这个示例程序应该触发我设备上的每个(或大部分)可位数的引脚.)但是当我尝试构建它时,我得到了一些奇怪的链接器错误:
1>------ Build started: Project: FTDI-Project, Configuration: Debug Win32 ------
1> FTDI-Project.cpp
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_Write@16 referenced in function _wmain
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_SetBaudRate@8 referenced in function _wmain
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_SetBitMode@12 referenced in function _wmain
1>FTDI-Project.obj : error LNK2019: unresolved external symbol __imp__FT_Open@8 referenced in function _wmain
1>C:\Users\username\Documents\Visual Studio 2010\Projects\FTDI-Project\Debug\FTDI-Project.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)
对我来说这一切看起来都非常复杂.我希望有人可以帮助我.我真的很感激!
由于您遇到 FTDI 库函数未解决的引用错误,因此您应该检查项目的链接器设置,并确保以某种方式告诉链接器在哪里可以找到 FTDI 库。FTDI 可能提供了一个名称以“.lib”结尾的文件,您需要将其添加到“其他链接器输入”列表中。
此外,您似乎对该设备使用的是 VCP 驱动程序还是 D2xx 驱动程序感到困惑。您不能同时使用两者,并且您应该通过在设备管理器中检查设备来确保您正在使用 D2xx 驱动程序,否则您将会遇到运行时错误。
另请注意,该教程已有 5 年历史,因此您可能必须参考 FTDI 的实际文档才能获取更新信息。
| 归档时间: |
|
| 查看次数: |
1887 次 |
| 最近记录: |