В чем может быть разница между встроенной последовательной операцией чтения и настольным ПК - PullRequest
0 голосов
/ 21 февраля 2019

Во встроенной системе Linux я кодирую проект, который тестирует последовательный порт в среде с петлевым питанием.Это означает, что я подключаю rx-tx. Периферийный выход - RS-232

. Чтобы проверить порт, я отправляю 1 байт, затем читаю отправленный байт.Я повторяю этот цикл от 0x00 до 0XFF.Я использую необработанный тип ввода для UART.

Выглядит нормально, если я запускаю свой код на настольном ПК с Linux.

Однако в моей встроенной Linux-системе я не могу правильно читать с соединения RS-232.В конце я получил чтение, возвращающее ноль.

Что вы думаете о возможной проблеме?

Я проверяю свою конфигурацию termios, указанную в Руководстве по последовательному программированию для операционных систем POSIX

         StatusResult  UartInterface::openComPort() {

           m_fileDesc = open(m_device.c_str(), O_RDWR | O_NOCTTY );

            if (m_fileDesc == -1) {
                retStatus.type = COMM_ERROR;
            }
            configureUART();
            return retStatus;
        }


        void UartInterface::configureUART(){

                struct termios options;

                tcgetattr(m_fileDesc, &options);

                cfsetispeed(&options, B9600);
                cfsetospeed(&options, B9600);

                options.c_cflag |= (CLOCAL |CREAD);

                tcsetattr(m_fileDesc, TCSANOW, &options);

                options.c_cflag &= ~CSIZE;
                options.c_cflag |= CS8;
                options.c_cflag &= ~PARENB;
                options.c_cflag &= ~CSTOPB;
                options.c_cflag &= ~CRTSCTS;
                /*=============================================*/

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

                /*=============================================*/

                options.c_iflag &= (IXON | IXOFF | IXANY);

                /*=============================================*/

                options.c_oflag &= ~OPOST;

                /*=============================================*/

                options.c_cc[VMIN] = 0;
                options.c_cc[VTIME] = 10;

                tcsetattr(m_fileDesc, TCSANOW, &options);

            }

Вот мой основной цикл тестирования

    std::cout<<"-----------UART DEBUG-------------\n";


    while( int(write_data) < 255 ){
        n = write(m_fileDesc, &write_data, 1);
        if( n != 1) {
            std::cout << "UART write failed!\n";
            res=false;
            return res;
        }

        n = read(m_fileDesc, &read_data, 1);
        if ( n == 1){
            if(read_data != write_data) {
                std::cout << "UART mismatch error!\t data_read:0x" << int(read_data)<<"   data write:0x"<<int(write_data)<< std::endl;
                res=false;
                //return res;
            }

            std::cout<<std::hex<<"Byte: 0x"<<int(read_data) <<"  is OK!"<<std::endl;


        }
        else {
            std::cout << "UART read failed! Res: "<<n<<"Errno"<< strerror(errno)<<std::endl;
            res=false;
            return res;
        }
        write_data++;
    }
    (res) ? (std::cout<<"Uart interface test OK!"<<std::endl) : ((std::cout<<"UART FAILED!!"<<std::endl));
    return res;
}

Вот вывод для встроенной системы Linux. Как вы видите, чтение и запись данных совершенно разные.После количества повторений оно заканчивается нулевым показанием?

    -----------UART DEBUG-------------
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x1
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x2
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x3
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x4
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x5
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x6
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x7
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x8
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x0   data write:0x9
Byte: 0x0  is OK!
UART mismatch error!     data_read:0x1   data write:0xa
Byte: 0x1  is OK!
UART mismatch error!     data_read:0x2   data write:0xb
Byte: 0x2  is OK!
UART mismatch error!     data_read:0x3   data write:0xc
Byte: 0x3  is OK!
UART mismatch error!     data_read:0x4   data write:0xd
Byte: 0x4  is OK!
UART mismatch error!     data_read:0x5   data write:0xe
Byte: 0x5  is OK!
UART mismatch error!     data_read:0x6   data write:0xf
Byte: 0x6  is OK!
UART mismatch error!     data_read:0x7   data write:0x10
Byte: 0x7  is OK!
UART mismatch error!     data_read:0x8   data write:0x11
Byte: 0x8  is OK!
UART mismatch error!     data_read:0x9   data write:0x12
Byte: 0x9  is OK!
UART mismatch error!     data_read:0xa   data write:0x13
Byte: 0xa  is OK!
UART mismatch error!     data_read:0xb   data write:0x14
Byte: 0xb  is OK!
UART mismatch error!     data_read:0xc   data write:0x15
Byte: 0xc  is OK!
UART mismatch error!     data_read:0xd   data write:0x16
Byte: 0xd  is OK!
UART mismatch error!     data_read:0xe   data write:0x17
Byte: 0xe  is OK!
UART mismatch error!     data_read:0xf   data write:0x18
Byte: 0xf  is OK!
UART mismatch error!     data_read:0x10   data write:0x19
Byte: 0x10  is OK!
UART mismatch error!     data_read:0x12   data write:0x1a
Byte: 0x12  is OK!
UART read failed! Res: 0 Errno: No such file or directory
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...