⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbmain.cpp

📁 用于和CH372 USB DEVICE CHIP 通讯,稍加修改可以上用于其他USB DEVICE, 使用WINDRIVER 编写
💻 CPP
📖 第 1 页 / 共 2 页
字号:

 SendCommand(&Commands,&Commands.lengthc);
  if(!GetStatus(&Status,&Commands.lengths))
  {if(Status.Mode==Commands.Mode)
   {ShowMessagem("AD0809 Mode connection OK!!!");
    for(i=0,j=0;oldmode==Commands.Mode&& oldpattern==Commands.Pattern;i++,j++)
    {if(i==64*1024/8)
      {i=0;
       FileWrite(FileHandle, FileContent, sizeof(FileContent)-1);
      }
     gettime(&temptime);
     timer=temptime.ti_hour*360000+temptime.ti_min*6000+temptime.ti_sec*100+temptime.ti_hund;
     timer=timer-originaltime;
     ReadData(FileContent+i*8,&Commands.lengthr);

     temps1=*(FileContent+i*8+0),temps2=*(FileContent+i*8+1);
     temps3=*(FileContent+i*8+2),temps4=*(FileContent+i*8+3);
     temps5=*(FileContent+i*8+4),temps6=*(FileContent+i*8+5);
     temps7=*(FileContent+i*8+6),temps8=*(FileContent+i*8+7);

     USBMajor->Series1->AddXY(timer,temps1,"",clRed);// Add a new  point
     USBMajor->Series2->AddXY(timer,temps2,"",clBlue);// Add a new  point
     USBMajor->Series3->AddXY(timer,temps3,"",clRed);// Add a new  point
     USBMajor->Series4->AddXY(timer,temps4,"",clBlue);// Add a new  point
     USBMajor->Series5->AddXY(timer,temps5,"",clRed);// Add a new  point
     USBMajor->Series6->AddXY(timer,temps6,"",clBlue);// Add a new  point
     USBMajor->Series7->AddXY(timer,temps7,"",clRed);// Add a new  point
     USBMajor->Series8->AddXY(timer,temps8,"",clBlue);// Add a new  point

      if(USBMajor->Series1->Count()>100)
      {
       USBMajor->Series1->Delete(0); // <-- remove the first point
       USBMajor->Series2->Delete(0); // <-- remove the first point
       USBMajor->Series3->Delete(0); // <-- remove the first point
       USBMajor->Series4->Delete(0); // <-- remove the first point
       USBMajor->Series5->Delete(0); // <-- remove the first point
       USBMajor->Series6->Delete(0); // <-- remove the first point
       USBMajor->Series7->Delete(0); // <-- remove the first point
       USBMajor->Series8->Delete(0); // <-- remove the first point
      }

     if(j*8>1.9*1024*1024*1024)
      {Commands.finish=true;
       ShowMessagem("File size reach the limitation of 1.9G, this sample end!!!");
       break;
      }
    }
   FileWrite(FileHandle, FileContent, i*8);
   }
   else
    ShowMessagem("AD0809 Mode feedback error, connection fail!!!");
  }
  else
   ShowMessagem("AD0809 Mode connecting fial!!!");
  FileClose(FileHandle);
}

void __fastcall TUSBMajor::ReadClick(TObject *Sender)
{
 if(USBMajor->Sampleselect->Checked==true)
  {Commands.Mode=0x00;
   Commands.Pattern=0x01;
   Commands.Update=0x01;
  }
  else
  {Commands.Mode=0x03;
   Commands.Update=0x01;
  }
  Commands.finish=false;
  Progressing(1);

}
//---------------------------------------------------------------------------

void __fastcall TUSBMajor::ClearClick(TObject *Sender)
{Status->Text="";
}
//---------------------------------------------------------------------------

void __fastcall TUSBMajor::FormCreate(TObject *Sender)
{
    USBMajor->Series1->FillSampleValues(100);
    USBMajor->Series2->FillSampleValues(100);
    USBMajor->Series3->FillSampleValues(100);
    USBMajor->Series4->FillSampleValues(100);
    USBMajor->Series5->FillSampleValues(100);
    USBMajor->Series6->FillSampleValues(100);
    USBMajor->Series7->FillSampleValues(100);
    USBMajor->Series8->FillSampleValues(100);
    
    BZERO(DrvCtx);
    USBMajor->Progress->StartFrame=2;
    USBMajor->Progress->StopFrame=2;
    USBMajor->Progress->Active=true;

    wVendorId = USE_DEFAULT;
    wProductId = USE_DEFAULT;
    Commands.finish=true;

    // use defaults
    if (wVendorId == USE_DEFAULT)
        wVendorId = DEFAULT_VENDOR_ID;
    if (wProductId == USE_DEFAULT)
        wProductId = DEFAULT_PRODUCT_ID;

    dwError = OsEventCreate(&DrvCtx.hEvent);
    if (dwError)
    {
        ShowMessagem("main: OsEventCreate() failed on event 1");
        goto Exit;
    }

    dwError = OsMutexCreate(&DrvCtx.hMutex);
    if (dwError)
    {
        ShowMessagem("main: OsEventCreate() failed on event 2");
        goto Exit;
    }

    dwError = OsEventCreate(&DrvCtx.hDeviceUnusedEvent);
    if (dwError)
    {
        ShowMessagem("main: OsEventCreate() failed on event 3");
        goto Exit;
    }
    OsEventSignal(DrvCtx.hDeviceUnusedEvent);

    BZERO(matchTable);
    matchTable.wVendorId = wVendorId;
    matchTable.wProductId = wProductId;

    BZERO(eventTable);
    eventTable.pfDeviceAttach = DeviceAttach;
    eventTable.pfDeviceDetach = DeviceDetach;
    eventTable.pUserData = &DrvCtx;

    dwError = WDU_Init(&hDriver, &matchTable, 1, &eventTable, DEFAULT_LICENSE_STRING, WD_ACKNOWLEDGE);
    if (dwError)
    {
        ShowMessagem("main: OsEventCreate() failed on event 4");
        goto Exit;
    }

Exit:
}
//---------------------------------------------------------------------------

void __fastcall TUSBMajor::FormDestroy(TObject *Sender)
{ 
    if (DrvCtx.hEvent)
        OsEventClose(DrvCtx.hEvent);
    if (DrvCtx.hMutex)
        OsMutexClose(DrvCtx.hMutex);
    if (DrvCtx.hDeviceUnusedEvent)
        OsEventClose(DrvCtx.hDeviceUnusedEvent);
    if (hDriver)
        WDU_Uninit(hDriver);
     WDU_PutDeviceInfo(pDevice);

}
//---------------------------------------------------------------------------

void Send_Display_Command(void)
{
  SendCommand(&Commands,&Commands.lengthc);
  if(!GetStatus(&Commands,&Commands.lengths))
  {if(Status.Mode==Commands.Mode)
   {ShowMessagem("Display Mode connection OK!!!");
    if(!WriteData(Commands.wbuf,&Commands.lengthw))
     {goto exitd;
     }
   }
   else
    ShowMessagem("Display Mode feedback error, connection fail!!!");
  }
 else
  ShowMessagem("Display Mode connecting fial!!!");
exitd:
}

void __fastcall TUSBMajor::SendClick(TObject *Sender)
{
 AnsiString temp;
 char * tempp;

 Commands.Mode=0x01;
 Commands.Pattern=0x00; 

 temp=USBMajor->DData->Text;
 tempp=temp.c_str();
 if(tempp[0]>'F'||tempp[0]<'0'||tempp[1]>'F'||tempp[0]<'0')
  {ShowMessagem(" Data is not valid!!! Retry");
   USBMajor->DData->Text="";
   goto exita;
  }
  Commands.wbuf[0]=tempp[0]-0x31;
  Commands.wbuf[1]=tempp[1]-0x31;
  Commands.lengthw=0x02;
  Commands.lengths=0x0;
  Commands.lengthc=0x08; 
  Commands.finish=false;
  Progressing(1);

exita:
}
//---------------------------------------------------------------------------

void Send_Dial_Command(void)
{
 SendCommand(&Commands,&Commands.lengthc);
  if(!GetStatus(&Status,&Commands.lengths))
  {if(Status.Mode==Commands.Mode)
   {ShowMessagem("DIAL Mode connection OK!!!");
   }
   else
    ShowMessagem("DIAL Mode feedback error, connection fail!!!");
  }
  else
   ShowMessagem("DIAL Mode connecting fial!!!");
}

void __fastcall TUSBMajor::DialClick(TObject *Sender)
{
 AnsiString temp;
 char * tempp;

 Commands.Mode=0x02;
 Commands.Update=0X01;

 temp=USBMajor->PhoneNumber->Text;
 tempp=temp.c_str();
 if(tempp[0]>'9'||tempp[0]<'0'||tempp[1]>'9'||tempp[0]<'0'||
    tempp[2]>'9'||tempp[2]<'0'||tempp[3]>'9'||tempp[3]<'0')
  {ShowMessagem(" Data is not valid!!! Retry");
   USBMajor->DData->Text="";
   goto exitb;
  }

  Commands.L4.Number[0]=tempp[0]-0x31;
  Commands.L4.Number[1]=tempp[1]-0x31;
  Commands.L4.Number[2]=tempp[2]-0x31;
  Commands.L4.Number[3]=tempp[3]-0x31;

  Commands.lengthc=0x08;
  Commands.lengths=0x00; 
  Commands.finish=false;
  Progressing(1);

exitb:

}
//---------------------------------------------------------------------------

void __fastcall TUSBMajor::OpenFileClick(TObject *Sender)
{AnsiString FileName;
 int FileHandle;
 unsigned long FileLength;

 USBMajor->Open_File->Execute();
 FileName=USBMajor->Open_File->FileName;
 ShowMessagem(FileName.c_str());
 USBMajor->File_Name->Text=FileName;

 FileHandle = FileOpen(FileName, fmOpenRead);
 if(FileHandle<0)
   {ShowMessagem("File open fail!!!");
    FileClose(FileHandle);
    return;
   }
 FileLength = FileSeek(FileHandle,0,2);
 if(FileLength>64*1024)
   {ShowMessagem("File size larger than 64*1024!!!");
    FileClose(FileHandle);
    return;
   }
 FileSeek(FileHandle,0,0);
 FileRead(FileHandle, FileContent, FileLength);
 FileClose(FileHandle);

 ProgramLength=FileLength;
 Fileflag=true;

}
//---------------------------------------------------------------------------

void ProgramEEPROM(void)
{ unsigned rest,cycle,i;
  USBMajor->PI->Color=clRed;
  SendCommand(&Commands,&Commands.lengthc);  //Program !
  if(!GetStatus(&Status,&Commands.lengths))
  {if(Status.Mode==Commands.Mode)
   {ShowMessagem("Program Mode connection OK!!!");
    rest=ProgramLength%8;
    cycle=ProgramLength/8;
    for(i=0;i<cycle;i++)
    {Commands.lengthw=8;
     if(!WriteData((FileContent+i*8),&Commands.lengthw))
      {USBMajor->PI->Color=clYellow;
       goto exitc;
      }
    }
    Commands.lengthw=rest;
    if(!WriteData((FileContent+i*8),&Commands.lengthw))
     {USBMajor->PI->Color=clYellow;
      goto exitc;
     }
    Status.lengths=0;
    if(!GetStatus(&Status,&Status.lengths))
    {if(Status.Mode==0xfc)
      USBMajor->PI->Color=clGreen;
    }

    //read eeprom 
   }
   else
    ShowMessagem("Program Mode feedback error, connection fail!!!");
  }
  else
   ShowMessagem("DProgram Mode connecting fial!!!");
exitc:
}

void __fastcall TUSBMajor::ProgramClick(TObject *Sender)
{if(Fileflag==true)
 {Commands.Mode=0x04;
  Commands.Update=0X1;
  Commands.Pattern=0x0;
  *((unsigned*)&(Commands.L4.Number[0]))=(unsigned)ProgramLength;

  Commands.lengthc=0x08;
  Commands.lengths=0x00;
  Commands.finish=false;
  Progressing(1);
 }
 else
  {ShowMessagem("please open the file for programing!!!");
  }


}
//---------------------------------------------------------------------------

unsigned char convert(unsigned char dcode)
{unsigned char code[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67,0x77,
                         0x7c,0x39,0x5e,0x79,0x71};
 unsigned char i;
  for(i=0;i<16;i++)
  {if(dcode==code[i])
    return(i+31);
  }
 return(0);
}

void Get_Debug_Code(void)
{ unsigned char oldmode=0xff,oldpattern=0xff;
  oldmode=Commands.Mode;
  oldpattern=Commands.Pattern;
  AnsiString temp="";
  unsigned char tempc;

  SendCommand(&Commands,&Commands.lengthc);
  if(!GetStatus(&Status,&Commands.lengths))
  {if(Status.Mode==Commands.Mode&&oldpattern==Commands.Pattern)
   {ShowMessagem("Debug Mode connection OK!!!");
    for(;Commands.Mode==oldmode;)
    {ReadData(Commands.rbuf,&Commands.lengthr);
     tempc=convert(Commands.rbuf[0]);
     if(tempc !=0)
     {temp=temp+tempc;
      USBMajor->Debugcode->Text=temp;
     }
     else
      ShowMessagem("Debgu code error!!!");
    }
   }
   else
    ShowMessagem("Debgu Mode feedback error, connection fail!!!");
  }
  else
   ShowMessagem("Debug Mode connecting fial!!!");
  if(Commands.Mode!=oldmode||oldpattern!=Commands.Pattern)
   ShowMessagem("Current transaction abort! and switch to another MODE");
}

void __fastcall TUSBMajor::NewClick(TObject *Sender)
{Commands.Mode=0x01;
 Commands.Update=0x1;
 Commands.Pattern=0x1;
 *((unsigned*)&(Commands.L4.Number[0]))=(unsigned)ProgramLength;

 Commands.lengthc=0x08;
 Commands.lengths=0x00;
 Commands.finish=false;
 Progressing(1);


}
//---------------------------------------------------------------------------

void Record_Data(void)
{
  SendCommand(&Commands,&Commands.lengthc);
  if(!GetStatus(&Status,&Commands.lengths))
  {if(Status.Mode==Commands.Mode)
   {ShowMessagem("Record Mode connection OK!!!");
   }
   else
    ShowMessagem("Record Mode feedback error, connection fail!!!");
  }
  else
   ShowMessagem("Record Mode connecting fial!!!");
}

void __fastcall TUSBMajor::RecordClick(TObject *Sender)
{Commands.Mode=0x0;
 Commands.Update=0x1;
 Commands.Pattern=0x3;

 Commands.lengthc=0x08;
 Commands.lengths=0x00;
 Commands.finish=false;
 Progressing(1);
}


//---------------------------------------------------------------------------

void __fastcall TUSBMajor::StopClick(TObject *Sender)
{Commands.Mode=0xfa;
 Commands.Pattern=0xfa;
}
//---------------------------------------------------------------------------





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -