uart.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "uart.h"
  2. #include "maindlg.h"
  3. #include "includes.h"
  4. Uart::Uart(QObject *parent)
  5. : QObject{parent}
  6. {
  7. m_pMainDlg = static_cast<MainDlg*>(parent);//保存主窗口指针
  8. init();
  9. }
  10. Uart::~Uart(){
  11. if(m_pSerialPort != NULL){
  12. delete m_pSerialPort;
  13. m_pSerialPort = NULL;
  14. }
  15. }
  16. //模块初始化
  17. void Uart::init(){
  18. m_pSerialPort = new QSerialPort();
  19. connect(this, &Uart::SignalSendDataToMainDlg, m_pMainDlg, &MainDlg::slotReceiveDataFromUart);
  20. }
  21. //获得本机可用串口列表
  22. quint8 Uart::GetUartNameList(QList<QString> *pstrUartNameList, quint8 *pbyLen){
  23. QList<QSerialPortInfo> uartList; //本机串口列表
  24. quint8 byLen = 0;
  25. QString strUartName = "";//串口名称
  26. //获得本机可用串口
  27. uartList = QSerialPortInfo::availablePorts();
  28. byLen = uartList.size();
  29. if(byLen > 0){
  30. for(quint8 i = 0; i < byLen; i++){
  31. strUartName = uartList[i].portName();
  32. pstrUartNameList->append(strUartName);
  33. }
  34. }else{
  35. QMessageBox::information(m_pMainDlg, "提示", "本机暂无串口可用");
  36. return 0;
  37. }
  38. *pbyLen = byLen;
  39. return 1;
  40. }
  41. //打开串口
  42. //uartName:串口名称
  43. quint8 Uart::OpenUart(QString strUartName){
  44. //配置串口参数
  45. m_pSerialPort->setBaudRate(QSerialPort::Baud115200);
  46. m_pSerialPort->setDataBits(QSerialPort::Data8);
  47. m_pSerialPort->setStopBits(QSerialPort::OneStop);
  48. m_pSerialPort->setParity(QSerialPort::NoParity);
  49. m_pSerialPort->setPortName(strUartName);
  50. m_pSerialPort->setReadBufferSize(1024);
  51. //打开串口
  52. m_pSerialPort->open(QIODeviceBase::ReadWrite);
  53. if(m_pSerialPort->isOpen()){//打开成功
  54. //链接数据接收槽函数
  55. connect(m_pSerialPort, &QSerialPort::readyRead, this, &Uart::slotReceiveData);
  56. }else{//打开失败
  57. QMessageBox::critical(m_pMainDlg, "提示", "打开串口失败");
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. //关闭串口
  63. void Uart::CloseUart(){
  64. m_pSerialPort->close();
  65. }
  66. //接收数据的槽函数
  67. void Uart::slotReceiveData(){
  68. QByteArray dataList;
  69. quint16 wLen = 0;
  70. dataList = m_pSerialPort->readAll();//读取缓存中全部数据
  71. wLen = dataList.size();//数据长度
  72. if(wLen){//收到了数据
  73. emit SignalSendDataToMainDlg(dataList, wLen);//将数据发送给主控模块
  74. }
  75. }
  76. // 发送数据
  77. // 返回值: 本次发送的字节数,如果失败则返回 -1
  78. qint64 Uart::SendData(QByteArray dataList) {
  79. quint16 wListLen = dataList.size();
  80. qint64 bytesSent = 0; // 使用 qint64 以支持大文件传输
  81. bool result = false;
  82. if (m_pSerialPort->isOpen()) {
  83. for (quint8 i = 0; i < 3; i++) {
  84. bytesSent = m_pSerialPort->write(dataList);
  85. if ((bytesSent == wListLen) && (bytesSent != -1)) {
  86. result = m_pSerialPort->waitForBytesWritten(2000);
  87. if (result) {
  88. break; // 成功写入并等待完成
  89. } else {
  90. bytesSent = -1; // 写入成功但等待失败
  91. }
  92. } else {
  93. QThread::sleep(1); // 使用 sleep 替代忙等待
  94. }
  95. }
  96. if(!result){
  97. QMessageBox::critical(m_pMainDlg, "提示", "发送数据到串口失败");
  98. bytesSent = -1;
  99. }
  100. } else {
  101. QMessageBox::information(m_pMainDlg, "提示", "请先打开串口");
  102. bytesSent = -1;
  103. }
  104. return bytesSent;
  105. }