• Octave下操作CH341


     1 #include <octave/oct.h>
     2 #include <windows.h>
     3 #include <cstdint>
     4 #include <fstream>
     5 #include <dMatrix.h>
     6 #include <int32NDArray.h>
     7 
     8 // CH341DLL Functions Prototype
     9 typedef HANDLE WINAPI(*CH341OpenDevice_fn)(unsigned int iIndex);
    10 
    11 typedef void (*CH341CloseDevice_fn)(unsigned int iIndex);
    12 
    13 typedef BOOL WINAPI(*CH341SetStream_fn)(
    14     unsigned int     iIndex,
    15     unsigned int     iMode);
    16 
    17 typedef BOOL WINAPI(*CH341StreamI2C_fn)(
    18     unsigned int    iIndex,
    19     unsigned int    iWriteLength,
    20     void *            iWriteBuffer,
    21     unsigned int    iReadLength,
    22     void *            oReadBuffer);
    23 
    24 // Octave function declaration
    25 DEFUN_DLD(i2c_rd, args, nargout, "USB I2C Read")
    26 {
    27     int32NDArray bytes;
    28     size_t bytes_read;
    29 
    30     if(args.length() < 2) {
    31         octave_stdout << "
      --i2c_rd I2C_ADR REG_ADR [Bytes to Read]
    " << std::endl;
    32         return octave_value_list();
    33     } else if(args.length() == 3) {
    34         bytes = args(2).matrix_value();
    35         bytes_read = bytes(0, 0);
    36     } else {
    37         bytes(0, 0) = 1;
    38         bytes_read = 1;
    39     }
    40     
    41     // Load CH341DLL to Current Process
    42     HMODULE hdll = LoadLibrary("CH341DLL.DLL");
    43 
    44     if(NULL == hdll) {
    45         octave_stdout <<"USB_I2C CH341DLL Load Failed!" << std::endl;
    46         return octave_value_list();
    47     }
    48 
    49     // Get CH341 Functions Handle
    50     CH341OpenDevice_fn  USBIO_OpenDevice    = (CH341OpenDevice_fn)    GetProcAddress(hdll, "CH341OpenDevice");
    51     CH341CloseDevice_fn USBIO_CloseDevice    = (CH341CloseDevice_fn)    GetProcAddress(hdll, "CH341CloseDevice");
    52     CH341SetStream_fn   USBIO_SetStream        = (CH341SetStream_fn)    GetProcAddress(hdll, "CH341SetStream");
    53     CH341StreamI2C_fn   USBIO_StreamI2C        = (CH341StreamI2C_fn)    GetProcAddress(hdll, "CH341StreamI2C");
    54 
    55     int32NDArray i2c_adr = args(0).int32_array_value();
    56     int32NDArray reg_adr = args(1).int32_array_value();
    57 
    58     Matrix result(1, bytes_read);
    59     uint8_t IBuf[bytes_read];
    60     uint8_t OBuf[2] = {uint8_t(i2c_adr(0, 0)), uint8_t(reg_adr(0, 0))};
    61 
    62     // Open CH341 Device
    63     HANDLE devh = USBIO_OpenDevice(0);
    64 
    65     if(devh == NULL) {
    66         octave_stdout << "CH341 Open Failed!" << std::endl;
    67         FreeLibrary(hdll);
    68         return octave_value_list();
    69     }
    70     
    71     if(!USBIO_SetStream(0, 0x82)) {
    72         octave_stdout << "CH341 SetStream Failed!" << std::endl;
    73         USBIO_CloseDevice(0);
    74         FreeLibrary(hdll);
    75         return octave_value_list();
    76     }
    77 
    78     // If number of bytes to read is not given.
    79     if(!USBIO_StreamI2C(0, 2, OBuf, bytes_read, IBuf)) {
    80         octave_stdout << "CH341 Read Failed!" << std::endl;    
    81     } else {
    82         for(size_t i = 0; i < bytes_read; ++i) {
    83             result(0, i) = double(IBuf[i]);
    84         }
    85     }
    86     
    87     USBIO_CloseDevice(0);
    88     FreeLibrary(hdll);
    89     return octave_value(result);
    90 }
  • 相关阅读:
    golang goroutine协程泄露
    golang值传递和引用传递
    golang语法速记
    RabbitMQ管理界面介绍
    golang不定参数
    google Flutter入门
    golang context
    mysql ptarchiver
    8个顶级免费CRM客户关系管理系统
    未注册单拼域名——.top篇
  • 原文地址:https://www.cnblogs.com/lyuyangly/p/8709195.html
Copyright © 2020-2023  润新知