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文件。
阅读全文
0 0
- C++调用libusb,继电器(usbrelay)开发
- libusb开发
- libusb-win32 连续调用 usb_find_busses
- Linux+libusb开发用户USB驱动程序(转)
- Ubuntu15下Qt+libusb开发
- Libusb开发教程<一> 安装
- LIBUSB
- libUSB
- libusb
- libusb
- libusb
- 继电器(一)
- 继电器(二)
- 继电器
- 继电器
- 继电器
- 继电器
- NDK开发java调用c(三)
- 【练习】Mybatis简单配置(NetBeans下)
- 宏定义中的 ## ... __VA_ARGS__
- 862B Mahmoud and Ehab and the bipartiteness (黑白染色)
- Android使用iconfont
- TCP 三次握手和四次分手简析
- C++调用libusb,继电器(usbrelay)开发
- sublime text3 Build3143 破解
- binary string codeforces 862D 交互题,二分答案
- Node.JS——基础知识
- C语言——字符串指针-strcpy
- L1正则和L2正则的比较分析详解
- celery初级教程(五)
- C#学习回顾笔记九:switch选择分支结构
- 对于nose框架中class级别的setUp和tearDown函数的一点理解