1

我需要与 RS232 端口(串行)上的指纹对话。我有一个像魅力一样工作的python代码:

import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 9600, parity='N', timeout=2)
print ser
ser.open()

ser.write(chr(0x00)) # channel 1 byte (alway the same)
ser.write(chr(0x05)) # command 1 byte

ser.write(chr(0x00)) # param1 2 bytes (byte low)
ser.write(chr(0x00)) # param1 2 bytes (byte hight)

ser.write(chr(0x00)) # param2 2 bytes (byte low)
ser.write(chr(0x00)) # param2 2 bytes (byte hight)

ser.write(chr(0x00)) # lwExtraData 2 bytes (byte low)
ser.write(chr(0x00)) # lwExtraData 2 bytes (byte hight)

ser.write(chr(0x00)) # hwExtraData 2 bytes (byte low)
ser.write(chr(0x00)) # hwExtraData 2 bytes (byte hight)

ser.write(chr(0x00)) # ErrorCode 1 byte

sum = 0x05 % 256

ser.write(chr(sum)) # Checksum (modulo 256) 1 byte

print "\nRespuesta Secugen :\n"

cadena = ser.read(12)

for i in range(12):
    print(hex(ord(cadena[i]))),


ser.close()

但我需要将此代码转换为 C++/Qt。我不明白我需要使用什么功能。

我试过这个:

serial = new QextSerialPort(serialPort);

    serial->setBaudRate(BAUD9600);
    serial->setFlowControl(FLOW_OFF);
    serial->setParity(PAR_NONE);
    serial->setDataBits(DATA_8);
    serial->setStopBits(STOP_1);
    serial->setTimeout(2);

res = serial->open(QIODevice::ReadWrite);

    if(res)
    {

        QByteArray zero = QByteArray::fromHex("\x00");

       serial->write(zero);

        [...]

      qDebug() << "byte a lire : " << serial->bytesAvailable();
      QByteArray test = serial->readAll();
    }

如果可能,我想使用 QextSerialPort 扩展。我试过但没有成功。我想我不明白我需要做什么转换。

- 编辑 -

我的问题并不是关于管理串口的类,抱歉我的问题不是很清楚。我不知道重现与 python 相同的代码的解决方案是什么:在 RS232 中将字符发送到 hexa。

4

2 回答 2

1

最后使用此代码:

QSerialPort serial;
serial.setPortName(serialPort);

qDebug() << "port : " + serialPort;

if (!serial.open(QIODevice::ReadWrite)) {
    qDebug() << "error open : ";
}

serial.setParity(QSerialPort::NoParity);
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setFlowControl(QSerialPort::NoFlowControl);

QByteArray text2 = QByteArray::fromHex("5");
QByteArray text1 = QByteArray::fromHex("0");

serial.write(text1.constData(),1);
serial.write(text2.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text1.constData(),1);
serial.write(text2.constData(),1);

if (serial.waitForBytesWritten(1000)) {

    if(serial.waitForReadyRead(1000))
    {
        QByteArray requestData = serial.readAll();
        qDebug() << "byte readed : " <<  requestData.size();

        qDebug() << QString::number(requestData[0], 16);
        qDebug() << QString::number(requestData[1], 16);
        qDebug() << QString::number(requestData[2], 16);
        qDebug() << QString::number(requestData[3], 16);
        qDebug() << QString::number(requestData[4], 16);
        qDebug() << QString::number(requestData[5], 16);
    }
}

serial.close();
于 2013-10-21T04:16:38.587 回答
1

QextSerialPort 很久以前没有更新。我使用QSerialPort

串行.h

class Serial : public QSerialPort{
void openPort(const QString &, const int &, const DataBits &, const  Parity &, const StopBits &, const FlowControl &);

}

串口.cpp

void Serial::openPort(const QString &serialPort, const int &baudRate, const DataBits &dataBits, const Parity &parity, const StopBits &stopBits, const FlowControl &flowControl) {

            this->serialPort = serialPort;
            this->baudRate = baudRate;
            this->dataBits = dataBits;
            this->parity = parity;
            this->stopBits = stopBits;
            this->flowControl = flowControl;

            this->setPortName(this->serialPort);

            if (this->open(QIODevice::ReadWrite)) {
            } else {
                qDebug() << "\n Can't open port | " << this->errorString();
            }

             if (
                    this->setBaudRate(this->baudRate)
                    && this->setDataBits(this->dataBits)
                    && this->setParity(this->parity)
                    && this->setStopBits(this->stopBits)
                    && this->setFlowControl(this->flowControl)) {
                qDebug() << "\n[ info ]        Port settings successfully";
            } else {
                qDebug() << "\n[ error ]        Port settings failed";
            }
}

您可以在其他类中使用 Serial 类:

Serial *serialPort = new Serial();

 if (serialPort->write(data)) {
                qDebug() << "\n[ info ]        data: " << data.toHex() << "         | wrote";
            } else {
                qDebug() << "\n[ error ]        error write data";
            };
于 2013-10-20T00:12:38.280 回答