123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "uart.h"
- #include "maindlg.h"
- #include "includes.h"
- Uart::Uart(QObject *parent)
- : QObject{parent}
- {
- m_pMainDlg = static_cast<MainDlg*>(parent);//保存主窗口指针
- init();
- }
- Uart::~Uart(){
- if(m_pSerialPort != NULL){
- delete m_pSerialPort;
- m_pSerialPort = NULL;
- }
- }
- //模块初始化
- void Uart::init(){
- m_pSerialPort = new QSerialPort();
- connect(this, &Uart::SignalSendDataToMainDlg, m_pMainDlg, &MainDlg::slotReceiveDataFromUart);
- }
- //获得本机可用串口列表
- quint8 Uart::GetUartNameList(QList<QString> *pstrUartNameList, quint8 *pbyLen){
- QList<QSerialPortInfo> uartList; //本机串口列表
- quint8 byLen = 0;
- QString strUartName = "";//串口名称
- //获得本机可用串口
- uartList = QSerialPortInfo::availablePorts();
- byLen = uartList.size();
- if(byLen > 0){
- for(quint8 i = 0; i < byLen; i++){
- strUartName = uartList[i].portName();
- pstrUartNameList->append(strUartName);
- }
- }else{
- QMessageBox::information(m_pMainDlg, "提示", "本机暂无串口可用");
- return 0;
- }
- *pbyLen = byLen;
- return 1;
- }
- //打开串口
- //uartName:串口名称
- quint8 Uart::OpenUart(QString strUartName){
- //配置串口参数
- m_pSerialPort->setBaudRate(QSerialPort::Baud115200);
- m_pSerialPort->setDataBits(QSerialPort::Data8);
- m_pSerialPort->setStopBits(QSerialPort::OneStop);
- m_pSerialPort->setParity(QSerialPort::NoParity);
- m_pSerialPort->setPortName(strUartName);
- m_pSerialPort->setReadBufferSize(1024);
- //打开串口
- m_pSerialPort->open(QIODeviceBase::ReadWrite);
- if(m_pSerialPort->isOpen()){//打开成功
- //链接数据接收槽函数
- connect(m_pSerialPort, &QSerialPort::readyRead, this, &Uart::slotReceiveData);
- }else{//打开失败
- QMessageBox::critical(m_pMainDlg, "提示", "打开串口失败");
- return 0;
- }
- return 1;
- }
- //关闭串口
- void Uart::CloseUart(){
- m_pSerialPort->close();
- }
- //接收数据的槽函数
- void Uart::slotReceiveData(){
- QByteArray dataList;
- quint16 wLen = 0;
- dataList = m_pSerialPort->readAll();//读取缓存中全部数据
- wLen = dataList.size();//数据长度
- if(wLen){//收到了数据
- emit SignalSendDataToMainDlg(dataList, wLen);//将数据发送给主控模块
- }
- }
- // 发送数据
- // 返回值: 本次发送的字节数,如果失败则返回 -1
- qint64 Uart::SendData(QByteArray dataList) {
- quint16 wListLen = dataList.size();
- qint64 bytesSent = 0; // 使用 qint64 以支持大文件传输
- bool result = false;
- if (m_pSerialPort->isOpen()) {
- for (quint8 i = 0; i < 3; i++) {
- bytesSent = m_pSerialPort->write(dataList);
- if ((bytesSent == wListLen) && (bytesSent != -1)) {
- result = m_pSerialPort->waitForBytesWritten(2000);
- if (result) {
- break; // 成功写入并等待完成
- } else {
- bytesSent = -1; // 写入成功但等待失败
- }
- } else {
- QThread::sleep(1); // 使用 sleep 替代忙等待
- }
- }
- if(!result){
- QMessageBox::critical(m_pMainDlg, "提示", "发送数据到串口失败");
- bytesSent = -1;
- }
- } else {
- QMessageBox::information(m_pMainDlg, "提示", "请先打开串口");
- bytesSent = -1;
- }
- return bytesSent;
- }
|