myserial.c
#include "myserial.h"
MySerial::MySerial(QString com)
{
mCom = com;
sendAck = 0;
UartRxActive = false;
serialInit();
}
void MySerial::serialInit()
{
connect(this,SIGNAL(readyRead()),this,SLOT(serialRead())); //连接槽
//获取计算机上所有串口并添加到comboBox中
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
if(infos.isEmpty())
{
this->close();
errorText = "未找到串口";
//emit msendError("未找到串口");
return;
}else{
foreach (QSerialPortInfo info, infos) {
if(mCom == info.portName())
{
this->setPortName(mCom);
if(this->open(QIODevice::ReadWrite)) //读写打开
{
this->setBaudRate(QSerialPort::Baud115200); //波特率
this->setDataBits(QSerialPort::Data8); //数据位
this->setParity(QSerialPort::NoParity); //无奇偶校验
this->setStopBits(QSerialPort::OneStop); //无停止位
this->setFlowControl(QSerialPort::NoFlowControl); //无控制
}else{
this->close();
errorText = "无法打开串口";
// emit msendError("无法打开串口");
}
return;
}
}
}
}
bool MySerial::SendWaitAck(QByteArray data, int waitTime)
{
int retryCnt = 0;
errorText.clear();
do{
QCoreApplication::processEvents(QEventLoop::AllEvents,100);
if((retryCnt)== 0){//不允许重发 if((retryCnt % 500)== 0)
sendAck =
QT 串口收发
最新推荐文章于 2024-04-19 11:11:20 发布