linux - 通过 ttyACM 传输二进制数据

标签 linux serial-port usb stty

下午好

我有一个外围设备,它通过虚拟串行端口通过 USB 进行通信。使用通用 ACM 串行驱动程序在 Windows 下一切正常,例如:https://www.kernel.org/doc/Documentation/usb/linux-cdc-acm.inf

在 Linux 下,它使用 CDC ACM 驱动程序。系统日志中的一切似乎工作正常,但通信行为异常。当我连接设备时,通信开始时大约有 10 个字节丢失。接下来,每隔一个命令就可以接收到。

我的问题是: 1)本设备的通讯协议(protocol)不使用ASCII,是二进制的(可以随机包含控制字符等...)。我应该使用 stty 来配置速度、数据位、停止位和奇偶校验,还是需要为二进制通信设置更多内容? (忽略内核中的控制位并传输每个字节 - 原始数据。)

2) 知道如何测试 linux ACM 驱动程序是否正常工作,或者我应该为我的 CDC ACM 设备尝试哪些其他驱动程序吗?

谢谢你的想法!

最佳答案

当您尝试通过串行端口发送行结束字符(0x0A 和 0x0D)之类的内容时,Linux 经常会破坏它们,如果它们实际上是二进制数据而不是作为行结束字符,这可能会导致问题。

这是一个片段 from Pololu这显示了如何正确配置串行端口,然后发送和接收几个字节。特别注意调用tcsetattr的部分。

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#ifdef _WIN32
#define O_NOCTTY 0
#else
#include <termios.h>
#endif

// Gets the position of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
int maestroGetPosition(int fd, unsigned char channel)
{
  unsigned char command[] = {0x90, channel};
  if(write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");
    return -1;
  }

  unsigned char response[2];
  if(read(fd,response,2) != 2)
  {
    perror("error reading");
    return -1;
  }

  return response[0] + 256*response[1];
}

// Sets the target of a Maestro channel.
// See the "Serial Servo Commands" section of the user's guide.
// The units of 'target' are quarter-microseconds.
int maestroSetTarget(int fd, unsigned char channel, unsigned short target)
{
  unsigned char command[] = {0x84, channel, target & 0x7F, target >> 7 & 0x7F};
  if (write(fd, command, sizeof(command)) == -1)
  {
    perror("error writing");
    return -1;
  }
  return 0;
}

int main()
{
  const char * device = "/dev/ttyACM0";  // Linux
  int fd = open(device, O_RDWR | O_NOCTTY);
  if (fd == -1)
  {
    perror(device);
    return 1;
  }

#ifdef _WIN32
  _setmode(fd, _O_BINARY);
#else
  struct termios options;
  tcgetattr(fd, &options);
  options.c_iflag &= ~(INLCR | IGNCR | ICRNL | IXON | IXOFF);
  options.c_oflag &= ~(ONLCR | OCRNL);
  options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  tcsetattr(fd, TCSANOW, &options);
#endif

  int position = maestroGetPosition(fd, 0);
  printf("Current position is %d.\n", position);

  int target = (position < 6000) ? 7000 : 5000;
  printf("Setting target to %d (%d us).\n", target, target/4);
  maestroSetTarget(fd, 0, target);

  close(fd);
  return 0;
}

您可以使用 stty 命令行实用程序执行相同的操作。

关于linux - 通过 ttyACM 传输二进制数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39491856/

相关文章:

android - ADB - 未找到设备

android - 针对 Android 的 libusb 进行编译和链接

c - 为 Pro Micro 编写 Windows 驱动程序

linux - 在 linux 和 mac 中构建库

c++ - 为什么这个文件不能在 gcc 中编译?它在 VS 中运行良好

android - 未知的 Android 设备 - linux mint

node.js - Node 串口不适用于 Electron

C Socket 程序示例 - 绑定(bind)错误 : Already in use

java - Java中从VendorID、ProductID自动检测串行COM端口

Linux串口应用程序不工作