Advertisement
kozdiabka

Talk to MTS 3G modem MF192+ via USB in C

Mar 12th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "usb.h"
  3. #include "string.h"
  4. #include "iconv.h"
  5.  
  6. int OpenMyDevice(struct usb_device **device, usb_dev_handle **udev_handle, int VID, int PID)
  7. {
  8.     struct usb_bus *bus;
  9.     struct usb_device *dev;
  10.     usb_dev_handle *udev;
  11.  
  12.     int isFound = 0;
  13.  
  14.     usb_init();
  15.     usb_find_busses();
  16.     usb_find_devices();
  17.  
  18.     for (bus = usb_busses; bus; bus = bus->next)
  19.     {
  20.         for (dev = bus->devices; dev; dev = dev->next)
  21.         {
  22.             udev = usb_open(dev);
  23.             if ( (dev->descriptor.idVendor == VID) & (dev->descriptor.idProduct == PID))
  24.             {
  25.                 isFound = 1;
  26.                 break;
  27.             }
  28.             else
  29.             {
  30.                 if (udev)
  31.                     usb_close(udev);
  32.             }
  33.         }
  34.         if (isFound)
  35.             break;
  36.     }
  37.  
  38.     *device = dev;
  39.     *udev_handle = udev;
  40.  
  41.     return isFound;
  42.  
  43. }
  44. //Buffer to read
  45. #define BUFREAD 64
  46. #define BUFWRITE 512
  47.  
  48. int main(void)
  49. {
  50.     struct usb_device *dev;
  51.     usb_dev_handle *udev;
  52.     int rc;
  53.  
  54.     char s[BUFREAD];
  55.     memset( &s, 0, sizeof s );
  56.     char *msg;
  57.    
  58.     msg = malloc(BUFWRITE);
  59.  
  60.    
  61. //Open DEVICE
  62.     if (OpenMyDevice(&dev, &udev, 0x19d2, 0x1217) != 1)
  63.     {
  64.         fprintf(stderr, "Device not found\n");
  65.         exit(1);
  66.     }
  67.  
  68.    
  69. //Sets main configuration
  70.     if (usb_set_configuration(udev, 1) != 0) //bConfigurationValue     1
  71.     {
  72.         usb_close(udev);
  73.         fprintf(stderr, "Couldn't set USB active configuration\n");
  74.         exit(-1);
  75.     }
  76. //WRITE TO USB DEVICE      
  77.     if (usb_claim_interface(udev, 1) != 0)
  78.     {
  79.         usb_close(udev);
  80.         fprintf(stderr, "Couldn't claim USB interface\n");
  81.         exit(-1);
  82.     }
  83.    
  84.     if (usb_set_altinterface(udev, 0) != 0)
  85.     {
  86.         usb_close(udev);
  87.         printf("Couldn't alternate USB interface\n");
  88.         exit(-1);
  89.     }
  90.  
  91.     msg = "AT\r\n";
  92.     int NUM = strlen(msg);
  93.     rc = usb_bulk_write(udev, 0x01, (char *)msg, NUM, 32);
  94.     if (rc < 0)
  95.         fprintf(stderr, "WRITE ERROR\n");
  96.     printf("written: %i bytes\n", rc);
  97.  
  98. //READ FROM USB DEVICE
  99. //Release previously claimed interface
  100.     if (usb_release_interface(udev, 1) != 0)
  101.     {
  102.         usb_close(udev);
  103.         fprintf(stderr, "Couldn't release USB interface\n");
  104.         exit(-1);
  105.     }
  106.    
  107.     if (usb_claim_interface(udev, 0) != 0)
  108.     {
  109.         usb_close(udev);
  110.         fprintf(stderr, "Couldn't claim USB interface\n");
  111.         exit(-1);
  112.     }
  113.    
  114.     if (usb_set_altinterface(udev, 0) != 0)
  115.     {
  116.         usb_close(udev);
  117.         printf("Couldn't alternate USB interface\n");
  118.         exit(-1);
  119.     }
  120.  
  121. //  usb_interrupt_read usb_bulk_read
  122.     rc = usb_interrupt_read(udev, 0x81, s, BUFREAD, 5);
  123.     if (rc < 0)
  124.         fprintf(stderr, "READ ERROR\n");   
  125.  
  126.     printf("Read from device %i bytes\n", rc);
  127.  
  128.  
  129. //CONVERTING ANSWER TO UTF-8       
  130.     iconv_t cd;
  131.     cd = iconv_open("UTF-8", "UCS-2");
  132.     char converted[1024];
  133.  
  134.     size_t k, strsize, t;
  135.  
  136.     char *IN, *OUT;
  137.     OUT = (char*)converted;
  138.     IN = (char*)s;
  139.  
  140.     if( cd == (iconv_t)(-1) )
  141.         err( 1, "iconv_open" );
  142.        
  143.     strsize = strlen(s);
  144.     t = sizeof converted;
  145.     memset( &converted, 0, sizeof converted );
  146.  
  147.     k = iconv(cd, &IN, &strsize, &OUT, &t);
  148.     iconv_close(cd);
  149.  
  150.  
  151.  
  152.     printf("Message sent to device:%s\n", msg);
  153.     printf("%s\n", "Read data as int");
  154.     int j;
  155.     for (j=0; j<rc; j++)
  156.         printf("%i\n", s[j]);
  157.     printf("Read data as string: %s\n", s);
  158.     printf("%s\n", converted);
  159.     usb_close(udev);
  160.  
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement