![]() |
|
Tài trợ cho PIC Vietnam |
Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC |
![]() |
|
Ðiều Chỉnh | Xếp Bài |
|
![]() |
#1 |
Đệ tử 8 túi
|
Trong cái ví dụ sử dụng thư viện dll, người ta xây dựng theo kiểu truyền đi, xong chờ nhận lại kết quả, rồi mới tính tiếp, mỗi lần truyền nhận như vậy delay tới 1s, do đó không thể nhanh được.
Bạn thử hạ thời gian delay xem thế nào, xem thử hạ tới đâu thì vẫn còn hoạt động tốt, xong rồi thông báo kết quả lên đây, để anh em thảo luận thêm. Làm theo vi dụ này, có thể tạm ứng dụng trong điều khiển với dung lượng dữ liệu cần truyền nhận nhỏ. |
![]() |
![]() |
![]() |
#2 | |
Đệ tử 3 túi
Tham gia ngày: Jun 2005
Bài gửi: 56
: |
Trích:
![]() Em thử hạ thời gian xuống, cỡ trên 10ms thì ổn định. Còn nếu <10ms thì lỗi độ dài dữ liệu nhân được < độ dài được chỉ định. |
|
![]() |
![]() |
![]() |
#3 |
Trưởng lão PIC bang
Tham gia ngày: Jun 2006
Bài gửi: 39
: |
Tôi phân tích tiếp xem thế nào nhé
![]() Cái ví dụ của microchip dùng trễ 100ms MPUSBRead(myInPipe,ReceiveData,ExpectedReceiveLeng th,ReceiveLength,100); bây giờ dịch tìm cái nguồn của hàm này, buộc dịch lại dll. trong file nguồn ta thấy: /////////////////////////////////////////////////////////////////////////////// // MPUSBRead : // // handle - Identifies the endpoint pipe to be read. The pipe handle must // have been created with MP_READ access attribute. // // pData - Points to the buffer that receives the data read from the pipe. // // dwLen - Specifies the number of bytes to be read from the pipe. // // pLength - Points to the number of bytes read. MPUSBRead sets this value to // zero before doing any work or error checking. // // dwMilliseconds // - Specifies the time-out interval, in milliseconds. The function // returns if the interval elapses, even if the operation is // incomplete. If dwMilliseconds is zero, the function tests the // data pipe and returns immediately. If dwMilliseconds is INFINITE, // the function's time-out interval never elapses. // // Note that "input" and "output" refer to the parameter designations in calls // to this function, which are the opposite of common sense from the // perspective of an application making the calls. // DWORD MPUSBRead(HANDLE handle, // Input PVOID pData, // Output DWORD dwLen, // Input PDWORD pLength, // Output DWORD dwMilliseconds) // Input { BOOL bResult; DWORD nBytesRead; OVERLAPPED gOverlapped; DWORD dwResult; dwResult = MPUSB_FAIL; // set up overlapped structure fields gOverlapped.Internal = 0; gOverlapped.InternalHigh = 0; gOverlapped.Offset = 0; gOverlapped.OffsetHigh = 0; gOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if(pLength != NULL)*pLength = 0; // attempt an asynchronous read operation bResult = ReadFile(handle,pData,dwLen,&nBytesRead,&gOverlapp ed); if(!bResult) { // deal with the error code switch (GetLastError()) { case ERROR_HANDLE_EOF: { // we have reached the end of the file // during the call to ReadFile break; } case ERROR_IO_PENDING: { // asynchronous i/o is still in progress switch(WaitForSingleObject(gOverlapped.hEvent, dwMilliseconds)) ... tôi trích đến đây vì thấy hàm WaitForSingleObject(...). Tham khảo link sau để thấy nó làm gì http://msdn.microsoft.com/library/de...ngleobject.asp như vậy ta có thể thử với biến dwMilliseconds, Các bạn tiếp tục xem thế nào. |
![]() |
![]() |
![]() |
|
|