在Ubuntu Linux (11.10)中使用C++从串口读取数据

3
我希望能够用C/C++代码从Linux的串口读取数据。 由于我仍然可以使用GtkTerm甚至使用cat /dev/ttyUSB0从该串口读取数据,因此这不是硬件/驱动程序问题。
看起来串口没有正确初始化,因为在使用类似gtkterm的程序之后,读取操作就可以正常工作了。
以下是我用于初始化串口的代码(第二个版本):
UbiDriver::UbiDriver(const std::string &ttyPort)
{
    // Doc : http://www.easysw.com/~mike/serial/serial.html#2_4

    m_serialHandle = open(ttyPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); // Open perif
    if(m_serialHandle < 0)
    {
        MY_THROW("Impossible d'ouvrir le port '" << ttyPort << "' !\nerrno = " << errno);
    }

    // Conf
    //if(fcntl(m_serialHandle, F_SETFL, 0) == -1) // lecture en mode bloquant
    if(fcntl(m_serialHandle, F_SETFL, O_NONBLOCK) == -1) // lecture en mode non bloquant
    {
        MY_THROW("fcntl failed !\nerrno = " << errno);
    }

    struct termios options;
    tcgetattr(m_serialHandle, &options); // Init struct avec la conf actuelle

    cfsetispeed(&options, B9600); // In speed
    cfsetospeed(&options, B9600); // Output speed

    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode...
    options.c_cflag &= ~PARENB; // Desactive bit de parité
    options.c_cflag &= ~CSTOPB; // Désactive 2 stop bits -> Active 1 stop bits
    options.c_cflag &= ~CSIZE; // Désactive le bit "CSIZE"
    options.c_cflag |= CS8; // Communication sur 8 bits

    options.c_oflag &= ~OPOST; // Raw output is selected by resetting the OPOST option in the c_oflag member:

    // Application de la conf
    if(tcsetattr(m_serialHandle, TCSAFLUSH, &options) == -1) // Vidage buffer & application immédiate
    {
        MY_THROW("tcsetattr failed !\nerrno = " << errno);
    }
}

读取端口数据

std::string UbiDriver::GetAnswer()
{
    const int buffSize = 1024;
    char buffer[buffSize] = {'\0'};
    int count = 0;
    std::string wholeAnswer = "";

    int noDataTime = 0;

    while(noDataTime < 2) // Tant qu'il y a des données à lire
    {
        count = read(m_serialHandle, buffer, buffSize - 1);
        if(count == -1)
        {
            MY_THROW("Impossible de lire sur le port serie. Verifiez la connexion avec l'imprimante !")
        }

        if(count > 0)
        {
            noDataTime = 0;

            buffer[count] = '\0';
            for(int i = 0; i < count; i++)
            {
                buffer[i] &= ~128; // Supression du premier 1 du binaire
            }

            wholeAnswer += std::string(buffer);
            std::cout << count << std::endl;
        }
        else
        {
            noDataTime++;
            usleep(100000);
        }
    }

    cerr << "----------- Answer -----------" << endl;
    cerr << "Size = " << wholeAnswer.size() << endl;
    cerr << wholeAnswer << endl;

    return wholeAnswer;
    return std::string("");
}

注意:此代码是在您的评论下完成的第二个版本。

10
你说它不起作用了。是什么没有起作用?发生了什么事情? - j4x
我不会直接为您的问题提供答案,这可能是特定平台的问题。但或许您可以考虑使用比termios更高级别的库来完成此任务,例如在Python中使用PySerial,或者在C语言中使用高级串口I/O库。 - Halim Qarroum
请查看以下内容,看看是否适用于您: https://dev59.com/8l_Va4cB1Zd3GeqPW8pa - Saurabh Ghorpade
1
为什么用于检查read()返回-1的代码被注释掉了?它应该在那里,并且应该记录errno或更好的是strerror(errno)。此外,您还应该检查tcgetattr、cfsetospeed和cfsetispeed是否返回错误。 - craig65535
man tcsetattr 还指出:“请注意,如果可以成功执行任何请求的更改,则tcsetattr()将返回成功。因此,在进行多个更改时,可能需要在此调用之后跟随另一个调用tcgetattr()以检查是否已成功执行所有更改。” - craig65535
2个回答

1
我打开了gtkterm源代码,最终找到了解决方案:实际上,您需要覆盖terminos结构(是的,通常情况下不应该这样做)。
如果有人发现更好的解决方案,请随时发布。同时,这里是工作代码,带有英文注释:
打开串口的代码如下:
// Doc : http://www.easysw.com/~mike/serial/serial.html#2_4

m_serialHandle = open(ttyPort.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); // Open serial port
if(m_serialHandle < 0)
{
    MY_THROW("Impossible d'ouvrir le port '" << ttyPort << "' !\nerrno = " << errno);
}

// Read mode
//if(fcntl(m_serialHandle, F_SETFL, 0) == -1) // Blocking read
if(fcntl(m_serialHandle, F_SETFL, O_NONBLOCK) == -1) // Non-blocking read
{
    MY_THROW("fcntl failed !\nerrno = " << errno);
}

// Get current terminos configuration
struct termios options;
tcgetattr(m_serialHandle, &options);

// Force termios values (should not be needed, but is)
options.c_cflag = B9600;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_iflag = IGNPAR | IGNBRK;
options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;

// Set data rate
cfsetispeed(&options, B9600); // In speed
cfsetospeed(&options, B9600); // Output speed

// Set communication flags
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode...
options.c_cflag &= ~PARENB; // Desactive bit de parité
options.c_cflag &= ~CSTOPB; // Désactive 2 stop bits -> Active 1 stop bits
options.c_cflag &= ~CSIZE; // Désactive le bit "CSIZE"
options.c_cflag |= CS8; // Communication sur 8 bits

options.c_oflag &= ~OPOST; // Raw output is selected by resetting the OPOST option in the c_oflag member:

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

// Apply
if(tcsetattr(m_serialHandle, TCSANOW, &options) == -1) // Vidage buffer & application immédiate
{
    MY_THROW("tcsetattr failed !\nerrno = " << errno);
}

// Empty buffers
tcflush(m_serialHandle, TCIOFLUSH);

阅读(非阻塞式)
const int buffSize = 1024;
char buffer[buffSize] = {'\0'};
int count = 0;
std::string wholeAnswer = "";

int noDataTime = 0;

while(noDataTime < 3) // while there is data to be read
{
    count = read(m_serialHandle, buffer, buffSize - 1);
    // May fail in NON-BLOCKING mode if there is nothing to be read
    /*if(count == -1)
    {
        MY_THROW("Impossible de lire sur le port serie. Verifiez la connexion avec l'imprimante !")
    }*/

    if(count > 0)
    {
        noDataTime = 0;

        buffer[count] = '\0';
        /*for(int i = 0; i < count; i++)
        {
            buffer[i] &= ~128; // Delete first binary bit. Could be useful for 7 bit communication, if the first bit is set to 1
        }*/

        wholeAnswer += std::string(buffer);
    }
    else
    {
        noDataTime++;
        usleep(100000);
    }
}

if(!wholeAnswer.empty())
{
    cerr << "----------- Answer -----------" << endl;
    cerr << "Size = " << wholeAnswer.size() << endl;
    cerr << wholeAnswer << endl;
}

return wholeAnswer;

0

检查您的代码,我没有看到您设置原始输入模式的地方。

您可能需要添加:

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

也许在这个版本的Ubuntu上改变的是默认输入模式被设置为CANONICAL而不是raw。 您可以查看此文档以获取完整细节:

http://www.easysw.com/~mike/serial/serial.html#2_3_2

还要检查其他设备使用的流控制方式。如果它需要硬件流控制,在打开端口时必须设置它。代码可能在之前的版本中工作,因为默认使用的值与您的程序兼容。您的程序应该设置所有需要的选项:端口速度、奇偶校验、流控制、输入模式等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接