为什么FT_Read()在子进程中失败但在父进程中起作用? [英] Why does FT_Read() fail in the child process but work in the parent process?

查看:393
本文介绍了为什么FT_Read()在子进程中失败但在父进程中起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序,该程序使用ftd2xx库将字节写入USB设备,然后读取答复.

I have the following program, which uses the ftd2xx library to write a byte to an USB device and then reads the reply.

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include "../ftd2xx.h"

void fatal (const char *format,...) {
  va_list argp;
  fprintf(stderr, "FATAL: ");
  va_start(argp, format);
  vfprintf(stderr, format, argp);
  va_end(argp);
  fprintf(stderr, "\n");
  exit(3);
}

int main(int argc, char* argv[]) {
  int pid = fork();
  if (pid > 0) { //replace with: if (pid == 0) {
    FT_STATUS ftStatus;
    printf("before FT_OpenEx\n");
    FT_HANDLE ftHandle;  
    if ((ftStatus = FT_OpenEx("DA011SCV", FT_OPEN_BY_SERIAL_NUMBER, &ftHandle)) != FT_OK) fatal("FT_OpenEx failed");
    printf("before FT_Write\n");
    uint8_t buffer[1];
    buffer[0] = 0x55;
    DWORD bytesWritten;
    if ((ftStatus = FT_Write(ftHandle, buffer, sizeof(buffer), &bytesWritten)) != FT_OK) fatal("FT_Write failed");
    printf("before FT_Read\n");
    DWORD bytesRead;
    if ((ftStatus = FT_Read(ftHandle, buffer, 1, &bytesRead)) != FT_OK) fatal("FT_Read failed");
    if (bytesRead > 0) {
      printf("FT_Read data=0x%02X\n", buffer[0]);
    } else {
      printf("FT_Read no data\n");
    }
    printf("before FT_Close\n");
    if ((ftStatus = FT_Close(ftHandle)) != FT_OK) fatal("FT_Close failed");
    printf("press Enter to exit\n");
  }
  getchar();
  exit(0);
}

如下所示的代码将产生以下输出:

The code as shown produces this output:

//Output if (pid > 0)
before FT_OpenEx
before FT_Write
before FT_Read
FT_Read data=0x55
before FT_Close
press Enter to exit

但是,如果我将第一个if的条件从(pid > 0)更改为(pid == 0),即如果我在子进程中进行USB通信,则程序会挂在FT_Read()函数和输出中是:

However, if I change the condition of the first if from (pid > 0) to (pid == 0), i.e. if I do the USB communication in the child process, then the program hangs in the FT_Read() function and the output is:

//Output if (pid == 0)
before FT_OpenEx
before FT_Write
before FT_Read

为什么会这样?

一些细节:

  • 设备中的USB芯片是具有出厂设置的FT240X.
  • USB设备就像回声一样:它接收到的每个字节都会立即发回.
  • 我用协议分析器检查了传输的字节值是否正确.
  • ftd2xx库版本为1.1.12.

推荐答案

您所描述的内容听起来像是ftd2xx库中存在一个错误-该库在加载时可能会执行一些初始化,而在该过程中将变得无效ID更改.

What you're describing sounds like there's a bug in the ftd2xx library -- it's possible that the library performs some initialization when it's loaded which becomes invalid when the process ID changes.

ftd2xx库是开源的,并根据禁止进行逆向工程的许可证进行分发,因此我无法确定正在发生的事情.您可能想尝试使用开放源FTDI库,例如 libftdi

The ftd2xx library is closed-source and distributed under a license that prohibits reverse-engineering, so there's no way for me to tell for sure what's going on. You may want to try using an open-source FTDI library, such as libftdi, instead.

这篇关于为什么FT_Read()在子进程中失败但在父进程中起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆