C++调用libusb,继电器(usbrelay)开发
2017-09-20 23:44
896 查看
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:
MyDLL.cpp
最后运行成c++的动态库dll或者so文件。
根据网上资料,修改之后:
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 lsusb struct 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文件。
相关文章推荐
- C# 调用继电器api usb_relay_device.dll
- Java调用c++动态链接库(一) JNative 开发环境的搭建
- 【VS开发】C++调用外部程序
- ARM汇编 C语言 C++ 相互调用 分类: 嵌入式开发学习 2014-04-18 21:17 800人阅读 评论(0) 收藏
- 【windows8开发】C++开发WinRT组件和JS调用
- Android-使用C++实现调用本地方法返回字符串显示在界面上/NDK-JNI开发实例(八)
- 【转载整理】eclipse 开发c/c++、Java使用JNI调用C程序、生成64位dll动态链接库
- c++简单的ATL COM开发和调用实例
- android jni开发 把一段java代码转换成c++方式调用
- libusb无驱设计(USB设备开发)
- 用C++开发与调用WebService的例子
- Android(安卓)开发通过NDK调用JNI,使用opencv做本地c++代码开发配置方法 边缘检测 范例代码
- linux下C++开发系列(六)——文件IO相关的系统调用
- 成功实现C/C++调用Java (Android NDK 开发)--参考1
- C++如何调用C#开发的dll
- Android学习开发4--Android在C++中调用java
- Android开发 通过JNI实现JAVA与C/C++程序间的调用和回调
- 调用C++开发的DLL报内存异常错误
- c#调用c++开发的dll const char* 返回值接收问题
- USB设备开发---- usb描述符概述(libusb库使用)