qt 子线程串口发送数据异常

class UartQThread : public QThread
{
Q_OBJECT
public:
UartQThread();
void run();
private:
QSerialPort *Uart;
QString UartPort;
uint32_t BaudRate;
uint32_t DataBits;
QString StopBits;
QString Parity;
bool UartReady;
char readbuff[128];
public:
void UartInit();
private slots:
void UartRead(void);
};

UartQThread::UartQThread()
{
UartInit();
}
void UartQThread::run()
{
int result;

while(true)
{
    result = Uart->write("this is uart send test456");
    qDebug("UartQThread 1000ms test:%d %d", result, Uart->bytesAvailable());
    msleep(1000);
}

}
void UartQThread::UartInit()
{
Uart = new QSerialPort();
UartReady = false;
connect(Uart,SIGNAL(readyRead()),this,SLOT(UartRead())); //连接槽
// ...... 省略 串口波特率等设置
if(Uart->open(QIODevice::ReadWrite))
{
qDebug("open uart port success");
}
else
{
qDebug()<<"open uart fail"<<UartPort;
}

 Uart->write("this is uart send test123");

}

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
UartQThread *UartProcessD = new UartQThread();
UartProcessD->start();
}
使用QSerialport,UartQThread 继承QThread ,对端串口收到了 UartInit发送数据;但是run 函数中,循环write 发送数据,对端串口只收到一次, 如果将UartInit放到run 里面初始化,一条也发送不出去,也收不到

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐