QT 串口收发

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 =
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值