?? usbrw.cpp
字號:
//---------------------------------------------------------------------------
//軟件名稱:USB--I2C測試程序(For C++ Builder 5.0)
//開發(fā)時間:2002-4-10
//說明:本程序演示了如何在C++ Builder 5.0中靜態(tài)調(diào)用動態(tài)鏈接庫EasyD12.dll讀寫24WC系列E2ROM
// 其中在工程中靜態(tài)加載動態(tài)鏈接庫的步驟如下:
// 1、打開要調(diào)用EasyD12.dll 的工程;
// 2、在Project菜單中選擇 “Add to Project...”,然后在彈出的對話框中
// 選擇 easyd12forcb.lib 文件。點擊確定。
// 3、在.cpp文件中聲明
// #include "EasyD12.h"
//版權(quán)所有:(C) 周立功單片機發(fā)展有限公司
#include <vcl.h>
#pragma hdrstop
#include "USBRW.h"
#include "EasyD12.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const int CardTypeAT[]={
CARD_AT24C01A,
CARD_AT24C02,
CARD_AT24C04,
CARD_AT24C08,
CARD_AT24C16,
CARD_AT24C64,
CARD_AT93C46,
CARD_AT93C46A,
CARD_AT45D041,
CARD_AT88SC102,
CARD_AT88SC1604,
CARD_AT88SC1604B
};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cbSelectChipChange(TObject *Sender)
{
nCurrentCardType=CardTypeAT[cbSelectChip->ItemIndex];
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
nCurrentCardType=CARD_AT24C01A;
cbSelectChip->Text="24WC01";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::edSendAdrKeyPress(TObject *Sender, char &Key)
{
if(Key==8){
edSendAdr->ReadOnly=false;
return;
}
if(!(Key>0x2F&&Key<0x3A))
edSendAdr->ReadOnly=true;
else
edSendAdr->ReadOnly=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::edSendLenKeyPress(TObject *Sender, char &Key)
{
if(Key==8){
edSendLen->ReadOnly=false;
return;
}
if(!(Key>0x2F&&Key<0x3A))
edSendLen->ReadOnly=true;
else
edSendLen->ReadOnly=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::edRcvAdrKeyPress(TObject *Sender, char &Key)
{
if(Key>0x60&&Key<0x67)
Key=Key-32;
if(Key==8){
edRcvAdr->ReadOnly=false;
return;
}
if(!((Key>0x2F&&Key<0x3A)||(Key>0x40 && Key<0x47 )))
edRcvAdr->ReadOnly=true;
else
edRcvAdr->ReadOnly=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::edRcvLenKeyPress(TObject *Sender, char &Key)
{
if(Key==8){
edRcvLen->ReadOnly=false;
return;
}
if(!(Key>0x2F&&Key<0x3A))
edRcvLen->ReadOnly=true;
else
edRcvLen->ReadOnly=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnSendClick(TObject *Sender)
{
int nSendAdr;
int nSendLen;
BYTE *cSendBuff;
BYTE buff[8];
String strSend;
if(edSendAdr->Text!="")
nSendAdr=StrToInt(edSendAdr->Text);
else
return;
if(edSendLen->Text!="")
nSendLen=StrToInt(edSendLen->Text);
else
return;
if(edSendData->Text!=""){
cSendBuff=new BYTE[edSendData->Text.Length()+1];
strSend=edSendData->Text;
}else
return;
strcpy((BYTE *)cSendBuff,strSend.c_str());
buff[0]='W';
buff[1]=nCurrentCardType%256;
buff[2]=nCurrentCardType/256;
buff[3]=nSendAdr%256;
buff[4]=nSendAdr/256;
buff[5]=nSendLen%256;
buff[6]=nSendLen/256;
buff[7]=buff[0]^buff[1]^buff[2]^buff[3]^buff[4]^buff[5]^buff[6];
if(WritePort1(buff,4)!=0){
MessageBox(this->Handle,"寫命令失敗!", "提示", MB_ICONSTOP);
return;
}
if(WritePort1(buff+4,4)!=0){
MessageBox(this->Handle,"寫命令失敗!", "提示", MB_ICONSTOP);
return;
}
if(ReadPort1(buff,2)!=0){
MessageBox(this->Handle,"讀端口1失敗!", "提示", MB_ICONSTOP);
return;
}
if(buff[0]!=0x55 && buff[1]!=0xaa){
MessageBox(this->Handle,(LPCSTR)"應(yīng)答錯誤!", "提示", MB_ICONSTOP);
return;
}
int nTempLen=min(nSendLen,edSendData->Text.Length());
if(WritePort2(cSendBuff,nTempLen)<0)
MessageBox(this->Handle,(LPCSTR)"寫出錯!", "提示", MB_ICONSTOP);
delete cSendBuff;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnRcvClick(TObject *Sender)
{
int nRcvAdr;
int nRcvLen;
BYTE *cRcvBuff;
BYTE buff1[8];
BYTE buff2[2];
if(edRcvAdr->Text!="")
nRcvAdr=StrToInt(edRcvAdr->Text);
else
return;
if(edRcvLen->Text!="")
nRcvLen=StrToInt(edRcvLen->Text);
else
return;
cRcvBuff=new BYTE[nRcvLen];
buff1[0]='R';
buff1[1]=nCurrentCardType%256;
buff1[2]=nCurrentCardType/256;
buff1[3]=nRcvAdr%256;
buff1[4]=nRcvAdr/256;
buff1[5]=nRcvLen%256;
buff1[6]=nRcvLen/256;
buff1[7]=buff1[0]^buff1[1]^buff1[2]^buff1[3]^buff1[4]^buff1[5]^buff1[6];
if(WritePort1(buff1,4)!=0){
MessageBox(this->Handle,"寫命令失敗!", "提示", MB_ICONSTOP);
return;
}
if(WritePort1(buff1+4,4)!=0){
MessageBox(this->Handle,"寫命令失敗!", "提示", MB_ICONSTOP);
return;
}
if(ReadPort1(buff2,2)!=0){
MessageBox(this->Handle,"讀端口1失敗!", "提示", MB_ICONSTOP);
return;
}
if(buff2[0]!=0x55 && buff2[1]!=0xaa){
MessageBox(this->Handle,"應(yīng)答錯誤!", "提示", MB_ICONSTOP);
return;
}
if(ReadPort2(cRcvBuff,nRcvLen)!=0){
MessageBox(this->Handle,"讀端口2失敗!", "提示", MB_ICONSTOP);
return;
}
char hexBuff[4];
String HexString;
for(int i=0;i<nRcvLen;i++){
sprintf(hexBuff,"%02X",int(cRcvBuff[i]));
HexString+=String(hexBuff).UpperCase();
}
edRcvData->Text=HexString;
delete[] cRcvBuff;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BtnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -