我对qt信号槽感到困惑。我使用了以下连接,但它连接到另一个插槽。
void MainWindow::on_otpburndata1Btn_Rd_clicked()
{
if(serialPort->clear(serialPort->AllDirections)){
QByteArray otpburndata1ord;
otpburndata1ord.resize(3);
otpburndata1ord[2]=0x02;
otpburndata1ord[1]=0x08;
otpburndata1ord[0]=0x1D;
serialPort->write(otpburndata1ord.data(),3);
connect(serialPort,SIGNAL(readyRead()),this,SLOT(receiveOtpBurnData1()));
}
}
void MainWindow::receiveOtpBurnData1(){
QByteArray otpburndata1;
otpburndata1.resize(1);
if(serialPort->bytesAvailable()==1){
otpburndata1 = serialPort->readAll();
if(otpburndata1[0]&0x01){
ui->OTP_DATA0->setStyleSheet("background-color:red");
ui->OTP_DATA0->setText("1");
flagThree[0]=1;
}else{
ui->OTP_DATA0->setStyleSheet("background-color:rgb(170, 255, 127)");
ui->OTP_DATA0->setText("0");
flagThree[0]=0;
}
...
...
...
}
}
因为我在此on_otpburndata1Btn_Rd_clicked()函数之前使用了另一个函数。当我单击Button:otpburndata1Btn_Rd时,它从端口读取数据,但是连接到另一个插槽otprddata1Received(),而不是右侧插槽receiveOtpBurnData1()。先前的代码如下:
void MainWindow::on_otprddata1Btn_clicked()
{
if(serialPort->clear(serialPort->AllDirections)){
QByteArray otprdata1ord;
otprdata1ord.resize(3);
otprdata1ord[0]=0x9D;
otprdata1ord[1]=0x00;
otprdata1ord[2]=0x02;
serialPort->write(otprdata1ord.data(),3);
connect(serialPort,SIGNAL(readyRead()),this,SLOT(otprddata1Received()));
}
}
void MainWindow::otprddata1Received(){
QByteArray otprdata1;
otprdata1.resize(1);
if(serialPort->bytesAvailable()==1){
otprdata1 = serialPort->readAll();
if(otprdata1[0]&0x01){
ui->OTP_RD_DATA0->setStyleSheet("background-color:red");
ui->OTP_RD_DATA0->setText("1");
}else{
ui->OTP_RD_DATA0->setStyleSheet("background-color:rgb(170, 255, 127)");
ui->OTP_RD_DATA0->setText("0");
}
....
}
答案 0 :(得分:0)
我已经通过为每个数据添加额外的2字节来解决问题,以区别点击哪个按钮以及显示接收数据的位置。因此,区分每个数据是正确的。例如,如果GUI将从串行端口接收1字节数据,我添加另外2个字节用于区分(当然1byte也可以)。代码如下:
void MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
....
connect(this,SIGNAL(readyReadotprddata1()),this,SLOT(otprddata1Received()));
connect(this,SIGNAL(readyReadotprddata2()),this,SLOT(otprddata2Received()));
connect(this,SIGNAL(readyReadotprddata3()),this,SLOT(otprddata3Received()));
connect(this,SIGNAL(readyReadotpburndata1()),this,SLOT(receiveOtpBurnData1()));
connect(this,SIGNAL(readyReadotpburndata2()),this,SLOT(receiveOtpBurnData2()));
connect(this,SIGNAL(readyReadotpburndata3()),this,SLOT(receiveOtpBurnData3()));
connect(this,SIGNAL(readyReadotpctrlreg1()),this,SLOT(receiveOtpctrlReg1()));
connect(this,SIGNAL(readyReadotpctrlreg2()),this,SLOT(receiveOtpctrlReg2()));
connect(this,SIGNAL(readyReadsiliconid()),this,SLOT(receiveSiliconidDataord()));
}
void MainWindow::on_otprddata1Btn_clicked()
{
if(serialPort->clear(serialPort->AllDirections)){
QByteArray otprdata1ord;
otprdata1ord.resize(3);
otprdata1ord[0]=0x9D;
otprdata1ord[1]=0x00;
otprdata1ord[2]=0x02;
serialPort->write(otprdata1ord.data(),3);
connect(serialPort,SIGNAL(readyRead()),this,SLOT(commonDistinct()));
}
}
....
void MainWindow::commonDistinct(){
if(serialPort->bytesAvailable()==3){
data=serialPort->readAll();
if(data[0].operator ==(0x9D) && data[1].operator ==(0x00)) emit readyReadotprddata1();
else if(data[0].operator ==(0xBD) && data[1].operator ==(0x00)) emit readyReadotprddata2();
else if(data[0].operator ==(0xDD) && data[1].operator ==(0x00)) emit readyReadotprddata3();
else if(data[0].operator ==(0x1D) && data[1].operator ==(0x08)) emit readyReadotpburndata1();
else if(data[0].operator ==(0x1D) && data[1].operator ==(0x04)) emit readyReadotpburndata2();
else if(data[0].operator ==(0x3D) && data[1].operator ==(0x04))emit readyReadotpburndata3();
else if(data[0].operator ==(0x1D) && data[1].operator ==(0x02))emit readyReadotpctrlreg1();
else if(data[0].operator ==(0x1D) && data[1].operator ==(0x01))emit readyReadotpctrlreg2();
else if(data[0].operator ==(0x5D) && data[1].operator ==(0x00))emit readyReadsiliconid();
}
}
void MainWindow::otprddata1Received(){
if(data[2]&0x01){
ui->OTP_RD_DATA0->setStyleSheet("background-color:red");
ui->OTP_RD_DATA0->setText("1");
}else{
ui->OTP_RD_DATA0->setStyleSheet("background-color:rgb(170, 255, 127)");
ui->OTP_RD_DATA0->setText("0");
}
....
}
为什么我使用这个答案是,每当有串口接收到数据时,每次都会发出Qt5信号readyRead()。我所有需要的数据都是1个字节。我不能使用bytesAvailable()函数来区分接收的数据。