我需要在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 发送一个字符。
QextSerialPort很久以前就没有更新了。我使用QSerialPort。
Serial.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";
};
使用此代码的最终工作:
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();