rtuport.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "rtuport.h"
  2. RtuPort::RtuPort()
  3. {
  4. m_serialPort = new QSerialPort(this);
  5. stard = false;
  6. //设置参数
  7. // m_serialPort->setPortName(GlobalInfo::this_()->config().portName);
  8. // m_serialPort->setBaudRate(GlobalInfo::this_()->config().portBaudRate.toInt());//设置默认波特率和读写方向
  9. // m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
  10. // m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
  11. // m_serialPort->setParity(QSerialPort::NoParity); //无校验位
  12. // m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
  13. connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
  14. // ret = QByteArray::fromHex("010300000006C5C8");
  15. // portTimer = new QTimer(this);
  16. // connect(portTimer,&QTimer::timeout,this,&RtuPort::portStart);
  17. // portTimer->start(1000);
  18. timer = new QTimer(this);
  19. connect(timer,&QTimer::timeout,this,&RtuPort::portWrite);
  20. }
  21. void RtuPort::start(QString & name, int band, SerialBaseHandle * handle)
  22. {
  23. if(m_serialPort->isOpen()) m_serialPort->close();
  24. m_serialPort->setPortName(name);
  25. m_serialPort->setBaudRate(band);
  26. m_serialPort->open(QIODevice::ReadWrite);
  27. if(!timer->isActive())
  28. timer->start(200);
  29. stard = true;
  30. }
  31. void RtuPort::portWrite()
  32. {
  33. if(!m_serialPort->isOpen()){
  34. m_serialPort->open(QIODevice::ReadWrite);
  35. return;
  36. }
  37. if(_handle && _handle->needSend()){
  38. m_serialPort->write(_handle->sendData());
  39. }
  40. }
  41. void RtuPort::receiveInfo()
  42. {
  43. QByteArray info = m_serialPort->readAll();
  44. if(_handle)
  45. _handle->handle(info);
  46. }