Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92525
  • 博文数量: 34
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 275
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-13 23:05
文章分类

全部博文(34)

文章存档

2011年(1)

2010年(7)

2009年(26)

我的朋友

分类: 嵌入式

2010-03-19 23:55:59

    今天无意间试探,居然发现可以在matlab下面调用外部的程序, 使用VC++6.0来运行编译连接工作, 我测试了两个硬件库, winpcap和libusb-win32, 两个都是用来操作硬件底层的开发库。 这不? 全部实现了啊。



    测试libusb, 把头文件和lib库文件放在同一个目录下就OK了

/*
 * testlibusb.c
 *
 * Test suite program
 */


#include "mex.h"
#include <string.h>
#include <stdio.h>
#include "usb.h"
#define printf mexPrintf
#define snprintf _snprintf
void dddmain();

#pragma comment(lib, "libusb.lib")

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    dddmain();
}


int verbose = 0;

void print_endpoint(struct usb_endpoint_descriptor *endpoint)
{
  printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
  printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
  printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
  printf(" bInterval: %d\n", endpoint->bInterval);
  printf(" bRefresh: %d\n", endpoint->bRefresh);
  printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
}

void print_altsetting(struct usb_interface_descriptor *interface)
{
  int i;

  printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
  printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
  printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
  printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
  printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
  printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
  printf(" iInterface: %d\n", interface->iInterface);

  for (i = 0; i < interface->bNumEndpoints; i++)
    print_endpoint(&interface->endpoint[i]);
}

void print_interface(struct usb_interface *interface)
{
  int i;

  for (i = 0; i < interface->num_altsetting; i++)
    print_altsetting(&interface->altsetting[i]);
}

void print_configuration(struct usb_config_descriptor *config)
{
  int i;

  printf(" wTotalLength: %d\n", config->wTotalLength);
  printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
  printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
  printf(" iConfiguration: %d\n", config->iConfiguration);
  printf(" bmAttributes: %02xh\n", config->bmAttributes);
  printf(" MaxPower: %d\n", config->MaxPower);

  for (i = 0; i < config->bNumInterfaces; i++)
    print_interface(&config->interface[i]);
}

int print_device(struct usb_device *dev, int level)
{
  usb_dev_handle *udev;
  char description[256];
  char string[256];
  int ret, i;

  udev = usb_open(dev);
  if (udev) {
    if (dev->descriptor.iManufacturer) {
      ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string,
                                  sizeof(string));
      if (ret > 0)
        snprintf(description, sizeof(description), "%s - ", string);
      else
        snprintf(description, sizeof(description), "%04X - ",
                 dev->descriptor.idVendor);
    } else
      snprintf(description, sizeof(description), "%04X - ",
               dev->descriptor.idVendor);

    if (dev->descriptor.iProduct) {
      ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string,
                                  sizeof(string));
      if (ret > 0)
        snprintf(description + strlen(description), sizeof(description) -
                 strlen(description), "%s", string);
      else
        snprintf(description + strlen(description), sizeof(description) -
                 strlen(description), "%04X", dev->descriptor.idProduct);
    } else
      snprintf(description + strlen(description), sizeof(description) -
               strlen(description), "%04X", dev->descriptor.idProduct);

  } else
    snprintf(description, sizeof(description), "%04X - %04X",
             dev->descriptor.idVendor, dev->descriptor.idProduct);

  printf("%.*sDev #%d: %s\n", level * 2, " ", dev->devnum,
         description);

  if (udev && verbose) {
    if (dev->descriptor.iSerialNumber) {
      ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string,
                                  sizeof(string));
      if (ret > 0)
        printf("%.*s - Serial Number: %s\n", level * 2,
               " ", string);
    }
  }

  if (udev)
    usb_close(udev);

  if (verbose) {
    if (!dev->config) {
      printf(" Couldn't retrieve descriptors\n");
      return 0;
    }

    for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
      print_configuration(&dev->config[i]);
  } else {
    for (i = 0; i < dev->num_children; i++)
      print_device(dev->children[i], level + 1);
  }

  return 0;
}

void dddmain()
{
  struct usb_bus *bus;
    verbose = 1;

  usb_init();
  usb_set_debug(0);

  usb_find_busses();
  usb_find_devices();

  for (bus = usb_get_busses(); bus; bus = bus->next) {
    if (bus->root_dev && !verbose)
      print_device(bus->root_dev, 0);
    else {
      struct usb_device *dev;

      for (dev = bus->devices; dev; dev = dev->next)
        print_device(dev, 0);
    }
  }

  return 0;
}



mexopts.bat 设置, 注意VCINSTALLDIR是在自己的目录下, 可以有空格

set MATLAB=%MATLAB% set MSDevDir=%MSDevDir% set VSINSTALLDIR=D:\software\Microsoft Visual Studio set VCINSTALLDIR=%VSINSTALLDIR%\VC98 set PATH=%VCINSTALLDIR%\BIN;%VSINSTALLDIR%\Common\msdev98\bin;%PATH% set INCLUDE=%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\MFC\INCLUDE;%VCINSTALLDIR%\ATL\INCLUDE;%INCLUDE% set LIB=%VCINSTALLDIR%\LIB;%VCINSTALLDIR%\MFC\LIB;%LIB% set MW_TARGET_ARCH=win32


pcap的文件,

#define WIN32
#define HAVE_REMOTE
#include "pcap.h"
#include "mex.h"

void dddmain();

#pragma comment(lib, "wpcap.lib")

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    dddmain();
}

void dddmain()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];
    
    /* Retrieve the device list from the local machine */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
    {
        mexPrintf("Error in pcap_findalldevs_ex: %s\n", errbuf);
        exit(1);
    }
    
    /* Print the list */
    for(d= alldevs; d != NULL; d= d->next)
    {
        mexPrintf("%d. %s", ++i, d->name);
        if (d->description)
            mexPrintf(" (%s)\n", d->description);
        else
            mexPrintf(" (No description available)\n");
    }
    
    if (i == 0)
    {
        mexPrintf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return;
    }

    /* We don't need any more the device list. Free it */
    pcap_freealldevs(alldevs);
}


经过测试, 全都OK, 哈哈哈哈哈
阅读(2550) | 评论(0) | 转发(0) |
0

上一篇:Hex文件格式分析

下一篇:gas汇编

给主人留下些什么吧!~~