2016-09-14 199 views
2

下午好,传输二进制数据通过ttyACM

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

在Linux下,它使用CDC ACM驱动程序。系统日志中的所有内容似乎都可以正常工作,但通信却表现得很奇怪。当我连接设备时,通信开始时约10个字节丢失。接下来,每个第二个命令都会收到。

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

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

感谢您的任何想法!当您尝试通过串行端口,这可能会导致问题,如果他们实际上是二进制数据,不作为行结束符给他们

+0

你只需要确保*的termios * **(一)**正确配置了非规范(又名*生*)模式,和**(B)**有软件流量控制禁用。你根本不详细;你用什么软件来执行这个数据传输?为什么它没有正确配置端口? – sawdust

+1

'stty -F/dev/ttyACM0 raw'可能会起作用(如果传输程序不是在配置这些属性)。请注意'raw'参数之前没有连字符。这是一个参数设置而不是开关。如果你输入'-raw',那么你会否定这个命令的目的。 – sawdust

+0

我没有任何软件,我使用echo -ne“\ XXY”到/ dev/ttyACMx的写字节和猫的/ dev/ttyACM记录从端口读取文件。为了查看收到的数据,我通过xxd打开我的日志文件。设置由stty完成。 我认为原始参数可能是解决方案,谢谢! – JirkaRCK

回答

0

Linux会经常裂伤之类的行结束符字符(0x0A和0X0D)。

这里是一个片段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命令行实用程序做同样的事情。