如何在变量中发送AT命令

时间:2019-04-29 19:54:43

标签: qt serial-port raspberry-pi3

我正在尝试使用RaspBerry Pi 3和ELM327读取ECU。使用QTCreator来实现。

问题来了,当我想设置一个变量以通过调用void将多个AT命令发送到ELM时,我知道这是将QString强制转换为QByteArray的强制性操作,因此serial.write()将起作用,否则它将提示“没有匹配的函数可以调用QSerialPort :: write(QString&)”。我的问题是我该怎么做。

我已经尝试过使用之前线程中发现的这种方法将命令转换为QByteArray:

QByteArray outd_c=outdata.toUft8();
QByteArray outd_c=outdata.toLatin1();

还尝试了:

void Screenapp::ELMWrite(const QByteArray &outd_c)
{
    outd_c=outdata.toLocal8Bits();
    ELM->flush();
    if(ELM->isOpen() && ELM->isWritable())
    {          
        ELM->write(outd_c);
        if(ELM->waitForBytesWritten(500))
        {
            ui->Terminal->addItem("Sent:");
            ui->Terminal->addItem(outdat);
            ELMRead();
        }
        else
        {
            ui->Terminal->addItem("Sent: Time Out");
        }
    }
    else
    {
        ui->label->setText("ELM closed or non writable");
    }
}

我得到的只是“发送:超时”和/或“响应:读取失败”。

我的代码:

QSerialPort *ELM;
QString indat, outdat;
QByteArray outd_c=outdata.toUtf8(),

void Screenapp::on_ComButton_clicked()
{
    ELM = new QSerialPort(this);
    ELM->setPortName("/dev/ttyS0");
    ELM->open(QIODevice::ReadWrite);

    if(ELM->isOpen()==true){
        ELM->setBaudRate(QSerialPort::Baud38400);
        ELM->setStopBits(QSerialPort::OneStop);
        ELM->setParity(QSerialPort::NoParity);
        ELM->setDataBits(QSerialPort::Data8);
        ELM->setFlowControl(QSerialPort::NoFlowControl);
        ui->statusBar->showMessage("Connected... Baud: 38400");
        ELM->flush();
        ui->label->setText("Serial port: cleaned");
    }else{
        ui->statusBar->showMessage("Not Connected");
        ui->label->setText("Check COM");
        ELM->close();
        QMessageBox::information(this,"ELM 327 Project","Is not possible to connect with ELM");
    }
}

void Screenapp::ELMWrite(const QByteArray &outd_c)
{
    ELM->flush();
    if(ELM->isOpen() && ELM->isWritable())
    {          
        ELM->write(outd_c);
        if(ELM->waitForBytesWritten(500))
        {
            ui->Terminal->addItem("Sent:");
            ui->Terminal->addItem(outdat);
            ELMRead();
        }
        else
        {
            ui->Terminal->addItem("Sent: Time Out");
        }
    }
    else
    {
        ui->label->setText("ELM closed or non writable");
    }
}

void Screenapp::ELMRead()
{
    if(ELM->isOpen() /*&& ELM->isReadable()*/)
    {
        if(ELM->waitForReadyRead(500))
        {
            indat = ELM->read(26);
            while (ELM->waitForReadyRead(500))
                indat +=ELM->read(26);
            indat = indat.simplified();
            ui->Terminal->addItem("Response:");
            ui->Terminal->addItem(indat);
        }
        else
        {
            ui->Terminal->addItem("Response: Read fail");
        }
    }
    else
    {
        ui->Terminal->addItem("ELM is not readable");
    }
}

void Screenapp::on_ATButton_clicked()
{
    ui->Terminal->clear();
    outdat="ATZ/r";
    ELMWrite();
    outdat="ATE0/r";
    ELMWrite();
    outdat="ATRV/r";
    ELMWrite();
    outdat="ATCSM0/r";
    ELMWrite();
    outdat="ATJE/r";
    ELMWrite();
    outdat="ATCS/r";
    ELMWrite();
    outdat="ATSPA/r";
    ELMWrite();
    outdat="ATPA4201/r";
    ELMWrite();
    outdat="ATDP/r";
    ELMWrite();
    outdat="ATDM1/r";
    ELMWrite();
}

对于第一个命令,我希望输出:

ATZ

ELM327 V2.2

对于其他命令,我希望输入“ OK”,但实际输出是“ Sent:Time out”。

感谢您的阅读,对不起我的英语不好,也欢迎您进行语言更正。

0 个答案:

没有答案