环境:
主机:WIN7
开发环境:Qt5 3.1.2
说明:
在tcp上传输xml消息.
协议格式如下:
2字节标识(0xc55c,网络序)+2字节预留 +4字节报文内容长度(网络序) + 4字节命令字(网络序)+报文内容
部分协议:
命令字:
请求值班信息:GET_DUTY_INFO
请求报文:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<request >
-
<identifier>客户端的唯一标识符</identifier>
-
</request>
回复报文:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<dutyinfo>
-
<version>版本号</version>//表示车辆值班记录有无变化(如增减)
-
<vehiclenum>车辆数目</vehiclenum>
-
<vehicle>
-
<dutyofvehicleuid>32位UUID</dutyofvehicleuid>
-
<number>车辆编号, 如1,2,3,4</ number>
-
<platenumber>车牌号</platenumber>
-
<image>图片url</image>
-
<liquidlevel>78</liquidlevel>//0至100
-
</vehicle>
-
<vehicle>
-
<dutyofvehicleuid>32位UUID</dutyofvehicleuid>
-
<number>车辆编号, 如1,2,3,4</ number>
-
<platenumber>车牌号</platenumber>
-
<image>图片url</image>
-
<liquidlevel>78</liquidlevel>//0至100
-
</vehicle>
-
…
-
</dutyinfo>
处理思路:
1.发送:
调用qt中处理xml文件的类将所需要发送的信息保存为xml文件,然后读取文件,将文件转化为字节流,并拼接帧头,然后发送
2.接收
将接收的字节流去掉帧头后保存为xml文件,然后调用qt中处理xml文件的类读取其中的信息
源代码:
保存为xml文件
-
-
QFile file(FILE_GET_DUTY_TX);
-
-
QDomDocument doc;
-
QDomElement root_elem;
-
QDomElement item;
-
QDomText text;
-
-
-
QString header("version=\"1.0\" encoding=\"UTF-8\"");
-
doc.appendChild(doc.createProcessingInstruction("xml",header));
-
-
-
root_elem = doc.createElement("request");
-
doc.appendChild(root_elem);
-
-
-
item = doc.createElement("identifier");
-
text = doc.createTextNode(QString(Local_Id));
-
item.appendChild(text);
-
root_elem.appendChild(item);
-
-
-
file.open(QIODevice::WriteOnly);
-
QTextStream out(&file);
-
out.setCodec("UTF-8");
-
doc.save(out,4,QDomNode::EncodingFromTextStream);
-
file.close();
读取xml文件,得到字节流
-
-
file.open(QIODevice::ReadOnly);
-
QTextStream get(&file);
-
-
Data_Xml_Tx = get.readAll().toLocal8Bit();
-
file.close();
发送函数
-
-
-
-
-
-
-
void Net::slot_net_tx_frame(int cmd,QByteArray frame)
-
{
-
QByteArray head;
-
int i = 0;
-
-
i = 0;
-
-
head[i++] = 0xc5;
-
head[i++] = 0x5c;
-
-
head[i++] = 0;
-
head[i++] = 0;
-
-
head[i++] = frame.size() >> 24;
-
head[i++] = frame.size() >> 16;
-
head[i++] = frame.size() >> 8;
-
head[i++] = frame.size();
-
-
head[i++] = cmd >> 24;
-
head[i++] = cmd >> 16;
-
head[i++] = cmd >> 8;
-
head[i++] = cmd;
-
-
frame.prepend(head);
-
-
-
if (tcp_client->state() == QAbstractSocket::ConnectedState)
-
{
-
-
-
tcp_client->write(frame);
-
-
#ifdef DEBUG
-
qDebug() << "发送网络帧1:cmd" << cmd;
-
#endif
-
}
-
else
-
{
-
-
Frame = frame;
-
-
if (tcp_client->state() != QAbstractSocket::ConnectingState)
-
{
-
tcp_client->connectToHost(Server_Ip,Server_Port);
-
}
-
}
-
}
tcp接收网络帧,并保存为xml文件,然后读取xml文件中的信息
-
-
-
-
-
void Get_Duty::deal_frame()
-
{
-
int sum = 0;
-
int i = 0;
-
QSqlQuery q;
-
QString version;
-
-
-
Lock_Net = 0;
-
-
Net_Permission = 0;
-
-
qDebug() << "接收帧:回复值班信息";
-
-
-
if ((uint8_t)Frame.at(11) == 0)
-
{
-
-
-
QFile file(FILE_GET_DUTY_RX);
-
-
file.open(QIODevice::WriteOnly);
-
QTextStream out(&file);
-
out.setCodec("UTF-8");
-
out << Frame.mid(LEN_FRAME_HEAD);
-
file.close();
-
-
-
QDomDocument doc(FILE_GET_DUTY_RX);
-
-
file.open(QIODevice::ReadOnly);
-
doc.setContent(&file);
-
file.close();
-
-
-
QDomElement root_node = doc.documentElement();
-
-
QDomNode node = root_node.firstChild();
-
version = node.toElement().text();
-
-
if (version == Version)
-
{
-
return;
-
}
-
-
-
Version = version;
-
qDebug() << "版本号" << Version;
-
-
-
node = node.nextSibling();
-
sum = node.toElement().text().toInt();
-
qDebug() << "车辆总数" << sum;
-
-
-
Car_Three_List[0].clear();
-
Car_Three_List[1].clear();
-
Car_Three_List[2].clear();
-
-
q.prepare("DELETE FROM duty");
-
q.exec();
-
-
for (i = 0;i < sum;i++)
-
{
-
node = node.nextSibling();
-
-
-
q.prepare("INSERT INTO duty VALUES(?,?,?,?,?,?)");
-
-
q.bindValue(0,node.toElement().childNodes().at(0).toElement().text());
-
-
q.bindValue(1,node.toElement().childNodes().at(1).toElement().text().toInt());
-
-
q.bindValue(2,node.toElement().childNodes().at(2).toElement().text());
-
-
q.bindValue(3,node.toElement().childNodes().at(3).toElement().text());
-
-
q.bindValue(4,node.toElement().childNodes().at(4).toElement().text().toInt());
-
-
q.bindValue(5,IMG_NULL);
-
q.exec();
-
-
-
if (i < 3)
-
{
-
Car_Three_List[i] = node.toElement().childNodes().at(0).toElement().text();
-
}
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
}
-
else
-
{
-
Version = "null";
-
-
-
q.prepare("DELETE FROM duty");
-
q.exec();
-
}
-
-
-
emit sig_recv_duty_info();
-
-
-
Frame.clear();
-
Len_Frame_Content = 0;
-
}
-
阅读(2903) | 评论(0) | 转发(1) |