Guest User

Untitled

a guest
Apr 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. void CUsbHidDemoDlg::OnSend()
  2. {
  3. char send_buf[1024];
  4. char recv_buf[1024;
  5. char seps[] = " ,\t\n";
  6. char *token;
  7. int status;
  8. int i, idx = 0;
  9. CString str;
  10.  
  11. int n;
  12. unsigned long size, myidx = 0, k;
  13. unsigned char *buf;
  14. size_t bsize = (sizeof(unsigned char) * myidx) + 1;
  15. FILE *f = fopen("hex.txt", "r");
  16. if(!f) exit(10);
  17.  
  18. myidx = 0;
  19. while(!feof(f)) { fscanf(f, "%02x ", &n); myidx++; }
  20. fseek(f, 0, SEEK_SET);
  21.  
  22. buf = (unsigned char *) malloc(bsize);
  23. memset(buf, 0, bsize);
  24.  
  25. myidx = 1;
  26. buf[0] = (unsigned char) m_csOutputReportID.GetBuffer(0)[0]; /* report id */
  27. while(!feof(f)) {
  28. fscanf(f, "%02x ", &n);
  29. buf[myidx++] = (unsigned char) n;
  30. }
  31.  
  32. printf("idx: %d\n", myidx);
  33.  
  34. for(k = 0; k < bsize; k += mdevices.m_WriteSize)
  35. {
  36. if(mdevices.Write(buf+k) == 0) {
  37. printf("write #%d failed\n", k);
  38. free(buf); fclose(f);
  39. exit(5);
  40. m_csStatus += "Write Failed: ";
  41.  
  42. LPVOID lpMsgBuf;
  43.  
  44. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  45. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
  46. GetLastError(),
  47. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  48. (LPTSTR) &lpMsgBuf, 0, NULL ); // Process any inserts in lpMsgBuf.
  49.  
  50. m_csStatus += (LPCTSTR)lpMsgBuf;
  51. m_csStatus += "\r\n";
  52.  
  53.  
  54. // Free the buffer.
  55. LocalFree( lpMsgBuf );
  56.  
  57. UpdateData(FALSE);
  58. return;
  59. }
  60. else
  61. printf("write #%d succeeded\n", k);
  62.  
  63. memset(send_buf, 0, sizeof(send_buf));
  64. Sleep(100);
  65.  
  66. status = mdevices.Read(recv_buf);
  67. if(status != 0) exit(6);
  68. if (status == 0)
  69. {
  70. m_csStatus += "Read Failed: ";
  71.  
  72. LPVOID lpMsgBuf;
  73.  
  74. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  75. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
  76. GetLastError(),
  77. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  78. (LPTSTR) &lpMsgBuf, 0, NULL ); // Process any inserts in lpMsgBuf.
  79.  
  80. m_csStatus += (LPCTSTR)lpMsgBuf;
  81. m_csStatus += "\r\n";
  82.  
  83. // Free the buffer.
  84. LocalFree( lpMsgBuf );
  85.  
  86. UpdateData(FALSE);
  87.  
  88. return;
  89. }
  90. else
  91. {
  92. m_csReadBuffer.Empty();
  93.  
  94. // The first byte is the report ID
  95. str.Format("%02x", (unsigned char)recv_buf[0]);
  96. m_csInputReportID = str;
  97.  
  98. // Extract and display the byte(s)
  99. for (i=1; i<status; ++i)
  100. {
  101. str.Format("%02x ", (unsigned char)recv_buf[i]);
  102. m_csReadBuffer += str;
  103. }
  104.  
  105. m_csStatus += "Read completed successfully!\r\n";
  106. }
  107.  
  108.  
  109. }
  110.  
  111. printf("everything succeeded\n");
  112.  
  113. free(buf);
  114. fclose(f);
  115.  
  116. UpdateData(TRUE);
  117.  
  118. m_csStatus.Empty();
  119. UpdateData(FALSE);
  120. UpdateWindow();
  121.  
  122. UpdateData(FALSE);
  123. }
Add Comment
Please, Sign In to add comment