在ubuntu中,我无法从C程序中通过usb串行端口发送两字节命令 我想用C或C++编写一个程序来控制一个索尼相机,它连接在一个USB串行设备上(应用逻辑USB到LANC(0600)),它运行在我的虚拟箱客户OS,Ubuntu 12.04。

在ubuntu中,我无法从C程序中通过usb串行端口发送两字节命令 我想用C或C++编写一个程序来控制一个索尼相机,它连接在一个USB串行设备上(应用逻辑USB到LANC(0600)),它运行在我的虚拟箱客户OS,Ubuntu 12.04。,ubuntu,usb,c,serial-port,Ubuntu,Usb,C,Serial Port,通过putty,我可以成功地向相机发送命令,相机会相应地进行放大和缩小。当我试图在C程序中模拟相同的行为时(甚至从命令行,echo-en'\x28\x3b'>/dev/ttyUSB0),我没有从相机得到响应 执行以下命令后,nwrited等于2,并且buf包含我发送的命令,因此它似乎工作正常,但相机再次没有响应 unsigned char cmd[2]; //cmd[0] = 0x28; //cmd[1] = 0x39; // zoom out cmd[0] = 0x28; cmd[1] = 0

通过putty,我可以成功地向相机发送命令,相机会相应地进行放大和缩小。当我试图在C程序中模拟相同的行为时(甚至从命令行,
echo-en'\x28\x3b'>/dev/ttyUSB0
),我没有从相机得到响应

执行以下命令后,
nwrited
等于
2
,并且
buf
包含我发送的命令,因此它似乎工作正常,但相机再次没有响应

unsigned char cmd[2];
//cmd[0] = 0x28;
//cmd[1] = 0x39; // zoom out
cmd[0] = 0x28;
cmd[1] = 0x3b; // zoom out
int nwritten = write (fd, cmd, 2);

.. sleeping...

char buf [100];
int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read
putty中的串行通信设置为:

/dev/ttyUSB0
9600 baud
Data bits: 8
Stop bits: 1
Parity: NONE
Flow Control:  XON/XOFF
我尝试在代码中匹配这些,但不确定是否成功

有关设备的其他信息:

$ lsusb
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 001 Device 003: ID 0403:6001 Future Technology Devices International, Ltd FT232 USB-Serial (UART) IC

$ dmesg | grep FTDI
my output:
[22531.630601] USB Serial support registered for FTDI USB Serial Device
[22531.630628] ftdi_sio 1-2:1.0: FTDI USB Serial Device converter detected
[22531.651525] usb 1-2: FTDI USB Serial Device converter now attached to ttyUSB0
[22531.651564] ftdi_sio: v1.6.0:USB FTDI Serial Converters Driver

以下是*nix上串行I/O的一般概述,以及针对您的具体情况的一些注意事项:

在Linux/Unix中,在C/C++中设置串行端口可能很困难,因为有许多可用选项。termios库目前常用于设置所有这些参数。许多配置参数都来自古代,当时人们使用物理计算机终端(例如谷歌VT100),每个型号的RS-232(或类似)接口上的配置略有不同。其他通常不相关的参数来自通过电话系统连接音频调制解调器的时代

您需要做出的第一个也是最重要的配置决策是,是否应将串行端口设置为在规范模式或非规范模式下运行。使用哪一种取决于您正在使用的设备

简而言之,规范模式面向那些行为类似于终端的设备,在这些设备中,您可以键入一行文本,必要时使用退格进行更正,并使用Enter或Return提交该行以供shell执行

另一方面,非规范模式更适合表示换行符和控制字符的字节没有特殊意义的二进制数据

因为我假设你只会给你的相机写东西,那么你可能只想给相机发送一串字节,而不必担心行或任何事情,所以这取决于你使用规范模式还是非规范模式

您需要做出的另一个决定是使用阻塞还是非阻塞I/O。阻塞I/O意味着在调用read()和write()函数时,您在该指令上编程“阻塞”,直到read()或write()完成。非阻塞意味着read()和write()立即返回,代码继续运行,I/O操作在后台继续。如果非阻塞I/O出现问题,操作系统会异步(随时)向您的程序发送信号

从根本上讲,阻塞I/O更易于编写,但并不总是像非阻塞I/O那样执行。在您的情况下(一个简单的程序),您可能应该使用阻塞I/O。使用阻塞或非阻塞模式的决定是在打开设备时做出的,但可以随时更改。警告:一些串行端口最初必须作为非阻塞设备打开,然后转换为阻塞设备。原因是设备将阻塞open(),等待调制解调器就绪信号。非阻塞打开设备有效地绕过了这一点

最后,您必须配置波特率、奇偶校验和开始/停止位。维基百科上关于UART的文章很好地描述了这一点。您已经拥有PuTTY配置中的设置

下面是一个玩具程序(可使用gcc编译),演示了如何使用termios库在规范模式下设置串行设备。您应该编写一个函数,将命令映射到相机使用的“字符代码”

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

static const speed_t DEFAULT_BAUD = B9600;

int usage(const char* progName)
{
    printf("%s PATH_TO_SERIAL_PORT COMMAND\n", progName);
    printf("Valid COMMAND:\n");
    printf("zoomO\n");
    printf("zoomI\n");
    exit(1);
}

int main(int argc, char **argv)
{
    struct termios terminalConfig;
    struct termios checkConfig;
    const char* progName = argv[0];
    const char* devicePath;
    const char* command;
    int deviceDescriptor;
    int flags;

    if (argc < 3) usage(progName);

    devicePath = argv[1];
    command = argv[2];

    // Open for read/write, process is not controlled by the serial device
    // (prevents spurious ^Cs, etc from killing us) and we open nonblocking
    // to avoid waiting for the data carrier detect (DCD) signal which can
    // doesn't exist and can cause blocking on some devices
    deviceDescriptor = open(devicePath, O_RDWR|O_NOCTTY|O_NONBLOCK);
    if (deviceDescriptor < 0 || !isatty(deviceDescriptor))
    {
        printf("Error opening serial port\n");
        exit(1);
    }

    // Set back to blocking I/O
    flags = fcntl(deviceDescriptor, F_GETFL); // Read current flags
    flags &= ~O_NONBLOCK; // Modify for blocking
    if (fcntl(deviceDescriptor, F_SETFL, flags) == -1) exit(1); // Check for error

    // Get the current configuration
    tcgetattr(deviceDescriptor, &terminalConfig);

    // Non-canonical (raw) mode. For canonical mode use
    // terminalConfig.c_lflag |= (ICANON | ECHO | ECHOE)
    cfmakeraw(&terminalConfig);
    terminalConfig.c_cflag |= CLOCAL|CREAD; // No carrier detect/enable rx
    terminalConfig.c_cflag &= ~CRTSCTS; // Disable rts/cts lines

    // Set speed to default baud
    cfsetispeed(&terminalConfig, DEFAULT_BAUD); // in speed
    cfsetospeed(&terminalConfig, DEFAULT_BAUD); // out speed

    // Clear the line before setting config
    tcflush(deviceDescriptor, TCIOFLUSH);

    // Set our config
    tcsetattr(deviceDescriptor, TCSANOW, &terminalConfig);
    // Check it back to see if it worked (tcsetattr doesn't return a meaningful value)
    tcgetattr(deviceDescriptor, &checkConfig);
    // This might be a little harsh, we'll find out in practice
    if (memcmp(&terminalConfig, &checkConfig, sizeof(terminalConfig)) != 0) exit(1);

    // Write command to port (write a function to map commands to byte codes)
    // Don't forget to add a '\n' and possibly a '\r' after the character code!
    write(deviceDescriptor, command, strlen(command));
    // Block until sent. Not too useful here, but can be when you must complete the send
    // before proceeding..
    tcdrain(deviceDescriptor);

    // Close the device
    close(deviceDescriptor);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
静态常数速度默认值波特=B9600;
int用法(常量字符*progName)
{
printf(“%s PATH\u TO\u SERIAL\u PORT COMMAND\n”,progName);
printf(“有效命令:\n”);
printf(“zoomO\n”);
printf(“zoomI\n”);
出口(1);
}
int main(int argc,字符**argv)
{
结构终端配置;
结构termios checkConfig;
const char*progName=argv[0];
const char*设备路径;
const char*命令;
int设备描述器;
国际旗帜;
if(argc<3)用法(progName);
devicePath=argv[1];
命令=argv[2];
//打开读/写,进程不受串行设备控制
//(防止虚假^Cs等杀死我们)我们打开非阻塞
//避免等待数据载波检测(DCD)信号,该信号可以
//不存在,可能导致某些设备阻塞
deviceDescriptor=打开(设备路径,O|u RDWR | O|u NOCTTY | O|u非块);
if(deviceDescriptor<0 | | |!isatty(deviceDescriptor))
{
printf(“打开串行端口时出错\n”);
出口(1);
}
//设置回阻塞I/O
flags=fcntl(deviceDescriptor,F_GETFL);//读取当前标志
标志&=~O_NONBLOCK;//修改以进行阻塞
如果(fcntl(deviceDescriptor,F_SETFL,flags)=-1)退出(1);//检查错误
//获取当前配置
tcgetattr(设备描述器和终端配置);
//非规范(原始)模式。用于规范模式
//终端配置c|U lflag |=(ICANON |回声|回声)
cfmakeraw(和终端配置);
terminalConfig.c|u cflag |=CLOCAL | CREAD;//无载波检测/启用接收
terminalConfig.c\u cflag&=~CRTSCTS;//禁用rts/cts线路
//将速度设置为默认波特率
cfsetispeed(&terminalConfig,默认值_波特);//速度
cfsetospeed(&terminalConfig,默认值_波特);//输出速度
//在设置配置之前清除该行
tcflush(设备描述器,TCIOFLUSH);
//设置我们的配置
tcsetattr(设备描述器、TCSANOW和终端配置);
//检查一下bac