Новичок
Профиль
Группа: Участник
Сообщений: 10
Регистрация: 20.3.2009
Репутация: нет Всего: нет
|
Там действительно есть цикл вот код. | Код | //---------------------------------------------------------------------------
#include <vcl.h> #pragma hdrstop
#include "main.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm"
#pragma hdrstop
#include <io.h> //для работы с файлами #include <fcntl.h> //для работы с файлами #include <sys\stat.h> //для работы с файлами
#include <stdio.h> #include "windows.h" #include "mpusbapi.h" // MPUSBAPI Header File
//--------------------------------------------------------------------------- #pragma argsused
void open();
// Global Vars char vid_pid[]= "vid_04d8&pid_000c"; // Default Demo Application Firmware Vid_04d8&Pid_0053 vid_04d8&pid_000c char out_pipe[]= "\\MCHP_EP1"; char in_pipe[]= "\\MCHP_EP1";
FILE *fp;
DWORD temp;
HINSTANCE libHandle; HANDLE myOutPipe; HANDLE myInPipe; WORD kof=1, kof1=1;
TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void LoadDLL(void) // функція завантаження бібліотеки { libHandle = NULL; libHandle = LoadLibrary("mpusbapi"); if(libHandle == NULL) { Form1->Edit1->Text="Error loading mpusbapi.dll"; } else { MPUSBGetDLLVersion=(DWORD(*)(void))\ GetProcAddress(libHandle,"_MPUSBGetDLLVersion"); MPUSBGetDeviceCount=(DWORD(*)(PCHAR))\ GetProcAddress(libHandle,"_MPUSBGetDeviceCount"); MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))\ GetProcAddress(libHandle,"_MPUSBOpen"); MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\ GetProcAddress(libHandle,"_MPUSBWrite"); MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\ GetProcAddress(libHandle,"_MPUSBRead"); MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\ GetProcAddress(libHandle,"_MPUSBReadInt"); MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(libHandle,"_MPUSBClose");
if((MPUSBGetDeviceCount == NULL) || (MPUSBOpen == NULL) || (MPUSBWrite == NULL) || (MPUSBRead == NULL) || (MPUSBClose == NULL) || (MPUSBGetDLLVersion == NULL) || (MPUSBReadInt == NULL)) Form1->Edit1->Text="GetProcAddress Error"; }//end if else }//end LoadDLL */
void CheckInvalidHandle(void) { if(GetLastError() == ERROR_INVALID_HANDLE) { // Most likely cause of the error is the board was disconnected. MPUSBClose(myOutPipe); MPUSBClose(myInPipe); myOutPipe = myInPipe = INVALID_HANDLE_VALUE; }//end if //else //Form1->Edit2->Text=GetLastError(); }//end CheckInvalidHandle
DWORD SendReceivePacket(BYTE *SendData, DWORD SendLength, BYTE *ReceiveData, DWORD *ReceiveLength, UINT SendDelay, UINT ReceiveDelay) { DWORD SentDataLength; DWORD ExpectedReceiveLength = *ReceiveLength;
if(myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE) { if(MPUSBWrite(myOutPipe,SendData,SendLength,&SentDataLength,SendDelay)) {
if(MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength, ReceiveLength,ReceiveDelay)) { if(*ReceiveLength == ExpectedReceiveLength) { return 1; // Success! } else if(*ReceiveLength < ExpectedReceiveLength) { return 2; // Partially failed, incorrect receive length }//end if else } else CheckInvalidHandle(); } else CheckInvalidHandle(); }//end if
return 0; // Operation Failed }//end SendReceivePacket*/
void __fastcall TForm1::Timer1Timer(TObject *Sender) { LoadDLL(); myOutPipe = MPUSBOpen(0,vid_pid,out_pipe,MP_WRITE,0); myInPipe = MPUSBOpen(0,vid_pid,in_pipe,MP_READ,0);
if (myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE) { Form1->Edit1->Text="Failed to open data pipes."; return; } else {Form1->Edit1->Text="Ok";}//end if
BYTE send_buf[64],receive_buf[64]; DWORD RecvLength=4;
#define READ_VERSION 0 send_buf[0] = READ_VERSION; // Command send_buf[1] = 0x02; // Expected length of the result
if(SendReceivePacket(send_buf,2,receive_buf,&RecvLength,1000,1000) == 1) { Label9->Caption=receive_buf[0]+receive_buf[1]*256; Label10->Caption=receive_buf[2]+receive_buf[3]*256; } else {Form1->Edit1->Text="USB Operation Failed";}
// Let's close the data pipes since we have nothing left to do.. MPUSBClose(myOutPipe); MPUSBClose(myInPipe); myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
} //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { char str[20]; WORD res1, res2, i=0, j=0; LoadDLL(); if ((fp = fopen("test.txt", "wt")) == NULL) {}
myOutPipe = MPUSBOpen(0,vid_pid,out_pipe,MP_WRITE,0); myInPipe = MPUSBOpen(0,vid_pid,out_pipe,MP_READ,0);
if (myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE) { Form1->Edit1->Text="Failed to open data pipes."; return; }//end if BYTE send_buf[64],receive_buf[64]; DWORD RecvLength=4; double ADRES,ADRES2; send_buf[0] = 0x37; // Read ch1.... send_buf[1] = 0x02; // Expected length of the result Form1->ProgressBar1->Max=atoi(Form1->ComboBox1->Text.c_str())*500; while (myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE && i<=atoi(Form1->ComboBox1->Text.c_str())*500) { //MPUSBRead(myInPipe,receive_buf, 64,&RecvLength, 1000); SendReceivePacket(send_buf,2,receive_buf,&RecvLength,1000,1000); j=0; //for (j=0; j<64; j+=1) { res1=receive_buf[0]+receive_buf[1]*256; res2=receive_buf[2]+receive_buf[3]*256; fprintf(fp, "%d",res1); fprintf(fp, "%c",9); fprintf(fp, "%d\n",res2); /*fprintf(fp, "%c",9); } fprintf(fp, "%d",receive_buf[j]); fprintf(fp, "%c",9); } fprintf(fp, "%d",i);*/ Form1->ProgressBar1->Position=i; i++; } Form1->ProgressBar1->Position=0; MPUSBClose(myOutPipe); MPUSBClose(myInPipe); myOutPipe = myInPipe = INVALID_HANDLE_VALUE; fclose(fp);
open(); }
|
|