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

📄 usbrw.~cpp

📁 EasyD12_PC动态连接库和例子
💻 ~CPP
字号:
//---------------------------------------------------------------------------
//软件名称:USB--I2C测试程序(For C++ Builder 5.0)
//开发时间:2002-4-10
//说明:本程序演示了如何在C++ Builder 5.0中静态调用动态链接库EasyD12.dll读写24WC系列E2ROM
//      其中在工程中静态加载动态链接库的步骤如下:
//      1、打开要调用EasyD12.dll 的工程;
//      2、在Project菜单中选择 “Add to Project...”,然后在弹出的对话框中
//         选择 easyd12forcb.lib 文件。点击确定。
//      3、在.cpp文件中声明
//           #include "EasyD12.h"
//版权所有:(C) 周立功单片机发展有限公司

#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)"应答错误!", "提示", 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,"应答错误!", "提示", 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();        
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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