Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CUsbHidDemoDlg::OnSend()
- {
- char send_buf[1024];
- char recv_buf[1024;
- char seps[] = " ,\t\n";
- char *token;
- int status;
- int i, idx = 0;
- CString str;
- int n;
- unsigned long size, myidx = 0, k;
- unsigned char *buf;
- size_t bsize = (sizeof(unsigned char) * myidx) + 1;
- FILE *f = fopen("hex.txt", "r");
- if(!f) exit(10);
- myidx = 0;
- while(!feof(f)) { fscanf(f, "%02x ", &n); myidx++; }
- fseek(f, 0, SEEK_SET);
- buf = (unsigned char *) malloc(bsize);
- memset(buf, 0, bsize);
- myidx = 1;
- buf[0] = (unsigned char) m_csOutputReportID.GetBuffer(0)[0]; /* report id */
- while(!feof(f)) {
- fscanf(f, "%02x ", &n);
- buf[myidx++] = (unsigned char) n;
- }
- printf("idx: %d\n", myidx);
- for(k = 0; k < bsize; k += mdevices.m_WriteSize)
- {
- if(mdevices.Write(buf+k) == 0) {
- printf("write #%d failed\n", k);
- free(buf); fclose(f);
- exit(5);
- m_csStatus += "Write Failed: ";
- LPVOID lpMsgBuf;
- FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
- GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf, 0, NULL ); // Process any inserts in lpMsgBuf.
- m_csStatus += (LPCTSTR)lpMsgBuf;
- m_csStatus += "\r\n";
- // Free the buffer.
- LocalFree( lpMsgBuf );
- UpdateData(FALSE);
- return;
- }
- else
- printf("write #%d succeeded\n", k);
- memset(send_buf, 0, sizeof(send_buf));
- Sleep(100);
- status = mdevices.Read(recv_buf);
- if(status != 0) exit(6);
- if (status == 0)
- {
- m_csStatus += "Read Failed: ";
- LPVOID lpMsgBuf;
- FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
- GetLastError(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPTSTR) &lpMsgBuf, 0, NULL ); // Process any inserts in lpMsgBuf.
- m_csStatus += (LPCTSTR)lpMsgBuf;
- m_csStatus += "\r\n";
- // Free the buffer.
- LocalFree( lpMsgBuf );
- UpdateData(FALSE);
- return;
- }
- else
- {
- m_csReadBuffer.Empty();
- // The first byte is the report ID
- str.Format("%02x", (unsigned char)recv_buf[0]);
- m_csInputReportID = str;
- // Extract and display the byte(s)
- for (i=1; i<status; ++i)
- {
- str.Format("%02x ", (unsigned char)recv_buf[i]);
- m_csReadBuffer += str;
- }
- m_csStatus += "Read completed successfully!\r\n";
- }
- }
- printf("everything succeeded\n");
- free(buf);
- fclose(f);
- UpdateData(TRUE);
- m_csStatus.Empty();
- UpdateData(FALSE);
- UpdateWindow();
- UpdateData(FALSE);
- }
Add Comment
Please, Sign In to add comment