1, header file
#ifndef MYSERIAL_H
#define MYSERIAL_H#include"mymethod.h"
#include
class MySerial: public QSerialPort
{
Q_OBJECT
public:
MySerial(QObject *parent);
~MySerial();
private:
QSerialPort* serialPort = NULL;//Serial port object
QByteArray HEAD;//Message header
void init();//initializationprivate slots:
void openSlot(SerialPortInfo serialPortInfo);//< span style="color: #008000;">Connect function
void rcvSlot();//Receive data slot function
void sendSlot(QByteArray ba);//Send data slot function
void closeSlot();//Close slot function
signals:
void serialStateSignal(QString info);//< span style="color: #008000;">Outbound debugging information
void getPacketSignal(int type, QByteArray ba);//tcp sends out different packets
};
#endif // MYSERIAL_H
2, source file
#include "myserial.h"
MySerial::MySerial(QObject *parent)
: QSerialPort(parent)
{
this->init();
}
MySerial::~MySerial()
{
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: initialization
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::init()
{
HEAD.resize(2); HEAD[0 ] = 0x55; HEAD[1] = 0xAA;
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: open serial port
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::openSlot(SerialPortInfo serialPortInfo)
{
if (NULL == serialPort)
{
serialPort = new QSerialPort();
QObject::connect(serialPort, SIGNAL(readyRead()), this, SLOT(rcvSlot()));
}
if (serialPort->isOpen())
{
return;
}
serialPort->setPortName(serialPortInfo.portName);
serialPort->setBaudRate(serialPortInfo.baudRate, QSerialPort::AllDirections);
serialPort->setDataBits(serialPortInfo.dataBits);//data bits
serialPort->setParity(serialPortInfo.parity);//Check Digit
serialPort->setStopBits(serialPortInfo.stopBits);//Stop Bits
serialPort->setFlowControl(QSerialPort::NoFlowControl);//Flow control
if (serialPort->open(QIODevice::ReadWrite))< span style="color: #008000;">//Open the serial port
{
emit serialStateSignal("The serial port is opened successfully... ");
}
else
{
emit serialStateSignal("The serial port failed to open!!! ");
}
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: Receive data
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::rcvSlot()
{
static QByteArray allBa;//Used to save all files
allBa.append(serialPort->readAll());// Read data
int head = allBa.indexOf(HEAD);//The position of the message header
uint16_t length = 0;//Store length bytes
QByteArray currentBa;
int id = 0;
while (-1 != head)
{
allBa = allBa.mid(head);//Before removing the header Of
memcpy(&length, allBa.data() + 2, 2< span style="color: #000000;">);
if (allBa.size() >= length)//< /span>The length is enough for analysis
{
currentBa = allBa.mid(0, length);//< /span>Get the current completion package
id = Mymethod::getPacketType(currentBa);
if (-1 != id)
{
emit getPacketSignal(id, currentBa);
}
allBa = allBa.mid(2);
}
else
{
break;
}
head = allBa.indexOf(HEAD);//Refresh message Head position
}
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//function name: send
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::sendSlot(QByteArray ba)
{
if (NULL == serialPort)
{
emit serialStateSignal("Serial port is not initialized!!!");
return;
}
if (!serialPort->isOpen())
{
emit serialStateSignal("Serial port is not open!!!");
return;
}
if (ba.size() != serialPort->write(ba))
{
emit serialStateSignal("Command transmission failed!!! ");
}
else
{
emit serialStateSignal("Send command successfully...");
}
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: Off
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::closeSlot()
{
if (NULL == serialPort)
{
return;
}
serialPort->close();
emit serialStateSignal("The serial port is closed... ");
}
3. Custom function to get the message type
int Mymethod::getPacketType(const QByteArray& ba)
{
int result = -1;
uint16_t length = 0;
if ((ba[0] == ( char)0x55) && (ba[1] == (char)0xAA))//The message header is correct
{
memcpy(&length, ba.data() + 2, 2< /span>);
if (length == ba.size())//< /span>The length is correct
{
QByteArray tempBa; tempBa.resize(ba.size() - 1 );
memcpy(tempBa.data(), ba.data(), ba.size() - 1);
char crc = Mymethod::CRC(tempBa);
if (crc == ba[ba.size()-1 ])//CRC is correct
{
result = (int)ba[4];
}
}
}
return result;
}
#ifndef MYSERIAL_H
#define MYSERIAL_H#include"mymethod.h"
#include
class MySerial: public QSerialPort
{
Q_OBJECT
public:
MySerial(QObject *parent);
~MySerial();
private:
QSerialPort* serialPort = NULL;//Serial port object
QByteArray HEAD;//Message header
void init();//initializationprivate slots:
void openSlot(SerialPortInfo serialPortInfo);//< span style="color: #008000;">Connect function
void rcvSlot();//Receive data slot function
void sendSlot(QByteArray ba);//Send data slot function
void closeSlot();//Close slot function
signals:
void serialStateSignal(QString info);//< span style="color: #008000;">Outbound debugging information
void getPacketSignal(int type, QByteArray ba);//tcp sends out different packets
};
#endif // MYSERIAL_H
#include "myserial.h"
MySerial::MySerial(QObject *parent)
: QSerialPort(parent)
{
this->init();
}
MySerial::~MySerial()
{
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: initialization
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::init()
{
HEAD.resize(2); HEAD[0 ] = 0x55; HEAD[1] = 0xAA;
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: open serial port
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::openSlot(SerialPortInfo serialPortInfo)
{
if (NULL == serialPort)
{
serialPort = new QSerialPort();
QObject::connect(serialPort, SIGNAL(readyRead()), this, SLOT(rcvSlot()));
}
if (serialPort->isOpen())
{
return;
}
serialPort->setPortName(serialPortInfo.portName);
serialPort->setBaudRate(serialPortInfo.baudRate, QSerialPort::AllDirections);
serialPort->setDataBits(serialPortInfo.dataBits);//data bits
serialPort->setParity(serialPortInfo.parity);//Check Digit
serialPort->setStopBits(serialPortInfo.stopBits);//Stop Bits
serialPort->setFlowControl(QSerialPort::NoFlowControl);//Flow control
if (serialPort->open(QIODevice::ReadWrite))< span style="color: #008000;">//Open the serial port
{
emit serialStateSignal("The serial port is opened successfully... ");
}
else
{
emit serialStateSignal("The serial port failed to open!!! ");
}
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: Receive data
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::rcvSlot()
{
static QByteArray allBa;//Used to save all files
allBa.append(serialPort->readAll());// Read data
int head = allBa.indexOf(HEAD);//The position of the message header
uint16_t length = 0;//Store length bytes
QByteArray currentBa;
int id = 0;
while (-1 != head)
{
allBa = allBa.mid(head);//Before removing the header Of
memcpy(&length, allBa.data() + 2, 2< span style="color: #000000;">);
if (allBa.size() >= length)//< /span>The length is enough for analysis
{
currentBa = allBa.mid(0, length);//< /span>Get the current completion package
id = Mymethod::getPacketType(currentBa);
if (-1 != id)
{
emit getPacketSignal(id, currentBa);
}
allBa = allBa.mid(2);
}
else
{
break;
}
head = allBa.indexOf(HEAD);//Refresh message Head position
}
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//function name: send
//Function parameter: NULL
//Function return value: NULL
//Function role: NULL
//Remarks: NULL
/***************************** ************************************/
void MySerial::sendSlot(QByteArray ba)
{
if (NULL == serialPort)
{
emit serialStateSignal("Serial port is not initialized!!!");
return;
}
if (!serialPort->isOpen())
{
emit serialStateSignal("Serial port is not open!!!");
return;
}
if (ba.size() != serialPort->write(ba))
{
emit serialStateSignal("Command transmission failed!!! ");
}
else
{
emit serialStateSignal("Send command successfully...");
}
}
/************* ************************************************** **/
//Author: Zhu Xiaoyong
//Function name: Off
//函数参数:NULL
//函数返回值:NULL
//函数作用:NULL
//备注:NULL
/*****************************************************************/
void MySerial::closeSlot()
{
if (NULL == serialPort)
{
return;
}
serialPort->close();
emit serialStateSignal("已关闭串口...");
}
int Mymethod::getPacketType(const QByteArray& ba)
{
int result = -1;
uint16_t length = 0;
if ((ba[0] == (char)0x55) && (ba[1] == (char)0xAA))//报文头正确
{
memcpy(&length, ba.data() + 2, 2);
if (length == ba.size())//长度正确
{
QByteArray tempBa; tempBa.resize(ba.size() - 1);
memcpy(tempBa.data(), ba.data(), ba.size() - 1);
char crc = Mymethod::CRC(tempBa);
if (crc == ba[ba.size() - 1])//CRC正确
{
result = (int)ba[4];
}
}
}
return result;
}