25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

118 satır
3.1 KiB

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QSerialPortInfo>
  4. #include <QDebug>
  5. #include <QSerialPort>
  6. #include <QByteArray>
  7. #include "crc.h"
  8. Widget::Widget(QWidget *parent) :
  9. QWidget(parent),
  10. ui(new Ui::Widget)
  11. {
  12. ui->setupUi(this);
  13. serialPort = new QSerialPort(this);
  14. connect(serialPort,&QSerialPort::readyRead,this,&Widget::on_SerialData_ReadyToRead);
  15. ui->comboBox_baudRate->setCurrentIndex(3);
  16. ui->comboBox_dataBit->setCurrentIndex(3);
  17. ui->comboBox_xiaoyan->setCurrentIndex(2);
  18. QList<QSerialPortInfo> serialList = QSerialPortInfo::availablePorts();
  19. for(QSerialPortInfo serialInfo : serialList)
  20. {
  21. ui->comboBox_serialNum->addItem(serialInfo.portName());
  22. }
  23. }
  24. Widget::~Widget()
  25. {
  26. delete ui;
  27. }
  28. void Widget::on_btnConnect_clicked()
  29. {
  30. if (ui->btnConnect->text() == "连接")
  31. {
  32. //配置串口号
  33. serialPort->setPortName(ui->comboBox_serialNum->currentText());
  34. //配置波特率
  35. serialPort->setBaudRate(ui->comboBox_baudRate->currentText().toInt());
  36. //配置数据位
  37. serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_dataBit->currentText().toInt()));
  38. //配置校验位
  39. switch (ui->comboBox_xiaoyan->currentIndex())
  40. {
  41. case 0:
  42. serialPort->setParity(QSerialPort::NoParity);
  43. break;
  44. case 1:
  45. serialPort->setParity(QSerialPort::OddParity);
  46. break;
  47. case 2:
  48. serialPort->setParity(QSerialPort::EvenParity);
  49. }
  50. //配置停止位
  51. serialPort->setStopBits(QSerialPort::StopBits(ui->comboBox_stopBit->currentText().toInt()));
  52. //打开串口
  53. if (serialPort->open(QIODevice::ReadWrite))
  54. {
  55. qDebug() << "Serial open success";
  56. ui->btnConnect->setText("断开");
  57. }
  58. else
  59. {
  60. qDebug() << "error";
  61. }
  62. }
  63. else
  64. {
  65. serialPort->close();
  66. ui->btnConnect->setText("连接");
  67. }
  68. }
  69. void Widget::on_pushWrite_clicked()
  70. {
  71. const char *sendData = ui->lineEdit->text().toStdString().c_str();
  72. serialPort->write(sendData);
  73. qDebug() << "SenOk" <<sendData;
  74. }
  75. void Widget::on_SerialData_ReadyToRead()
  76. {
  77. QByteArray revMessage = serialPort->readAll();
  78. QString hexData = revMessage.toHex().toUpper();
  79. qDebug() << hexData;
  80. ui->textEdit->append(hexData);
  81. }
  82. void Widget::on_btn_read_clicked()
  83. {
  84. QByteArray SendCommand;
  85. SendCommand.append(ui->comboBox_stationAddress->currentText().toInt()%256);
  86. if (ui->comboBox_gongnengma->currentIndex() == 0)
  87. {
  88. SendCommand.append(0x01);
  89. }
  90. else if(ui->comboBox_gongnengma->currentIndex() == 1)
  91. {
  92. SendCommand.append(0x03);
  93. }
  94. SendCommand.append(ui->lineEdit_stratAddress->text().toInt()/256);
  95. SendCommand.append(ui->lineEdit_stratAddress->text().toInt()%256);
  96. SendCommand.append(ui->lineEdit_length->text().toInt()/256);
  97. SendCommand.append(ui->lineEdit_length->text().toInt()%256);
  98. quint16 temp = calculateCrc(SendCommand);
  99. SendCommand.append(temp%256);
  100. SendCommand.append(temp/256);
  101. qDebug() << SendCommand.toHex();
  102. serialPort->write(SendCommand);
  103. }