Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "usb.h"
- #include "string.h"
- #include "iconv.h"
- int OpenMyDevice(struct usb_device **device, usb_dev_handle **udev_handle, int VID, int PID)
- {
- struct usb_bus *bus;
- struct usb_device *dev;
- usb_dev_handle *udev;
- int isFound = 0;
- usb_init();
- usb_find_busses();
- usb_find_devices();
- for (bus = usb_busses; bus; bus = bus->next)
- {
- for (dev = bus->devices; dev; dev = dev->next)
- {
- udev = usb_open(dev);
- if ( (dev->descriptor.idVendor == VID) & (dev->descriptor.idProduct == PID))
- {
- isFound = 1;
- break;
- }
- else
- {
- if (udev)
- usb_close(udev);
- }
- }
- if (isFound)
- break;
- }
- *device = dev;
- *udev_handle = udev;
- return isFound;
- }
- //Buffer to read
- #define BUFREAD 64
- #define BUFWRITE 512
- int main(void)
- {
- struct usb_device *dev;
- usb_dev_handle *udev;
- int rc;
- char s[BUFREAD];
- memset( &s, 0, sizeof s );
- char *msg;
- msg = malloc(BUFWRITE);
- //Open DEVICE
- if (OpenMyDevice(&dev, &udev, 0x19d2, 0x1217) != 1)
- {
- fprintf(stderr, "Device not found\n");
- exit(1);
- }
- //Sets main configuration
- if (usb_set_configuration(udev, 1) != 0) //bConfigurationValue 1
- {
- usb_close(udev);
- fprintf(stderr, "Couldn't set USB active configuration\n");
- exit(-1);
- }
- //WRITE TO USB DEVICE
- if (usb_claim_interface(udev, 1) != 0)
- {
- usb_close(udev);
- fprintf(stderr, "Couldn't claim USB interface\n");
- exit(-1);
- }
- if (usb_set_altinterface(udev, 0) != 0)
- {
- usb_close(udev);
- printf("Couldn't alternate USB interface\n");
- exit(-1);
- }
- msg = "AT\r\n";
- int NUM = strlen(msg);
- rc = usb_bulk_write(udev, 0x01, (char *)msg, NUM, 32);
- if (rc < 0)
- fprintf(stderr, "WRITE ERROR\n");
- printf("written: %i bytes\n", rc);
- //READ FROM USB DEVICE
- //Release previously claimed interface
- if (usb_release_interface(udev, 1) != 0)
- {
- usb_close(udev);
- fprintf(stderr, "Couldn't release USB interface\n");
- exit(-1);
- }
- if (usb_claim_interface(udev, 0) != 0)
- {
- usb_close(udev);
- fprintf(stderr, "Couldn't claim USB interface\n");
- exit(-1);
- }
- if (usb_set_altinterface(udev, 0) != 0)
- {
- usb_close(udev);
- printf("Couldn't alternate USB interface\n");
- exit(-1);
- }
- // usb_interrupt_read usb_bulk_read
- rc = usb_interrupt_read(udev, 0x81, s, BUFREAD, 5);
- if (rc < 0)
- fprintf(stderr, "READ ERROR\n");
- printf("Read from device %i bytes\n", rc);
- //CONVERTING ANSWER TO UTF-8
- iconv_t cd;
- cd = iconv_open("UTF-8", "UCS-2");
- char converted[1024];
- size_t k, strsize, t;
- char *IN, *OUT;
- OUT = (char*)converted;
- IN = (char*)s;
- if( cd == (iconv_t)(-1) )
- err( 1, "iconv_open" );
- strsize = strlen(s);
- t = sizeof converted;
- memset( &converted, 0, sizeof converted );
- k = iconv(cd, &IN, &strsize, &OUT, &t);
- iconv_close(cd);
- printf("Message sent to device:%s\n", msg);
- printf("%s\n", "Read data as int");
- int j;
- for (j=0; j<rc; j++)
- printf("%i\n", s[j]);
- printf("Read data as string: %s\n", s);
- printf("%s\n", converted);
- usb_close(udev);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement