ch372USB通信
By
admin
at 2020-09-11 • 0人收藏 • 1206人看过
import console; var dll = raw.loadDll("/CH37xWR.dll",,"cdecl"); usbInit = dll.api("usbInit","bool(INT ulIndex)" ); usbClose = dll.api("usbClose","bool(void)" ); usbWrite = dll.api("usbWrite","INT(INT unEndPoint,pointer pSendBytes,INT unSendLength)" ); usbRead = dll.api("usbRead","INT(INT unEndPoint,pointer pRecvBytes,INT unSendLength)" ); console.log(usbInit(0)) //var data = raw.buffer({byte a[4]={0xb0;0x4f;0x01;0x01}}); var data2 = raw.buffer({byte a[3]={0xD0;0x2f;0x00}}); console.log(usbWrite(2,data2,3)); sleep(1000) var ret = raw.buffer(100,'\0'); console.log(usbRead(2,ret,64)) console.dump(raw.tostring(ret)) var data = raw.buffer({byte a[4]={0xb0;0x4f;0x01;0x01}}); console.log(usbWrite(2,data,4)); sleep(1000) var ret = raw.buffer(100,'\0'); console.log(usbRead(2,ret,64)) console.dump(string.hex(raw.tostring(ret),"")) console.log(usbClose()) console.pause(true);
本来用网上别人二次封装的, 发现各种有问题, 算了自己封装了一个.
VS2015封装dll的工程代码如下:
#include "stdafx.h" #include "doDll.h" #include "CH375DLL.h" #pragma comment(lib,"CH375DLL") //隐式调用库文件 bool OPenSuccess = false; unsigned int CH375_Index = 0; DODLL_API bool usbInit(int index=0) { CH375_Index = index; if (OPenSuccess == false) { OPenSuccess = (CH375OpenDevice(CH375_Index) != INVALID_HANDLE_VALUE); } return OPenSuccess; } DODLL_API bool usbClose(void) { if (OPenSuccess == true) { CH375CloseDevice(CH375_Index); OPenSuccess = false; } return true; } DODLL_API long usbWrite(INT unEndPoint, unsigned char* pSendBytes, ULONG unSendLength) { ULONG len = unSendLength; if (OPenSuccess == true) { BOOL ret = CH375WriteData(CH375_Index, pSendBytes, &len); if (ret == TRUE) { return len; } else { return -1; } } return -1; } DODLL_API long usbRead(INT unEndPoint, unsigned char* pRecBytes, ULONG unSendLength) { ULONG len = unSendLength; if (OPenSuccess == true) { BOOL ret = CH375ReadData(CH375_Index, pRecBytes, &len); if (ret == TRUE) { return len; } else { return -1; } } return -1; }
登录后方可回帖