嵌入式linux usb-ftdi串口读取问题

时间:2013-10-08 15:50:30

标签: serial-port embedded-linux usbserial

我有一个运行嵌入式Linux的基于TI Cortex的Pengwyn板,我试图用来从USB-ftdi外设读取原始串行数据,因此我可以将其处理成数据包。

为此,我编写了一个简单的程序(使用Qt)和termios框架。这在我的桌面(Ubuntu VM)机器上没有问题 - 它打开/ dev / ttyUSB0,配置串口,读取和处理数据没有问题。

当我在pengwyn板上运行相同的程序(明显交叉编译)时遇到问题... read()函数调用不会填充缓冲区 - 尽管它返回一个非错误值,表明已读取的字节数???

(我还必须为pengwyn板重建linux内核以包含USB-ftdi串行驱动程序。)

我怀疑这是一个目标配置问题,但我已经比较了两个平台之间的文件权限和termios设置,看起来没问题。我是这个平台和serial / termios的新手,所以我毫无疑问地看了一些东西,但我已经查看了POSIX操作系统的#34;串行编程指南"并搜索了关于arm和usb-ftdi阅读问题的类似帖子,但还没有找到任何东西。

有任何建议/意见吗?

测试程序的相关摘录:

void Test::startRx(void)
{   
    bool retval = false;
    m_fd = open(m_port.toLocal8Bit(),O_RDONLY |O_NOCTTY | O_NDELAY);
    if (m_fd == -1)
    {
        qDebug() << "Unable to open: " << m_port.toLocal8Bit() << strerror(errno);
    }
    else
    {
        m_isConnected = true;
        qDebug() << m_port.toLocal8Bit() << "is open...";
        fcntl(m_fd, F_SETFL, 0);

        struct termios options;
        if (tcgetattr(m_fd, &options)!=0)
        {
           qDebug() << "tcgetattr() failed";
        }

        //Set the baud rates to 9600
        cfsetispeed(&options, B9600);
        cfsetospeed(&options, B9600);

        //Enable the receiver and set local mode
        options.c_cflag |= (CLOCAL | CREAD);

        //Set character size
        options.c_cflag &= ~CSIZE; /* Mask the character size bits */
        options.c_cflag |= CS8;    /* Select 8 data bits */

        //No parity 8N1:
        options.c_cflag &= ~PARENB;
        options.c_cflag &= ~CSTOPB;
        options.c_cflag &= ~CSIZE;
        options.c_cflag |= CS8;

        //Disable hardware flow control
        options.c_cflag &= ~CRTSCTS;

        //Disable software flow control
        options.c_iflag &= ~(IXON | IXOFF | IXANY);

        //Raw output
        options.c_oflag &= ~OPOST;

        //Raw input
        options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

        //Set the new options for the port...
        tcsetattr(m_fd, TCSANOW, &options);

        int status;
        ioctl(m_fd, TIOCMGET, &status );
        qDebug() << "current modem status:" << status;


        QThread* m_reader_thread = new QThread;
        m_serial_port_reader = new SerialPortReader(0, m_fd);

        if (m_serial_port_reader)
        {            
            qDebug() << "creating serial port reader thread...";
            m_serial_port_reader->moveToThread(m_reader_thread);
            connect(m_serial_port_reader, SIGNAL(notifyRxPacketData(QByteArray *)), this, SLOT(processRxPacketData(QByteArray*)));

            //connect(m_serial_port_reader, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
            connect(m_reader_thread, SIGNAL(started()), m_serial_port_reader, SLOT(process()));
            connect(m_serial_port_reader, SIGNAL(finished()), m_reader_thread, SLOT(quit()));
            connect(m_serial_port_reader, SIGNAL(finished()), m_serial_port_reader, SLOT(quit()));
            connect(m_reader_thread, SIGNAL(finished()), m_reader_thread, SLOT(deleteLater()));
            m_reader_thread->start();
            retval = true;
        }

....

void SerialPortReader::readData(int i)
{
    m_socket_notifier->setEnabled(false);
    if (i == m_fd)
    {
        unsigned char buf[BUFSIZE] = {0};
        int bytesRead = read(m_fd, buf, BUFSIZE);
        //qDebug() << Q_FUNC_INFO << "file decriptor:" << m_fd << ", no. bytes read:" << bytesRead;

        if (bytesRead < 0)
        {
          qDebug() << Q_FUNC_INFO << "serial port read error";
          return;
        }
        // if the device "disappeared", e.g. from USB, we get a read event for 0 bytes
        else if (bytesRead == 0)
        {
           //qDebug() << Q_FUNC_INFO << "finishing!!!";
           return;
        }
        //process data...
    }
    m_socket_notifier->setEnabled(true);
}

0 个答案:

没有答案