C++ 如何在Linux中读取/dev/ttyUSB0设备?

C++ 如何在Linux中读取/dev/ttyUSB0设备?,c++,c,linux,sockets,rfid,C++,C,Linux,Sockets,Rfid,我已经在串行端口连接了一个USB RFID,并用于读取RFID标签。所以我需要从设备读取数据并处理输出。 我从Linux命令行使用screen命令获取数据,但我无法从screen获取值,无法传递到我的应用程序。 是否有其他方法读取/dev/ttyUSB0? 我使用了下面显示的代码,但它显示当前资源不可用(即使我已授予chmod权限) #包括//标准输入/输出函数 #包括 #包含//字符串函数定义 #包含//UNIX标准函数定义 #包含//文件控制定义 #包括//错误号定义 #包括//POSIX终

我已经在串行端口连接了一个USB RFID,并用于读取RFID标签。所以我需要从设备读取数据并处理输出。 我从Linux命令行使用screen命令获取数据,但我无法从screen获取值,无法传递到我的应用程序。 是否有其他方法读取/dev/ttyUSB0? 我使用了下面显示的代码,但它显示当前资源不可用(即使我已授予chmod权限)

#包括//标准输入/输出函数
#包括
#包含//字符串函数定义
#包含//UNIX标准函数定义
#包含//文件控制定义
#包括//错误号定义
#包括//POSIX终端控制定义
int main()
{
/*打开文件描述符*/
int USB=打开(“/dev/ttyUSB0”,O|RDWR | O|NONBLOCK | O|NDELAY);
/*错误处理*/
如果(USB<0)
{

//可能您在
打开
的参数中有
O_NONBLOCK
。当您现在调用
read
并且当前没有可读取的数据时,您会出现此错误。您可以删除
O_NONBLOCK
或在循环中读取,直到获得“资源不可用”以外的内容.

如果screen命令仍在运行,那么它将保持设备打开,阻止其他程序访问它-串行设备通常是“一次一个用户”您在打开或读取功能时收到错误吗?@Petesh我已完全关闭屏幕,并且在重新启动后也尝试过。它仍然显示相同的内容。@SimonFischer:i a我在读取函数时出错在读取调用之后,errno变量的内容是什么?(printf(“%i”,errno);)我已经删除了非块,但仍然面临相同的错误。资源不可用。或者是否有任何方法将数据从屏幕传送到管道。以便我可以从管道读取
#include <stdio.h>      // standard input / output functions
#include <stdlib.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions

int main()
{
    /* Open File Descriptor */
    int USB = open( "/dev/ttyUSB0", O_RDWR| O_NONBLOCK | O_NDELAY );

    /* Error Handling */
    if ( USB < 0 )
    {
        //cout << "Error " << errno << " opening " << "/dev/ttyUSB0" << ": " << strerror (errno) << endl;
        perror("USB ");
    }

    /* *** Configure Port *** */
    struct termios tty;
    memset (&tty, 0, sizeof tty);

    /* Error Handling */
    if ( tcgetattr ( USB, &tty ) != 0 )
    {
        //cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << endl;
        perror("tcgerattr ");
    }

    /* Set Baud Rate */
    cfsetospeed (&tty, B9600);
    cfsetispeed (&tty, B9600);

    /* Setting other Port Stuff */
    tty.c_cflag     &=  ~PARENB;        // Make 8n1
    tty.c_cflag     &=  ~CSTOPB;
    tty.c_cflag     &=  ~CSIZE;
    tty.c_cflag     |=  CS8;
    tty.c_cflag     &=  ~CRTSCTS;       // no flow control
    tty.c_lflag     =   0;          // no signaling chars, no echo, no canonical processing
    tty.c_oflag     =   0;                  // no remapping, no delays
    tty.c_cc[VMIN]      =   0;                  // read doesn't block
    tty.c_cc[VTIME]     =   5;                  // 0.5 seconds read timeout

    tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines
    tty.c_iflag     &=  ~(IXON | IXOFF | IXANY);// turn off s/w flow ctrl
    tty.c_lflag     &=  ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    tty.c_oflag     &=  ~OPOST;              // make raw

    /* Flush Port, then applies attributes */
    tcflush( USB, TCIFLUSH );

    if ( tcsetattr ( USB, TCSANOW, &tty ) != 0)
    {
        //cout << "Error " << errno << " from tcsetattr" << endl;
    }

    /* *** WRITE *** */

    unsigned char cmd[] = {'I', 'N', 'I', 'T', ' ', '\r', '\0'};
    //int n_written = write( USB, cmd, sizeof(cmd) -1 );

    /* Allocate memory for read buffer */
    char buf [256];
    memset (&buf, '\0', sizeof buf);

    /* *** READ *** */
    int n = read( USB, &buf , sizeof buf );

    /* Error Handling */
    if (n < 0)
    {
        //cout << "Error reading: " << strerror(errno) << endl;
        perror("read error ");
    }

    /* Print what I read... */
    //cout << "Read: " << buf << endl;
    printf("%s",buf);;

    close(USB);
}