QSerialPort无法打开,错误代码为11

时间:2019-01-18 04:14:50

标签: c++ c qt serial-port qtserialport

我正在尝试与提供串行端口的设备通信。当我使用设备生产商提供的示例C程序时,它已成功连接,并且可以发送/接收数据。但是,当我将我的C ++程序与QSerialPort一起使用时,open()方法返回false,而error()返回11(如{ {3}})。谁能帮我吗?

  • 操作系统:Cent OS 6.9
  • gcc / g ++:4.8
  • Qt:5.6.0

QSerialPort连接的示例:

#include <qserialport.h>
#include <qstring.h>

int main(int argc, const char *argv[])
{
    QString portName = "/dev/corser/x64ExpCL4x1_s0";
    QSerialPort *pSerial = new QSerialPort();
    pSerial->setPortName(portName);
    pSerial->setBaudRate(QSerialPort::Baud9600);
    pSerial->setParity(QSerialPort::NoParity);
    pSerial->setDataBits(QSerialPort::Data8);
    pSerial->setStopBits(QSerialPort::OneStop);
    pSerial->setFlowControl(QSerialPort::NoFlowControl);
    bool success = pSerial->open(QIODevice::ReadWrite);
    if (!success) {
        printf("Error: %d\n", pSerial->error());
    } else {
        printf("Success.\n");
        pSerial->close();
    }
    delete pSerial;
    return 0;
}

此程序将打印出Error: 11


这是一个C程序,摘自该设备的示例程序。该程序只是尝试连接串行端口,然后关闭连接。虽然原始示例程序很复杂。它实际上是一种“终端”程序,它将用户输入的命令发送到设备,并打印出设备的响应。

#include <unistd.h>
#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>

#include <sys/ioctl.h>
#include <ctype.h>          /* Character types */


#define TRUE 1
#define FALSE 0

typedef struct 
{
    int     open;           // Flag for current open status of port 
    int     update;         // Flag indicating parameter changes    
    char    *port;          // Current port to be opened            
    int     iod;            // I/O index for open device port       
    struct  termios stty;   // Terminal port control structure      
} SCOM_CTL;

struct termios ttctl = {0};
struct termios ttsav = {0};
struct termios coctl = {0};
struct termios cnctl = {0};

#define OPEN            1
#define CLOSE           2
#define UPDATE          3
#define NO_UPDATE       4

SCOM_CTL scom;

int main( int argc, char **argv )
{
    scom.port = "/dev/corser/x64ExpCL4x1_s0";
    scom_init();
    scom_open_port();

    if (scom.open == OPEN) 
    { 
        fputs("\rSuccess.\r\n", stdout);
        scom_close_port(scom.iod);
    }
    else
    {
        fputs("\rError.\r\n", stdout);
    }
}

// Initialize the console (stdin) for raw access.
int scom_init()
{
    int i;
    scom.open = CLOSE;
    scom.update = NO_UPDATE;
    scom.iod  = -1;
    tcgetattr(0, &coctl);   // Save a copy to restore stdin.
    tcgetattr(0, &cnctl);
    cfmakeraw( &cnctl);     // Set stdin to raw !
    tcsetattr(0, 0, &cnctl);    
}

int scom_open_port()
{
    int i, iod;

    if (scom.open == OPEN) 
    {
        close(scom.iod);
        scom.open = CLOSE;
    }

    if ( (iod = open( scom.port, (O_RDWR | O_NOCTTY))) == -1) 
    {
        conres();       /* Reset the console. */
        fputs("term : Open Failure on device\n", stdout);
        return -1;  
    }

    scom.iod = iod;
    scom.open = OPEN;
    scom.update = NO_UPDATE;
}

int scom_close_port()
{
    scom.open = CLOSE;
    close(scom.iod);
}

int conres()
{
    return(tcsetattr(0, 0, &coctl));
}

此程序将打印出Success.

0 个答案:

没有答案