| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #include "rtuport.h"
- RtuPort::RtuPort()
- {
- m_serialPort = new QSerialPort(this);
- stard = false;
- //设置参数
- // m_serialPort->setPortName(GlobalInfo::this_()->config().portName);
- // m_serialPort->setBaudRate(GlobalInfo::this_()->config().portBaudRate.toInt());//设置默认波特率和读写方向
- // m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
- // m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
- // m_serialPort->setParity(QSerialPort::NoParity); //无校验位
- // m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
- connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(receiveInfo()));
- // ret = QByteArray::fromHex("010300000006C5C8");
- // portTimer = new QTimer(this);
- // connect(portTimer,&QTimer::timeout,this,&RtuPort::portStart);
- // portTimer->start(1000);
- timer = new QTimer(this);
- connect(timer,&QTimer::timeout,this,&RtuPort::portWrite);
- }
- void RtuPort::start(QString & name, int band, SerialBaseHandle * handle)
- {
- if(m_serialPort->isOpen()) m_serialPort->close();
- m_serialPort->setPortName(name);
- m_serialPort->setBaudRate(band);
- m_serialPort->open(QIODevice::ReadWrite);
- if(!timer->isActive())
- timer->start(200);
- stard = true;
- }
- void RtuPort::portWrite()
- {
- if(!m_serialPort->isOpen()){
- m_serialPort->open(QIODevice::ReadWrite);
- return;
- }
- if(_handle && _handle->needSend()){
- m_serialPort->write(_handle->sendData());
- }
- }
- void RtuPort::receiveInfo()
- {
- QByteArray info = m_serialPort->readAll();
- if(_handle)
- _handle->handle(info);
- }
|