#include <QUdpSocket>
// UDP 接受的数据
char buf[1024] = { 0 };
// 接受到数据的标识
bool bRecvFlag = false;
//Udp服务器
QUdpSocket* mSocket;
//通信的ip和端口,用于获取发送者的 IP 和端口
QHostAddress sendAddr; // 本机
QHostAddress recAddr; // 设备ip
quint16 recPort;
quint16 sendPort;
bool XXXCard::init() {
mSocket = new QUdpSocket(this);
// 发送指令的机器所在ip和端口
QSettings ini_config("ICCardReader.ini", QSettings::IniFormat);
ini_config.setIniCodec(QTextCodec::codecForName("utf-8"));
ini_config.beginGroup("XXX");
QString sendIP = ini_config.value("sendIP").toString();
// 监听error()信号 不要监听这个,mSocket->waitForReadyRead会走到监听的槽函数里
// connect(mSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));
//sendAddr.setAddress("192.168.1.2");
sendAddr.setAddress(sendIP.isEmpty()?"192.168.1.2": sendIP);
QString sendPortStr = ini_config.value("sendPort").toString();
sendPortStr = sendPortStr.isEmpty() ? "8080" : sendPortStr;
sendPort = sendPortStr.toInt();
bool bRet = mSocket->bind(sendPort);
// 接受指令的设备所在ip和端口 会返回响应报文
QString cardIP = ini_config.value("cardIP").toString();
recAddr.setAddress(cardIP.isEmpty() ? "192.168.1.232": cardIP);
QString cardPortStr = ini_config.value("cardPort").toString();
cardPortStr = cardPortStr.isEmpty() ? "8080" : cardPortStr;
recPort = cardPortStr.toInt();
// recPort = 8080;
// connect(mSocket, &QUdpSocket::readyRead, this, &HYB502Card::recvData);
return bRet;
}
// 发送二进制指令 0成功 其他返回错误
int XXXX::send_hex(const std::string& in, std::string& out) {
outPutMsg(QtDebugMsg, "XXXX::send_hex invoke begin.req = " + QString(QString::fromLocal8Bit(in.c_str())));
ss = "";
response = "";
QString qstr = QString::fromStdString(in);
QByteArray baReq = qstr.toLocal8Bit();
mSocket->writeDatagram(baReq, recAddr, recPort);
bool flag = true;
int i = 30;
while (flag & i > 0)
{
i--;
Sleep(50);
if (!mSocket->waitForReadyRead(10)) {
continue;
}
quint16 port = 0;
qint64 size = mSocket->readDatagram(buf, sizeof(buf), &sendAddr, &sendPort);
if (size < 1) {
continue;
}
out = buf;
flag = false;
}
QString res = QString::fromLocal8Bit(out.c_str());
outPutMsg(QtDebugMsg, "APDU res = " + res);
// pcsc_receiveBufferLen -= 2;//接受数据长度减去sw
int l_res = -1;
if (out.length() >= 4) {
ss = out.substr(out.length() - 4);
response = out.substr(0, out.length() - 4);
l_res = 0;
}
outPutMsg(QtDebugMsg, "XXXX::send_hex invoke end.");
return l_res;
}
这里很多让用信号槽的方式接受返回的信息 .connect(mSocket, &QUdpSocket::readyRead, this, &HYB502Card::recvData); 实际使用中发现不好使(我查的是说发送一直占用焦点什么的,导致不会捕获到信号啥的),所以使用readDatagram直接从缓存里获取。
connect(mSocket, &QUdpSocket::readyRead, this, &HYB502Card::recvData);方式感觉比较适合单纯的接收或者发,不适合一边发一边收;
校验IP是否合法:
QRegExp regexp(“((2[0-5]\d|25[0-5]|[01]?\d\d?)\.{3}(2[0-4]\d|25[0-5]|[01]?\d\d?)”);
regexp.exactMatch(IPAddress);
可能我理解的不准确,欢迎指正!