|
|
@@ -1,56 +1,55 @@
|
|
|
-#include "rtuport.h"
|
|
|
+#include "rtuport.h"
|
|
|
|
|
|
RtuPort::RtuPort()
|
|
|
{
|
|
|
- m_serialPort = new QSerialPort();
|
|
|
+ m_serialPort = new QSerialPort(this);
|
|
|
+ stard = false;
|
|
|
//设置参数
|
|
|
|
|
|
- m_serialPort->setPortName(GlobalInfo::this_()->config().portName);
|
|
|
- m_serialPort->setBaudRate(GlobalInfo::this_()->config().portBaudRate.toInt(),QSerialPort::AllDirections);//设置默认波特率和读写方向
|
|
|
- m_serialPort->setDataBits(QSerialPort::Data8); //数据位为8位
|
|
|
- m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//无流控制
|
|
|
- m_serialPort->setParity(QSerialPort::NoParity); //无校验位
|
|
|
- m_serialPort->setStopBits(QSerialPort::OneStop); //一位停止位
|
|
|
+// 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);
|
|
|
+// 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::portStart()
|
|
|
+void RtuPort::start(QString & name, int band, SerialBaseHandle * handle)
|
|
|
{
|
|
|
- m_serialPort->open(QIODevice::ReadWrite);
|
|
|
- if(!m_serialPort->isOpen()){
|
|
|
- qDebug()<<"串口未打开";
|
|
|
- return;
|
|
|
- }
|
|
|
- qDebug("串口已开启");
|
|
|
- portTimer->stop();
|
|
|
- timer = new QTimer(this);
|
|
|
- connect(timer,&QTimer::timeout,this,&RtuPort::portWrite);
|
|
|
- timer->start(200);
|
|
|
+ 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()
|
|
|
{
|
|
|
-// qDebug()<<"测试";
|
|
|
-// if(hexData.isEmpty()){
|
|
|
-// qDebug()<<"null";
|
|
|
-// timer->stop();
|
|
|
-// portTimer->start(1000);
|
|
|
-// }
|
|
|
- m_serialPort->write(ret);
|
|
|
+ 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();
|
|
|
- QByteArray hexData = info.toHex().data();
|
|
|
-// qDebug()<<"receiveInfo测试:"<<hexData.toUpper();
|
|
|
- log.readed(info);
|
|
|
-// QByteArray data;
|
|
|
+ if(_handle)
|
|
|
+ _handle->handle(info);
|
|
|
|
|
|
}
|
|
|
|