C++调用libusb,继电器(usbrelay)开发

来源:互联网 发布:淘宝联盟省钱购买 编辑:程序博客网 时间:2024/06/05 06:26

usbrelay只提供了windows下的代码,最后项目要在linux下发布,所以只有重新写个驱动。
根据网上资料,修改之后:
1、先下载libusb1.0,我用的是libusb-1.0.20;
2、打开libusb-1.0.20\MS64\static,把libusb-1.0.lib文件加入源文件;
3、打开libusb-1.0.20\include\libusb-1.0,把libusb.h文件加入头文件;

这里写图片描述

MyDLL.h:

 #define JNADLL extern "C" __declspec(dllexport)JNADLL int add(int x, int y);JNADLL int lock();JNADLL int unlock();

MyDLL.cpp

// MyDLL.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"  using namespace std; #include "MyDLL.h" #include "libusb.h"  // libusb head file#include <stdio.h>#include <sys/types.h>#include <string.h>#define VID 0x16c0      // get of lsusb#define PID 0x05df      // get of lsusbstruct libusb_device_handle *devh = NULL;int add(int x, int y)    {        return x + y;    }int lock(){      /* usb init before libusb_open* */    int ret = libusb_init(NULL);    if (ret < 0) {        perror("libusb_init");        return ret;    }    /* end */    /* open device with vid and pid, must after libusb_init */    devh = libusb_open_device_with_vid_pid(NULL, VID, PID);    if (devh==NULL)         perror("Cannot open device\n");         printf("Device Opened\n");    /* end */    /* claim interface */    ret = libusb_claim_interface(devh, 0);    if (ret < 0) {        perror("libusb_claim_interface");        devh = NULL;        libusb_close(devh);        return ret;    }    /* end */    /* open device data */    unsigned char open_data[]={0x53, 0x32,0x4f, 0x44  ,0x32, 0x00, 0x00, 0x00};    //unsigned char open_data[8];    //memset(open_data, 0, sizeof(open_data));    libusb_control_transfer(devh, 0xa1, 0x01, 0x0300, 0x00, open_data, 0x08, 1000);    if ( 0 > libusb_control_transfer(devh, 0xa1, 0x01, 0x0300, 0x00, open_data, 0x08, 1000)) {        perror("libusb_control_transfer");    }    printf("receive data: %s\n", open_data);    int i = 0;    for(i = 0; i < 8; i++) {        printf("%02x\t", open_data[i]);    }    putchar(10);    /* end */    /* lock relay */    unsigned char lock_data[] = {0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00};    if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x0300, 0x00, lock_data, 0x08, 1000)) {        perror("libusb_control_transfer");    }    /* end */    /* delay */    Sleep(2);    /* release claim interface */   libusb_release_interface(devh, 0);    /* end */    /* close device */    libusb_close(devh);    Sleep (10);    return 1;}int unlock(){      /* usb init before libusb_open* */    int ret = libusb_init(NULL);    if (ret < 0) {        perror("libusb_init");        return ret;    }    /* end */    /* open device with vid and pid, must after libusb_init */    devh = libusb_open_device_with_vid_pid(NULL, VID, PID);    if (devh==NULL)         perror("Cannot open device\n");         printf("Device Opened\n");    /* end */    /* claim interface */    ret = libusb_claim_interface(devh, 0);    if (ret < 0) {        perror("libusb_claim_interface");        devh = NULL;        libusb_close(devh);        return ret;    }    /* end */    /* open device data */    unsigned char open_data[]={0x53, 0x32,0x4f, 0x44  ,0x32, 0x00, 0x00, 0x00};    //unsigned char open_data[8];    //memset(open_data, 0, sizeof(open_data));    libusb_control_transfer(devh, 0xa1, 0x01, 0x0300, 0x00, open_data, 0x08, 1000);    if ( 0 > libusb_control_transfer(devh, 0xa1, 0x01, 0x0300, 0x00, open_data, 0x08, 1000)) {        perror("libusb_control_transfer");    }    printf("receive data: %s\n", open_data);    int i = 0;    for(i = 0; i < 8; i++) {        printf("%02x\t", open_data[i]);    }    putchar(10);    /* end */    /* lock relay     unsigned char lock_data[] = {0xff,0x01,0x00,0x00,0x00,0x00,0x00,0x00};    if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x0300, 0x00, lock_data, 0x08, 1000)) {        perror("libusb_control_transfer");    }*/    /* end */    /* delay */    //Sleep(3000);    /* unlock relay */    unsigned char unlock_data[] = {0xfd, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};    if (0 > libusb_control_transfer(devh, 0x21, 0x09, 0x0300, 0x00, unlock_data, 0x08, 1000)) {        perror("libusb_control_transfer");    }    /* end */    /* release claim interface */   libusb_release_interface(devh, 0);    /* end */    /* close device */    libusb_close(devh);    Sleep (10);    return 1;}

最后运行成c++的动态库dll或者so文件。