简体   繁体   English

USB到串行UART的位冲击

[英]Bit-banging with USB to Serial UART

I just bought the UM232R USB Serial UART Development Module which uses a FT232RL chip to emulate a UART-like interface over USB. 我刚刚购买了UM232R USB串行UART开发模块 ,该模块使用FT232RL芯片通过USB模拟类似UART的接口。
I actually just bought this complicated module for a very simple purpose: to trigger a very simple LED circuit that I built myself. 我实际上只是为了一个非常简单的目的而买了这个复杂的模块:触发一个我自己构建的非常简单的LED电路。 So all I want is to "bit-bang" the first bit-bangable pin "CB0" (pin 23) [see page 8/9 in the datasheet] of the module. 所以我想要的是“咬一下”模块的第一个可以进位的引脚“CB0”(引脚23) [参见数据表中的第8/9页] Using C++ or AHK (or maybe Python, even though I don't really know it), it doesn't really matter. 使用C ++或AHK(或者Python,即使我真的不知道它),它并不重要。 And it needs to run on Windows. 它需要在Windows上运行。

What I've tried so far: 到目前为止我尝试过的:
I found a nice tutorial on how to bit-bang FTDI devices. 我找到了一个关于如何对FTDI设备进行bit-bang的很好的教程。 But first of all I installed the VCP driver or to be more accurate the "setup executable" on the very right of the table. 但首先,我安装了VCP驱动程序,或者更准确地说明了表格右侧的“设置可执行文件”。 This installed not only the VCP driver but also the D2XX driver. 这不仅安装了VCP驱动程序,还安装了D2XX驱动程序。 Then I downloaded the D2XX driver as a zip (the one for windows). 然后我下载了D2XX驱动程序作为zip(一个用于Windows)。

Okay, then: 好吧:

  • I created a new Visual C++ project (Win32 Console Application with precompiled header). 我创建了一个新的Visual C ++项目(带有预编译头的Win32控制台应用程序)。
  • I extracted the D2XX driver in the project folder. 我在项目文件夹中解压缩了D2XX驱动程序。
  • I added the ftd2xx.h header file to the project. 我将ftd2xx.h头文件添加到项目中。
  • I took this piece of code from the mentioned tutorial and modified it to this: 我从上面提到的教程中获取了这段代码并将其修改为:

(I actually just added windows.h, stdafx.h and modified to #include <ftd2xx.h> #include "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;
}

(If I'm not mistaken, this example program should trigger every (or most of the) bit-bangable pins on my device.) But when I tried to build it, I got some weird linker errors: (如果我没有弄错的话,这个示例程序应该触发我设备上的每个(或大部分)可位数的引脚。)但是当我尝试构建它时,我得到了一些奇怪的链接器错误:

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 ==========

It all just seems incredibly complicated to me. 对我来说这一切看起来都非常复杂。 I hope someone of you can help me with this. 我希望有人可以帮助我。 I would really appreciate it! 我真的很感激!

Since you are getting unresolved reference errors for FTDI library functions, you should check the linker settings for your project and make sure you are somehow telling the linker where to find the FTDI library. 由于您正在获取FTDI库函数的未解决的引用错误,因此应检查项目的链接器设置,并确保以某种方式告知链接器在何处查找FTDI库。 There is probably a file provided by FTDI whose name ends in ".lib" and you need to add it to the "Additional Linker Inputs" list. FTDI可能提供了一个文件,其名称以“.lib”结尾,您需要将其添加到“其他链接器输入”列表中。

Also you seem to be confused about whether you are using the VCP driver or the D2xx driver for this device. 此外,您似乎对是否使用VCP驱动程序或D2xx驱动程序感到困惑。 You can't use both, and you should make sure you are using the D2xx driver by inspecting the device in the device manager or else you will get runtime errors down the road. 您不能同时使用两者,并且您应该通过检查设备管理器中的设备来确保使用D2xx驱动程序,否则您将在未来遇到运行时错误。

Also, please note that tutorial is 5 years old so you might have to refer to actual documentation from FTDI to get updated information. 此外,请注意该教程已有5年历史,因此您可能需要参考FTDI的实际文档以获取更新信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM