1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00
LibQQt/examples/qqtnetworkexample/qqtserialmessage.cpp

116 lines
3.1 KiB
C++
Raw Normal View History

2017-10-17 22:00:45 +08:00
#include "qqtserialmessage.h"
#include "qqtcore.h"
2018-01-15 09:08:57 +08:00
QQTSerialMessage::QQTSerialMessage ( QObject* parent ) :
QQtMessage ( parent )
2017-10-17 22:00:45 +08:00
{
m_Head = _SERIAL_HEAD;
m_Size = m_Cmd = m_Sum = 0;
m_Data.clear();;
m_Tail = _SERIAL_TAIL;
}
2018-01-15 09:08:57 +08:00
const quint16& QQTSerialMessage::head() const { return m_Head; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::setHead ( quint16 head ) { m_Head = head; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
const quint16& QQTSerialMessage::size() const { return m_Size; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::setSize ( quint16 size ) { m_Size = size; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
const quint16& QQTSerialMessage::cmd() const { return m_Cmd; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::setCmd ( quint16 cmd ) { m_Cmd = cmd; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
const QByteArray& QQTSerialMessage::data() const { return m_Data; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::setData ( QByteArray& data ) { m_Data = data; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
const quint16& QQTSerialMessage::sum() const { return m_Sum; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::setSum ( quint16 sum ) { m_Sum = sum; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
const quint16& QQTSerialMessage::tail() const { return m_Tail; }
2017-10-17 22:00:45 +08:00
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::setTail ( quint16 tail ) { m_Tail = tail; }
2017-10-17 22:00:45 +08:00
void QQTSerialMessage::translate()
{
m_Size = m_Data.length() + 0x0A;
QByteArray qbaVerify;
qbaVerify << m_Size << m_Cmd << m_Data;
m_Sum = 0;
2018-01-15 09:08:57 +08:00
2017-10-17 22:00:45 +08:00
/*
*
* quint8 0x0112+0x0088=0x009a
*/
2018-01-15 09:08:57 +08:00
for ( int i = 0; i < qbaVerify.length(); i++ )
m_Sum += quint8 ( qbaVerify.at ( i ) );
2017-10-17 22:00:45 +08:00
}
2018-01-15 09:08:57 +08:00
QDebug operator<< ( QDebug dbg, const QQTSerialMessage& c )
2017-10-17 22:00:45 +08:00
{
#if 1
dbg.nospace() << "{" << hex << c.head() << "|" <<
2018-01-15 09:08:57 +08:00
hex << c.size() << "@@" << dec << c.size() << "|" <<
hex << c.cmd() << "|" <<
c.data().data() << "@" << dec << c.data().size() << "|" <<
hex << c.sum() << "|" <<
hex << c.tail() << "}";
2017-10-17 22:00:45 +08:00
#elif 0
QByteArray a;
2018-01-15 09:08:57 +08:00
c.pack ( a );
2017-10-17 22:00:45 +08:00
dbg.nospace() << a.toHex().toUpper();
#else
QByteArray head;
head << c.head();
QByteArray size;
size << c.size();
QByteArray cmd;
cmd << c.cmd();
QByteArray data;
data << c.data();
QByteArray sum;
sum << c.sum();
QByteArray tail;
tail << c.tail();
dbg.nospace() << "{" << head.toHex().toUpper() << " " <<
2018-01-15 09:08:57 +08:00
size.toHex().toUpper() << "@" <<
dec << c.size() << " " <<
cmd.toHex().toUpper() << " " <<
data.toHex().toUpper() << "@" <<
dec << data.size() << " " <<
sum.toHex().toUpper() << " " <<
tail.toHex().toUpper() << "}";
2017-10-17 22:00:45 +08:00
#endif
return dbg.space();
}
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::parser ( const QByteArray& netData )
2017-10-17 22:00:45 +08:00
{
QByteArray l = netData;
quint16 b0 = 0, b1 = 0, b2 = 0, b4 = 0, b5 = 0;
QByteArray b3;
l >> b0 >> b1 >> b2;
2018-01-15 09:08:57 +08:00
b3.resize ( b1 - 0x0A );
2017-10-17 22:00:45 +08:00
l >> b3 >> b4 >> b5;
2018-01-15 09:08:57 +08:00
setHead ( b0 );
setSize ( b1 );
setCmd ( b2 );
setData ( b3 );
setSum ( b4 );
setTail ( b5 );
2017-10-17 22:00:45 +08:00
}
2018-01-15 09:08:57 +08:00
void QQTSerialMessage::packer ( QByteArray& l ) const
2017-10-17 22:00:45 +08:00
{
l << head();
l << size();
l << cmd();
l << data();
l << sum();
l << tail();
}