比特敲打与USB串行UART

Bit-banging with USB to Serial UART

本文关键字:USB 串行 UART      更新时间:2023-10-16

我刚刚购买了UM232R USB串行UART开发模块,该模块使用FT232RL芯片在USB上模拟类似UART的接口。
实际上,我买这个复杂的模块只是为了一个非常简单的目的:触发我自己做的一个非常简单的LED电路。因此,我想要的是"bit-bang"模块的第一个bit-bang引脚"CB0"(引脚23)[见数据表第8/9页]。使用c++或AHK(或者可能是Python,尽管我并不真正了解它),这并不重要。而且它需要在Windows上运行。

我已经尝试过了:
我找到了一个很好的教程,教你如何使用FTDI设备。但首先,我安装了VCP驱动程序,或者更准确地说,是表右侧的"setup executable"。这不仅安装了VCP驱动程序,还安装了D2XX驱动程序。然后我下载了D2XX驱动程序的压缩包(用于windows的那个)。

好的,那么:

    我创建了一个新的Visual c++项目(Win32控制台应用程序)预编译头)。
  • 我提取了项目文件夹中的D2XX驱动程序。
  • 我在项目中添加了ftd2xx.h头文件。我从提到的教程中取出这段代码,并将其修改为:

(我实际上只是添加了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;
}

(如果我没记错的话,这个示例程序应该触发我的设备上的每个(或大多数)可敲位引脚。)但是当我试图构建它时,我得到了一些奇怪的链接器错误:

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:UsersusernameDocumentsVisual Studio 2010ProjectsFTDI-ProjectDebugFTDI-Project.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

这一切对我来说似乎难以置信地复杂。我希望你们中有人能帮我。我真的很感激!

由于您正在获得FTDI库函数的未解决的引用错误,您应该检查项目的链接器设置,并确保您以某种方式告诉链接器在哪里可以找到FTDI库。可能有一个由FTDI提供的文件名以"结尾的文件。您需要将其添加到"附加链接器输入"列表中。

您似乎也对是否使用VCP驱动程序或D2xx驱动程序对此设备感到困惑。您不能同时使用两者,您应该通过在设备管理器中检查设备来确保您正在使用D2xx驱动程序,否则您将在路上遇到运行时错误。

另外,请注意,教程是5年前的,所以你可能需要参考FTDI的实际文档来获取最新信息。