Как прочитать устройство / dev / ttyUSB0 в Linux?

0

Я подключил USB RFID в последовательном порту и использовал для чтения RFID-тегов. Поэтому мне нужно прочитать данные с устройства и обработать вывод. Я получаю данные с помощью команды экрана из командной строки Linux, но я не смог принять значение с экрана и не могу перейти к моему приложению. Есть ли другой способ прочитать /dev/ttyUSB0? Я использовал код, показанный ниже, но он показывает, что ресурс недоступен (хотя я дал разрешения chmod)

#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);
}
Теги:
sockets
rfid

1 ответ

1

У вас есть O_NONBLOCK в параметрах для open. Когда вы теперь вызываете read и в настоящее время нет данных для чтения, вы получаете эту ошибку. Вы можете либо удалить O_NONBLOCK либо выполнить чтение в цикле, пока не получите что-то еще, кроме "недоступного ресурса".

  • 0
    Я удалил НЕБЛОКИРОВКУ, все еще я сталкиваюсь с той же самой ошибкой. Ресурс недоступен. Или есть ли способ перенести данные с экрана в канал. так что я могу читать из трубы

Ещё вопросы

Сообщество Overcoder
Наверх
Меню