下午好,
我有一个外围设备通过虚拟串口通过usb进行通信。使用通用ACM串行驱动程序在Windows下运行良好,例如:https://www.kernel.org/doc/Documentation/usb/linux-cdc-acm.inf
在Linux下,它使用CDC ACM驱动程序。 sys日志中的所有内容似乎都可以正常工作,但通信行为却很奇怪。当我连接设备时,在通信开始时大约10个字节丢失。接下来,只收到每个第二个命令。
我的问题是: 1)该设备的通信协议不使用ASCII,它是二进制的(它可以随机包含控制字符等)。我应该使用stty来配置速度,数据位,停止位和奇偶校验,还是需要为二进制通信设置更多内容? (忽略内核中的控制位并传输每个字节 - 原始数据。)
2)任何想法,如何测试linux ACM驱动程序是否正常工作,或者我应该尝试哪些其他驱动程序用于我的CDC ACM设备?
感谢您的任何想法!
答案 0 :(得分:0)
以下是一个代码段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
命令行实用程序执行相同的操作。