网络读卡器qt代码示例,包含获取本地ip地址。读取IC卡卡号,读取扇区数据

电子说

1.3w人已加入

描述

获取本机所有的网卡IP:

 

bool listening=false;
QTcpServer m_server;
QTcpSocket m_socket;
QStringList getAllIPAddresses() {
   QStringList ips;
   QList< QHostAddress > list = QNetworkInterface::allAddresses();
   for (int i = 0; i < list.size(); ++i) {
       QHostAddress address = list.at(i);
       // 检查是否为IPv4地址,并排除回环地址
       if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress::LocalHost) {
           ips < < address.toString();
       }
   }
   return ips;
}
根据IP地址获取Mac
QString getMacByIp(const QString &ip) {
   QHostInfo info = QHostInfo::fromName(ip);
   foreach (const QHostAddress &address, info.addresses()) {
       if (address.protocol() == QAbstractSocket::IPv4Protocol) {
           foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
               if (interface.flags().testFlag(QNetworkInterface::IsUp) && interface.flags().testFlag(QNetworkInterface::IsRunning)) {
                   foreach (const QNetworkAddressEntry &entry, interface.addressEntries()) {
                       if (entry.ip() == address) {
                           return interface.hardwareAddress();
                       }
                   }
               }
           }
       }
   }
   return QString();
}
//解析读取IC卡卡号-------------------------------------------------------------------------------------------------------------------
void MainWindow::Analyze_c1(QHostAddress ip,quint16 port,unsigned char databuff[], qint64 buflen)
{
   DisableSendAgain(ip, port,databuff,buflen);//确认接收到的信息,否则同一信息读卡器会重发三次
   QString IPAddress=QString::asprintf("%d", databuff[1])+"."+QString::asprintf("%d", databuff[2])+"."+QString::asprintf("%d", databuff[3])+"."+QString::asprintf("%d", databuff[4]); //设备IP地址,广域网上使用,一般不对此IP回应,而是对数据流的 ip及端口 回应
   QString DeviceNumber=QString::asprintf("%05d",databuff[5]+databuff[6]*256);   //设备自编机号
   QString FrameNumber=QString::asprintf("%05d",databuff[7]+databuff[8]*256);    //数据包号
   QString CardnoHexZ=QString::asprintf("%02X%02X%02X%02X", databuff[10],databuff[11],databuff[12],databuff[13]);  //16进制卡号正码
   QString CardnoHexF=QString::asprintf("%02X%02X%02X%02X", databuff[13],databuff[12],databuff[11],databuff[10]);  //16进制卡号反码
   bool status;
   QString Cardno8H10D=QString::asprintf("%010u",CardnoHexF.toUInt(&status,16));  //转8H10D反码卡号
   QString SerialNumber="";        //设备全球唯一硬件序号
   for (int i=14;itextEdit_M1UID- >setText(CardnoHexZ);
   if(ui- >checkBox_Resp- >isChecked()){Respondinfo(getsysdatetime()+"卡号:"+Cardno8H10D,ip,port,DeviceNumber);}  //回应驱动设备显示+蜂鸣响声
}
主动读取扇区数据的返回-
void MainWindow::Analyze_c3(QHostAddress ip,quint16 port,unsigned char databuff[], qint64 buflen)
{
   QString IPAddress=QString::asprintf("%d", databuff[1])+"."+QString::asprintf("%d", databuff[2])+"."+QString::asprintf("%d", databuff[3])+"."+QString::asprintf("%d", databuff[4]); //设备IP地址,广域网上使用,一般不对此IP回应,而是对数据流的 ip及端口 回应
   QString DeviceNumber=QString::asprintf("%05d",databuff[5]+databuff[6]*256);   //设备自编机号
   QString CardnoHex="";
   for (int i=12;i< 16;i++){
       CardnoHex=CardnoHex+QString::asprintf("%02X", databuff[i]);
   }
   int j=databuff[10]*48;
   int m=databuff[11]*48;
   for(int i=0;i< m;i++){
       readcardbuf[j+1]=databuff[16+i];
   }
   if((databuff[10]+databuff[11]) >=databuff[9]){
       m=databuff[9]*48;
       QString DataHex="";
       for (int i=0;i< m;i++){
           DataHex=DataHex+QString::asprintf("%02X ",readcardbuf[i]);
       }
       ui- >textEdit_data- >setPlainText(DataHex);
       QString DispInf="读取M1卡扇区数据成功,设备IP:"+IPAddress+",机号:"+DeviceNumber+",16进制卡号:"+CardnoHex;
       listadditems(1,DispInf);
       ui- >textEdit_M1UID- >setText(CardnoHex);
       if(ui- >checkBox_Resp- >isChecked()){Respondinfo(getsysdatetime()+"卡号:"+CardnoHex,ip,port,DeviceNumber);}  //回应驱动设备显示+蜂鸣响声
   }
}
读卡器

 

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分