USB-IO MATLAB. Gerox(c) 5/30/2003 USB-IO HID USB-IO HID USB IO. usbg.dll STEP.1 DOS. main STEP.2. STEP.3 main.

Similar documents
How to get Real Time Data into Matlab

Wir schaffen Wissen heute für morgen

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

DYNAMIC ENGINEERING 150 DuBois, Suite C, Santa Cruz, CA Fax Est

Title: Migrating to Delcom USB HID Chip Set (Generation 2) from the Delcom USB custom driver Chip Set (Generation 1).

Implementation of Parma Polyhedron Library -functions in MATLAB

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

Face Recognition. Programming Project. Haofu Liao, BSEE. Department of Electrical and Computer Engineering. Northeastern University.

& WizChan. Driver Documentation

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

DYNAMIC ENGINEERING 150 DuBois, Suite 3 Santa Cruz, CA (831) Fax (831) Est

DYNAMIC ENGINEERING 150 DuBois St., Suite C Santa Cruz, CA (831) Fax (831) Est.

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

3D Visualization for Matlab

DYNAMIC ENGINEERING 150 DuBois, Suite 3 Santa Cruz, CA (831) Fax (831) Est.

A System for Interfacing MATLAB with External Software Geared Toward Automatic Differentiation

DYNAMIC ENGINEERING 435 Park Dr., Ben Lomond, Calif Fax Est

DYNAMIC ENGINEERING 150 DuBois St Suite 3, Santa Cruz CA Fax Est.

DYNAMIC ENGINEERING 150 DuBois, Suite 3 Santa Cruz, CA (831) Fax (831) Est

USING LAPACK SOLVERS FOR STRUCTURED MATRICES WITHIN MATLAB

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Decision Making and Loops

Purpose: How to train an MLP neural network in MATLAB environment!

Master Thesis Accelerating Image Registration on GPUs

Introduction to Matlab/Octave

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Prepared by: Shraddha Modi

Term Project report for EE5302

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Lecture 02 C FUNDAMENTALS

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

Latest R Systems Placement Test Questions

SA30228 / CVE

CSE 220: Systems Programming

AMCAT Automata Coding Sample Questions And Answers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Do not start the test until instructed to do so!

CYSE 411/AIT681 Secure Software Engineering Topic #10. Secure Coding: Integer Security

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

2/9/18. Readings. CYSE 411/AIT681 Secure Software Engineering. Introductory Example. Secure Coding. Vulnerability. Introductory Example.

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Static Analysis in Practice

Memory Management. CSC215 Lecture

Problem Solving and 'C' Programming

Lecture 2: C Programming Basic

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

DAY 3. CS3600, Northeastern University. Alan Mislove

Static Analysis in Practice

20 Dynamic allocation of memory: malloc and calloc

Constant-time programming in C

Fastbin_dup into stack exploitation

CSC 1600 Memory Layout for Unix Processes"

Assembler Programming. Lecture 10

CSCE : Computer Systems Homework #1 Part 1 (25 pts) Due date: 1/24/19

AN1814 APPLICATION NOTE

PRINCIPLES OF OPERATING SYSTEMS

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Soliton SCTS Extender

C Programming Review CSC 4320/6320

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

NWEN 241 Systems Programming Exercises (Set 1)

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Intermediate Programming, Spring 2017*

EL2310 Scientific Programming

Memory management. Johan Montelius KTH

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

CSE 410: Systems Programming

A Crash Course in C. Steven Reeves

Introduction Presentation A

CS16 Exam #1 7/17/ Minutes 100 Points total

C++ Programming Applied to Robotics, Mark Aull Lesson 2: Intro to Visual Studio, debugger Intro to C++, variables, conditionals, loops, strings

8. Characters and Arrays

ECE 2400 Computer Systems Programming, Fall 2017 Prelim 1 Prep

Computers Programming Course 7. Iulian Năstac

Solution for Data Structure

Dynamic Memory Allocation

USING THE SYSTEM-C LIBRARY FOR BIT TRUE SIMULATIONS IN MATLAB

POINTER AND ARRAY SUNU WIBIRAMA

Unit 3 Decision making, Looping and Arrays

ECEN 449 Microprocessor System Design. Review of C Programming

14. Memory API. Operating System: Three Easy Pieces

Chapter 6. Data Structure. /* start contains the address of the first node */ start = &elephant1; print_elephants( start ); return EXIT_SUCCESS;

1 Dynamic memory allocation and arrays

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS 222: Pointers and Manual Memory Management

A brief introduction to C programming for Java programmers

Types, Variables, and Constants

Fundamental of Programming (C)

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

Intermediate Programming, Spring 2017*

Understanding Pointers

Transcription:

USB-IO for MATLAB HID USB-IO MATLAB USB-IO HID USB IO usbg.dll MATLAB MEX STEP.1 DOS C main STEP.2 STEP.3 main mexfunction USB-IO USB VisualC++ 6.0 Win2000DDK setupapi.lib,hid.lib PATH C: Program Files Microsoft Visual Studio Common Tools WinNT; C: Program Files Microsoft Visual Studio Common MSDev98 Bin; C: Program Files Microsoft Visual Studio Common Tools; C: Program Files Microsoft Visual Studio VC98 bin LIB C: Program Files Microsoft Visual Studio VC98 mfc lib; C: Program Files Microsoft Visual Studio VC98 lib; c: ntddk lib; c: ntddk libchk i386 INCLUDE C: Program Files Microsoft Visual Studio VC98 atl include; C: Program Files Microsoft Visual Studio VC98 mfc include; C: Program Files Microsoft Visual Studio VC98 include; c: ntddk inc DOS Visual C++ usbtest1.c cl usbtest1.c HID GUID pragma usbtest1.c printf("%x %x %x n", (unsigned long)hidguid.data1,(unsigned long)hidguid.data2, (unsigned long)hidguid.data3); C: >cl usbtest1.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. usbtest1.c Microsoft (R) Incremental Linker Version 6.00.8447 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. /out:usbtest1.exe usbtest1.obj C: >usbtest1 4d1e55b2 f16f 11cf

GUID hidguid.data4[8] MAC GUID GUID usbtest2.c int i,j; int count; HDEVINFO dis; BOOL r; DWORD size; SP_DEVICE_INTERFACE_DATA sdid; PSP_DEVICE_INTERFACE_DETAIL_DATA_A pdidd = NULL; size_t didd_size = 0; HANDLE dh,fd; HIDD_ATTRIBUTES attr; size_t len; char buffer[255]; dis = SetupDiGetClassDevsA(&hidGuid, NULL, 0, DIGCF_PRESENT DIGCF_DEVICEINTERFACE ); count = 0; for (i = 0;;i++){ memset(&sdid,0,sizeof(sdid)); sdid.cbsize = sizeof( SP_DEVICE_INTERFACE_DATA ); r = SetupDiEnumDeviceInterfaces(dis,NULL,&hidGuid,i,&sdid); if (!r) break; size = 0; r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,NULL,0,&size,NULL); if ( r GetLastError()!= ERROR_INSUFFICIENT_BUFFER ) continue; if ( size > didd_size ) { didd_size = ( size + 15 ) & ~15; pdidd = malloc(didd_size); memset(pdidd,0,didd_size); pdidd->cbsize = sizeof(sp_device_interface_detail_data_a); r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,pdidd,size,NULL,NULL); if (!r ) continue; dh = CreateFileA( ( LPCSTR )pdidd->devicepath,generic_read GENERIC_WRITE,FILE_SHARE_READ if (dh == INVALID_HANDLE_VALUE ) continue; memset(&attr,0,sizeof(hidd_attributes)); attr.size = sizeof(hidd_attributes); HidD_GetAttributes(dh,&attr); CloseHandle(dh); printf("%s n",(lpcstr)pdidd->devicepath); printf("%x %x %x",attr.vendorid,attr.productid,attr.versionnumber); count++; SetupDiDestroyDeviceInfoList( dis ); USB-IO ID HID USB malloc free C: >cl usbtest2.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. usbtest2.c Microsoft (R) Incremental Linker Version 6.00.8447 Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:usbtest2.exe usbtest2.obj C: >usbtest 4d1e55b2 f16f 11cf? hid#vid_0bfe&pid_1003#6&99bfe71&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030 bfe 1003 1 C: >? hid#vid_0bfe&pid_1003#6&99bfe71&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030 VenderID 0xbfe,ProductID 0x1003,VersionNumber HID SetupDiDestroyDeviceInfoList( dis ); HID VenderID ProductID CreateFile,ReadFile,WriteFile USB-IO 1 Read,Write usbtest3.c int i,j; int count; HDEVINFO dis; BOOL r; DWORD size; SP_DEVICE_INTERFACE_DATA sdid; PSP_DEVICE_INTERFACE_DETAIL_DATA_A pdidd = NULL; size_t didd_size = 0; HANDLE dh,fd; HIDD_ATTRIBUTES attr; size_t len; char buffer[255]; printf("%x %x %x n",(unsigned long)hidguid.data1,(unsigned long)hidguid.data2,(unsigned long)hidguid.data3); dis = SetupDiGetClassDevsA(&hidGuid, NULL, 0, DIGCF_PRESENT DIGCF_DEVICEINTERFACE ); count = 0; for (i = 0;;i++){ memset(&sdid,0,sizeof(sdid)); sdid.cbsize = sizeof( SP_DEVICE_INTERFACE_DATA ); r = SetupDiEnumDeviceInterfaces(dis,NULL,&hidGuid,i,&sdid); if (!r) break; size = 0; r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,NULL,0,&size,NULL); if ( r GetLastError()!= ERROR_INSUFFICIENT_BUFFER ) continue; if ( size > didd_size ) { didd_size = ( size + 15 ) & ~15; pdidd = malloc(didd_size); memset(pdidd,0,didd_size); pdidd->cbsize = sizeof(sp_device_interface_detail_data_a); r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,pdidd,size,NULL,NULL); if (!r ) continue; dh = CreateFileA( ( LPCSTR )pdidd->devicepath,generic_read GENERIC_WRITE,FILE_SHARE_READ if (dh == INVALID_HANDLE_VALUE ) continue; memset(&attr,0,sizeof(hidd_attributes));

attr.size = sizeof(hidd_attributes); HidD_GetAttributes(dh,&attr); CloseHandle(dh); printf("%s n",(lpcstr)pdidd->devicepath); printf("%x %x %x",attr.vendorid,attr.productid,attr.versionnumber); count++; SetupDiDestroyDeviceInfoList( dis ); //////////////////////////// fd = CreateFile((LPCSTR)pdidd->DevicePath,GENERIC_READ GENERIC_WRITE,FILE_SHARE_READ printf("[%x]",fd); getch(); ///////////////////////////////////// printf(" n"); for (j=0;j <255;j++) { buffer[0]=0; buffer[1]=0x01; buffer[2]=j; buffer[3]=0; buffer[4]=0; buffer[5]=0; buffer[6]=0; buffer[7]=0; buffer[8]=0; count = 9; r=writefile((handle)fd,buffer,9,&count,0); for (i = 0;i < 8;i++) printf("%x ",buffer[i]); printf(" n"); r=readfile((handle)fd,buffer,9,&count,0); for (i = 0;i < 8;i++) printf("%x ",buffer[i]); printf(" n"); CloseHandle( ( HANDLE )fd ); free(pdidd); buffer[1] buffer[1]=01 buffer[1]=02 buffer[1]=03 buffer[1]=04 buffer[2] 0 buffer[2] 1 buffer[2] 0 buffer[2] 1 ( ( ) ) MEX usbg.c usbg(0,255);% 0 255 data=usbg(0) % data 0 int usb_write(int port,int data), int usb_read(int port) int usb_init(void); int usb_close(void); 4 usbtest4.c int usb_write(int port,int data,handle fd){ char buffer[9]; int ch = 2; int count = 9; if (port == 0) ch = 1;

buffer[0]=0; buffer[1]=(char)ch; buffer[2]=(char)data; buffer[3]=0xff; buffer[4]=0xff; buffer[5]=0xff; buffer[6]=0xff; buffer[7]=0xff; buffer[8]=0xff; return WriteFile((HANDLE)fd,buffer,9,&count,0); int usb_read(int port,handle fd){ char buffer[9]; int ch = 4; int count = 9; if (port == 0) ch = 3; usb_write(port,0xff,fd); buffer[0]=0; buffer[1]=(char)ch; buffer[2]=0xff; buffer[3]=0xff; buffer[4]=0xff; buffer[5]=0xff; buffer[6]=0xff; buffer[7]=0xff; buffer[8]=0xff; WriteFile((HANDLE)fd,buffer,9,&count,0); ReadFile((HANDLE)fd,buffer,9,&count,0); return buffer[2]; HANDLE usb_init(void){ HDEVINFO dis; int count,i; BOOL r; DWORD size; SP_DEVICE_INTERFACE_DATA sdid; PSP_DEVICE_INTERFACE_DETAIL_DATA_A pdidd = NULL; size_t didd_size = 0; HANDLE dh,fd; HIDD_ATTRIBUTES attr; size_t len; char buffer[255]; // printf("%x %x %x n",(unsigned long)hidguid.data1,(unsigned long)hidguid.data2,(unsigned long)hidguid.data3); dis = SetupDiGetClassDevsA(&hidGuid, NULL, 0, DIGCF_PRESENT DIGCF_DEVICEINTERFACE ); count = 0; for (i = 0;;i++){ memset(&sdid,0,sizeof(sdid)); sdid.cbsize = sizeof( SP_DEVICE_INTERFACE_DATA ); r = SetupDiEnumDeviceInterfaces(dis,NULL,&hidGuid,i,&sdid); if (!r) break; size = 0; r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,NULL,0,&size,NULL); if ( r GetLastError()!= ERROR_INSUFFICIENT_BUFFER ) continue; if ( size > didd_size ) { didd_size = ( size + 15 ) & ~15; pdidd = malloc(didd_size); memset(pdidd,0,didd_size); pdidd->cbsize = sizeof(sp_device_interface_detail_data_a); r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,pdidd,size,NULL,NULL); if (!r ) continue; dh = CreateFileA( ( LPCSTR )pdidd->devicepath,generic_read GENERIC_WRITE,FILE_SHARE_READ if (dh == INVALID_HANDLE_VALUE ) continue; memset(&attr,0,sizeof(hidd_attributes)); attr.size = sizeof(hidd_attributes); HidD_GetAttributes(dh,&attr); CloseHandle(dh);

// printf("%s n",(lpcstr)pdidd->devicepath); // printf("%x %x %x",attr.vendorid,attr.productid,attr.versionnumber); count++; SetupDiDestroyDeviceInfoList( dis ); fd = CreateFile((LPCSTR)pdidd->DevicePath,GENERIC_READ GENERIC_WRITE,FILE_SHARE_READ // printf("[%x]",fd); free(pdidd); return (HANDLE)fd; void usb_close(handle fd) { CloseHandle(( HANDLE )fd); int i; HANDLE fd = usb_init(); for(i = 0;i < 20;i++) { printf("%x",usb_read(0,fd)); usb_write(0,i,fd); getch(); usb_close(fd); Mex main Mex MEX include mex.h MATLAB printf print mexprintf usbg.c // USB-IO for MATLAB by Gerox(c) 2003 int usb_write(int port,int data,handle fd){ char buffer[9]; int ch = 2; int count = 9; if (port == 0) ch = 1; buffer[0]=(char)0; buffer[1]=(char)ch; buffer[2]=(char)data; buffer[3]=(char)0xff; buffer[4]=(char)0xff; buffer[5]=(char)0xff; buffer[6]=(char)0xff; buffer[7]=(char)0xff; buffer[8]=(char)0xff; return WriteFile((HANDLE)fd,buffer,9,&count,0); int usb_read(int port,handle fd){ char buffer[9]; int ch = 4; int count = 9; if (port == 0) ch = 3; // usb_write(port,0xff,fd); buffer[0]=(char)0; buffer[1]=(char)ch; buffer[2]=(char)0xff; buffer[3]=(char)0xff; buffer[4]=(char)0xff; buffer[5]=(char)0xff; buffer[6]=(char)0xff;

buffer[7]=(char)0xff; buffer[8]=(char)0xff; WriteFile((HANDLE)fd,buffer,9,&count,0); ReadFile((HANDLE)fd,buffer,9,&count,0); return buffer[2]; HANDLE usb_init(void){ HDEVINFO dis; int count,i; BOOL r; DWORD size; SP_DEVICE_INTERFACE_DATA sdid; PSP_DEVICE_INTERFACE_DETAIL_DATA_A pdidd = NULL; size_t didd_size = 0; HANDLE dh,fd; HIDD_ATTRIBUTES attr; // printf("%x %x %x n",(unsigned long)hidguid.data1,(unsigned long)hidguid.data2,(unsigned long)hidguid.data3); dis = SetupDiGetClassDevsA(&hidGuid, NULL, 0, DIGCF_PRESENT DIGCF_DEVICEINTERFACE ); count = 0; for (i = 0;;i++){ memset(&sdid,0,sizeof(sdid)); sdid.cbsize = sizeof( SP_DEVICE_INTERFACE_DATA ); r = SetupDiEnumDeviceInterfaces(dis,NULL,&hidGuid,i,&sdid); if (!r) break; size = 0; r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,NULL,0,&size,NULL); if ( r GetLastError()!= ERROR_INSUFFICIENT_BUFFER ) continue; if ( size > didd_size ) { didd_size = ( size + 15 ) & ~15; pdidd = malloc(didd_size); memset(pdidd,0,didd_size); pdidd->cbsize = sizeof(sp_device_interface_detail_data_a); r = SetupDiGetDeviceInterfaceDetailA(dis,&sdid,pdidd,size,NULL,NULL); if (!r ) continue; dh = CreateFileA( ( LPCSTR )pdidd->devicepath,generic_read GENERIC_WRITE,FILE_SHARE_READ if (dh == INVALID_HANDLE_VALUE ) continue; memset(&attr,0,sizeof(hidd_attributes)); attr.size = sizeof(hidd_attributes); HidD_GetAttributes(dh,&attr); CloseHandle(dh); mexprintf("%s n",(lpcstr)pdidd->devicepath); mexprintf("vendorid=%x nproductid=%x nversion=%x n",attr.vendorid,attr.productid,attr.versionnumber); count++; SetupDiDestroyDeviceInfoList( dis ); fd = CreateFile((LPCSTR)pdidd->DevicePath,GENERIC_READ GENERIC_WRITE,FILE_SHARE_READ // printf("[%x]",fd); mexprintf("ready to use n"); free(pdidd); return (HANDLE)fd; void usb_close(handle fd) { CloseHandle(( HANDLE )fd); #if 0 int i; HANDLE fd = usb_init(); for(i = 0;i < 20;i++) { printf("%x",usb_read(0,fd)); usb_write(0,i,fd); getch(); usb_close(fd);

#else #include "mex.h" static int initialized = 0; static HANDLE Fd; void cleanup(void) { if(initialized!= 0) { usb_close(fd); mexprintf("mex-file is safely terminated. n"); initialized = 0; void mexfunction(int nlhs,mxarray *plhs[],int nrhs,const mxarray * prhs[]) { double data,*pldata; if(nlhs > 1) { mexerrmsgtxt("one output must be required.");return; if(initialized == 0) { Fd = usb_init(); mexatexit(cleanup); initialized++; // nrhs==1 read if(nrhs == 1) { data = (double)usb_read((int)mxgetscalar(prhs[0]),fd); else { if(nrhs == 2) { data =(double) usb_write((int)mxgetscalar(prhs[0]),(int)mxgetscalar(prhs[1]),fd); else return; plhs[0] = mxcreatedoublematrix(1,1,mxreal); pldata = mxgetpr(plhs[0]); pldata[0] = data; return; #endif MATLAB >> usbg(0,255)? hid#vid_0bfe&pid_1003#6&99bfe71&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030 VendorID=bfe ProductID=1003 Version=1 Ready to use ans = 1 0 255 2 ID >> usbg(0,8) ans = 1 USB clear usbg