//serial_helper.h
#ifndef _SERIAL_HELPER_H_
#define _SERIAL_HELPER_H_
#include <QObject>
#include <QSerialPortInfo>
#include <QSerialPort>
class serial_helper : public QObject
{
Q_OBJECT
public:
serial_helper(QObject *parent);
int ConnectComPort(QSerialPortInfo info);
int SendComMsg(QString sInfo);
int CloseComPort();
~serial_helper();
public slots:
void Slot_ReadComData();
signals:
void signal_read(QString);
private:
QSerialPort m_serialPort;
char m_szBuf[2048];
};
#endif
//serial_helper.cpp
#include "serial_helper.h"
#include <QDebug>
#include <windows.h>
serial_helper::serial_helper(QObject *parent)
: QObject(parent)
{
}
serial_helper::~serial_helper()
{
}
int serial_helper::ConnectComPort(QSerialPortInfo info)
{
m_serialPort.setPort(info);
if (!m_serialPort.open(QIODevice::ReadWrite))
{
return -1;
}
m_serialPort.setBaudRate(QSerialPort::Baud115200);
m_serialPort.setParity(QSerialPort::NoParity);
m_serialPort.setDataBits(QSerialPort::Data8);
m_serialPort.setStopBits(QSerialPort::OneStop);
m_serialPort.setFlowControl(QSerialPort::NoFlowControl);
m_serialPort.setReadBufferSize(1024);
connect(&m_serialPort, SIGNAL(readyRead()), this, SLOT(Slot_ReadComData()));
//m_serialPort.write("\r\n");
return 0;
}
int serial_helper::SendComMsg(QString sInfo)
{
m_serialPort.write(QString("%1\r\n").arg(sInfo).toStdString().c_str());
return 0;
}
int serial_helper::CloseComPort()
{
m_serialPort.close();
return 0;
}
void serial_helper::Slot_ReadComData()
{
Sleep(600);
memset(m_szBuf, 0, 2048);
int len = m_serialPort.read(m_szBuf, 2048);
qDebug() << "read:" << QString(m_szBuf);
qDebug() << "len:" << len;
QString strInfo(m_szBuf);
if (strInfo.length() == 0)
{
return;
}
emit signal_read(strInfo);
}
//mgr_serial.h
#pragma once
#include <QObject>
#include <QStringList>
#include <QVector>
#include <QSerialPortInfo>
#include <QMap>
#include "serial_helper.h"
class mgr_serial : public QObject
{
Q_OBJECT
public:
static mgr_serial* GetInstance();
int GetComList(QStringList &slist);
int FindSerialComIndex();
//连接板子
int ConnectBoardComPort(QString comName);
//连接蓝牙
int ConnectBlueComPort(QString comName);
int SendBoardMsg(QString sInfo);
int SendBlueMsg(QString sInfo);
int Uinit();
~mgr_serial();
signals:
void signal_serial_index(int);
void signal_blue_index(int);
void signal_board_read(QString);
void signal_blue_read(QString);
public slots:
void Slot_CloseSerial();
void Slot_FindComsIndex(QString sInfo);
void Slot_BlueRead(QString sInfo);
void Slot_BoardRead(QString sInfo);
private:
mgr_serial(QObject *parent);
int CloseFindSerialComs();
QStringList m_comList;
QMap<QString, QSerialPortInfo> m_map;
QVector<serial_helper*> m_vec;
serial_helper *m_board_helper;
serial_helper *m_blue_helper;
int m_board_index;
int m_blue_index;
static mgr_serial *m_instance;
};
//mgr_serial.cpp
#include "mgr_serial.h"
#include <QTimer>
#include <QDebug>
mgr_serial::mgr_serial(QObject *parent)
: QObject(parent), m_board_index(-1), m_blue_index(-1), m_board_helper(nullptr), m_blue_helper(nullptr)
{
m_board_helper = new serial_helper(nullptr);
m_blue_helper = new serial_helper(nullptr);
connect(m_board_helper, SIGNAL(signal_read(QString)), this, SLOT(Slot_BoardRead(QString)));
connect(m_blue_helper, SIGNAL(signal_read(QString)), this, SLOT(Slot_BlueRead(QString)));
}
int mgr_serial::Uinit()
{
if (m_board_helper)
{
m_board_helper->CloseComPort();
delete m_board_helper;
m_board_helper = nullptr;
}
if (m_blue_helper)
{
m_blue_helper->CloseComPort();
delete m_blue_helper;
m_blue_helper = nullptr;
}
return 0;
}
mgr_serial *mgr_serial::m_instance = nullptr;
mgr_serial* mgr_serial::GetInstance()
{
if (!m_instance)
{
m_instance = new mgr_serial(nullptr);
}
return m_instance;
}
mgr_serial::~mgr_serial()
{
if (m_board_helper)
{
delete m_board_helper;
m_board_helper = nullptr;
}
if (m_blue_helper)
{
delete m_blue_helper;
m_blue_helper = nullptr;
}
}
int mgr_serial::GetComList(QStringList &slist)
{
m_map.clear();
m_comList.clear();
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
QString retPortName = info.portName();
if (retPortName.length() == 0)
{
continue;
}
m_map[retPortName] = info;
m_comList.append(retPortName);
slist.append(retPortName);
}
return 0;
}
int mgr_serial::ConnectBoardComPort(QString comName)
{
if (m_board_helper)
{
m_board_helper->CloseComPort();
}
QSerialPortInfo sInfo = m_map[comName];
int nRet = m_board_helper->ConnectComPort(sInfo);
//需要回车才能进入终端
m_board_helper->SendComMsg("\r\n");
return nRet;
}
int mgr_serial::ConnectBlueComPort(QString comName)
{
if (m_blue_helper)
{
m_blue_helper->CloseComPort();
}
QSerialPortInfo sInfo = m_map[comName];
int nRet = m_blue_helper->ConnectComPort(sInfo);
return nRet;
}
void mgr_serial::Slot_BoardRead(QString sInfo)
{
emit signal_board_read(sInfo);
}
void mgr_serial::Slot_BlueRead(QString sInfo)
{
emit signal_blue_read(sInfo);
}
int mgr_serial::FindSerialComIndex()
{
m_board_index = -1;
m_blue_index = -1;
//串口只能打开一个,找串口的时候需要关闭
if (m_board_helper)
{
m_board_helper->CloseComPort();
}
if (m_blue_helper)
{
m_blue_helper->CloseComPort();
}
int nRet = 0;
for (int i = 0; i < m_comList.size(); i++)
{
QSerialPortInfo info = m_map[m_comList[i]];
serial_helper *pSerial = new serial_helper(nullptr);
pSerial->setProperty("index", i);
connect(pSerial, SIGNAL(signal_read(QString)), this, SLOT(Slot_FindComsIndex(QString)));
nRet = pSerial->ConnectComPort(info);
pSerial->SendComMsg("fct_stop\r\n");
m_vec.push_back(pSerial);
}
//等待3秒关闭所有的串口
QTimer::singleShot(3000, this, SLOT(Slot_CloseSerial()));
return 0;
}
int mgr_serial::SendBoardMsg(QString sInfo)
{
m_board_helper->SendComMsg(sInfo);
return 0;
}
int mgr_serial::SendBlueMsg(QString sInfo)
{
m_blue_helper->SendComMsg(sInfo);
return 0;
}
int mgr_serial::CloseFindSerialComs()
{
for (int i = 0; i < m_vec.size(); i++)
{
disconnect(m_vec[i], SIGNAL(signal_read(QString)), this, SLOT(Slot_FindComsIndex(QString)));
m_vec[i]->CloseComPort();
delete m_vec[i];
m_vec[i] = nullptr;
}
m_vec.clear();
return 0;
}
void mgr_serial::Slot_CloseSerial()
{
CloseFindSerialComs();
}
void mgr_serial::Slot_FindComsIndex(QString sInfo)
{
int nIndex = this->sender()->property("index").toInt();
QString sReInfo = sInfo.replace("\r\n", "");
if (sReInfo == "fct_stop")
{
m_blue_index = nIndex;
emit signal_blue_index(nIndex);
}
else
{
m_board_index = nIndex;
emit signal_serial_index(nIndex);
}
//
if ((m_board_index != -1) && (m_blue_index != -1))
{
CloseFindSerialComs();
}
}
//调用方式
QStringList sList;
mgr_serial::GetInstance()->GetComList(sList);
ui.cmb_serial->addItems(sList);
ui.cmb_blue->addItems(sList);
//定位哪个index
mgr_serial::GetInstance()->FindSerialComIndex();
注意:readyread()信号触发时需要延时处理下,否则收的数据不全。