c++ 复杂版找串口的pid和vid

//
// Created by Administrator on 2020/1/17.
//

#ifndef USBLIST_SERIALSCAN_H
#define USBLIST_SERIALSCAN_H

#include <iostream>
#include <map>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <list>
#include <string>
#include <fstream>
#include <android/log.h>

#define  LOG_TAG "C_TAG"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)

using namespace std;

struct serialScanInfo {
    int pid;
    int vid;
    int sid;
    string port;
};

class SerialScan {
public:
    SerialScan();

    string getPort();

private:
    void split(const string& src, const string& separator, list<string>& dest);

    void getSerialScan();

    list<struct serialScanInfo> scanInfo;
    list<struct serialScanInfo>::iterator scanInfoIt;
};


#endif //USBLIST_SERIALSCAN_H
//
// Created by Administrator on 2020/1/17.
//

#include "serialscan.h"

SerialScan::SerialScan() {
    getSerialScan();
}

void SerialScan::split(const string& src, const string& separator, list<string>& dest) {
    string str = src;
    string substring;
    string::size_type start = 0, index;
    dest.clear();
    index = str.find_first_of(separator, start);
    do {
        if (index != string::npos) {
            substring = str.substr(start, index - start);
            dest.push_back(substring);
            start = index + separator.size();
            index = str.find(separator, start);
            if (start == string::npos) break;
        }
    } while (index != string::npos);

    //the last part
    substring = str.substr(start);
    dest.push_back(substring);
}

void SerialScan::getSerialScan() {
    scanInfo.clear();
    FILE *fp = NULL;
    char buf[1024] = {0};
    fp = popen("ls -l /sys/bus/usb-serial/devices", "r");
    if(fp == NULL)
        LOGD("open /sys/bus/usb-serial/devices error");
    fread(buf, 1, sizeof(buf), fp);
    LOGD("read /sys/bus/usb-serial/devices = %s", buf);
    fclose(fp);

    list<string> scanList;
    split(buf,"\n",scanList);
//    total 0
//    lrwxrwxrwx 1 root root 0 2020-01-17 06:07 ttyUSB0 -> ../../../devices/platform/fe380000.usb/usb3/3-1/3-1.4/3-1.4:1.0/ttyUSB0

    list<string>::iterator scanListIt;
    list<string> endList;
    for(scanListIt=scanList.begin();scanListIt!=scanList.end();scanListIt++){
        list<string> tempS;
        split((*scanListIt), " ", tempS);

        if(tempS.size() > 2) {
            string strS = tempS.back();
            endList.push_back(strS); // ../../../devices/platform/fe380000.usb/usb3/3-1/3-1.4/3-1.4:1.0/ttyUSB0
        }
    }

    list<string>::iterator endListIt;
    for(endListIt=endList.begin();endListIt!=endList.end();endListIt++) {
        string strE = (*endListIt);
        strE = strE.substr(0, strE.size() - 7); // ../../../devices/platform/fe380000.usb/usb3/3-1/3-1.4/3-1.4:1.0
        list<string> tempE;
        split((*endListIt), "/", tempE);
        string port = tempE.back(); // ttyUSB0

        string path = " /sys/bus/usb-serial/devices/";
        path += strE;
        path += "uevent";
        LOGD("PATH = %s", path.c_str());

        string cmd = "cat ";
        cmd += path;
        LOGD("Dir = %s", cmd.c_str());

        FILE *fd = NULL;
        bzero(buf, 1024);
        fd = popen(cmd.c_str(), "r");
        if(fp == NULL)
            LOGD("open %s error", cmd.c_str());
        fread(buf, 1, sizeof(buf), fd);
        LOGD("read  = %s", buf);
        fclose(fp);

        list<string> ueventList;
        split(buf, "\n", ueventList);
        int id[3] = {0};
        list<string>::iterator ueventListIt;
        for (ueventListIt = ueventList.begin(); ueventListIt != ueventList.end(); ueventListIt++) {
            string strU = *ueventListIt;
            string tempU = *ueventListIt;

            strU = strU.substr(0, 7);
            tempU = tempU.substr(8, tempU.size());
            if(strU.compare("PRODUCT") == 0) {
                LOGD("strU = %s", strU.c_str());
                LOGD("tempU = %s", tempU.c_str());
                list<string> tempL;
                split(tempU, "/", tempL);
                list<string>::iterator tempLI;
                int i=0;
                for(tempLI = tempL.begin();tempLI != tempL.end();tempLI++){
                    string string1 = "0x";
                    string1 += *tempLI;
                    char *endptr;
                    id[i] = strtol(string1.c_str(), &endptr, 0);
                    LOGD("%d = %d", i, id[i]);
                    i++;
                }
                struct serialScanInfo info;
                info.pid = id[0];
                info.vid = id[1];
                info.sid = id[2];
                info.port = port;

                scanInfo.push_back(info);
                break;
            }
        }
    }
}

string SerialScan::getPort() {
    //1a86/7523
    string port;
    for(scanInfoIt=scanInfo.begin();scanInfoIt!=scanInfo.end();scanInfoIt++) {
        if(((*scanInfoIt).pid == 0x1a86) && (*scanInfoIt).vid == 0x7523) {
            port = (*scanInfoIt).port;
            break;
        }
    }
    return port;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值