高分解bcb程序转delphi7(200分) ( 积分: 200 )

  • 主题发起人 deltaSoft
  • 开始时间
D

deltaSoft

Unregistered / Unconfirmed
GUEST, unregistred user!
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include &quot;SkinCaption.hpp&quot;
#include &quot;WinSkinData.hpp&quot;
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TComboBox *commindex;
TLabel *Label8;
TButton *Button7;
TGroupBox *GroupBox3;
TEdit *Edit_edc;
TButton *Button_caledc;
TLabel *Label11;
TEdit *Edit_edc_cal;
TLabel *Label12;
TButton *Button_updata;
TGroupBox *GroupBox1;
TButton *Button1;
TEdit *Edit1;
TGroupBox *GroupBox4;
TButton *Button2;
TEdit *Edit2;
TLabel *Label4;
TCheckBox *CheckBox1;
TGroupBox *GroupBox2;
TLabel *Label5;
TButton *Button3;
TEdit *Edit3;
TButton *Button4;
TLabel *Label6;
TEdit *Edit4;
TLabel *Label7;
TLabel *Label9;
TEdit *Edit5;
TEdit *Edit6;
TLabel *Label10;
void __fastcall Button7Click(TObject *Sender);
void __fastcall Button_opencjzdClick(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button_updataClick(TObject *Sender);
void __fastcall Button_caledcClick(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall CheckBox1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
HWND m_hComHandle;
bool commstate;
bool __fastcall TForm1::eek:pencomm(unsigned char byPort);
bool __fastcall TForm1::closecomm(void);

void __fastcall settimeout(DWORD itime,DWORD otime);
void __fastcall mydelayms(DWORD xms);
void __fastcall sendandrev(unsigned char* sendchar,unsigned char *sendlen,unsigned char* revchar,unsigned char *revlen);
void __fastcall intohistory(unsigned char* sendchar,unsigned char *sendlen,unsigned char* revchar,unsigned char *revlen);


};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------




#endif

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

#include <vcl.h>
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link &quot;SkinCaption&quot;
#pragma link &quot;WinSkinData&quot;
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
AnsiString formatmytext(AnsiString textls);//格式化字符串
int isrighthex(AnsiString textls,int maxlen); //判断字符串是否为十六制格式
bool isrightint(AnsiString textls);//判断字符串是否能转换成整型
unsigned char mytexttopiccdata(AnsiString textls,unsigned char *piccdata);//将字符串转化成字节

AnsiString historytext;//通讯历史记录字符串

//--------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
/*
超级终端程序hypertrm.exe

*/
void __fastcall TForm1::Button7Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button_opencjzdClick(TObject *Sender)
{
AnsiString WinDir;
AnsiString winpath;
UINT BufferSize = GetWindowsDirectory(NULL,0);
if(BufferSize>0)
{
WinDir.SetLength(BufferSize+1);
GetWindowsDirectory(WinDir.c_str(),BufferSize+1);
winpath = (AnsiString)(WinDir.c_str());
if(WinDir.SubString(WinDir.Length(),1).operator !=(&quot;//&quot;))
{

winpath +=(&quot;//&quot;);
}


//创建目录
ShellExecute(NULL,&quot;open&quot;,&quot;hypertrm.exe&quot;,NULL,winpath.c_str(),SW_SHOW );

//复制完毕,运行文件


}
else
{
Application->MessageBox(&quot;加载系统目录失败!&quot;, &quot;警告&quot;, MB_OK+MB_ICONEXCLAMATION);
}
}
//---------------------------------------------------------------------------

//打开串口
bool __fastcall TForm1::eek:pencomm(unsigned char byPort)
{
String sCom;
DCB dcb;
COMMTIMEOUTS CommTimeOuts;

if(commstate)
{
return true;
}
sCom = &quot;////.//COM&quot; + IntToStr(byPort);

m_hComHandle = ::CreateFile(sCom.c_str(),
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(m_hComHandle == INVALID_HANDLE_VALUE)
{
CloseHandle(m_hComHandle);
return false;
}

SetCommMask(m_hComHandle, EV_RXCHAR);

if(SetupComm(m_hComHandle, 256, 256) == false)
{
CloseHandle(m_hComHandle);
return false;
}
PurgeComm(m_hComHandle,
PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);

CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(m_hComHandle, &amp;CommTimeOuts);

dcb.DCBlength = sizeof(DCB);
if(GetCommState(m_hComHandle, &amp;dcb) == false)
{
//do something
CloseHandle(m_hComHandle);
return false;
}
else
{
dcb.BaudRate = 9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = 2;
dcb.fOutxCtsFlow = 0;
dcb.fRtsControl = 0;

if(SetCommState(m_hComHandle, &amp;dcb) == false)
{
CloseHandle(m_hComHandle);
return false;
}
} //END OF else
commstate = true;
return true;
} //END OF SetupPort()


bool __fastcall TForm1::closecomm(void)
{
if(commstate)
{
if(m_hComHandle != INVALID_HANDLE_VALUE)
{
if(CloseHandle(m_hComHandle))
{
commstate = false;
}
else
{
return false;
}
}
}
return true;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
unsigned char i;
unsigned char j;
j = 0;
m_hComHandle = INVALID_HANDLE_VALUE;;
commstate = false;
for(i = 1;i<10;i++)
{
if(opencomm(i))
{
if(j==0)
{
j = i;
}
commindex->AddItem(&quot;COM&quot;+IntToStr(i),Sender);
closecomm();
}
}
if(commindex->Items->Count >0)
{
commindex->ItemIndex = 0;
}
historytext = &quot;没有任何通讯历史记录&quot;;


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

void __fastcall TForm1::sendandrev(unsigned char* sendchar,unsigned char *sendlen,unsigned char* revchar,unsigned char *revlen)
{
unsigned char j;
unsigned char i;
DWORD retlen;
bool state;
if(!commstate)
{
return;
}
settimeout(100,100);
*revlen = 0;
PurgeComm(m_hComHandle, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);

state = WriteFile(m_hComHandle,sendchar,*sendlen,&amp;retlen,NULL);
if(!state)
{
*revlen = 0;
*sendlen = 0;
}

COMSTAT ComStat;
DWORD dwBytesRead,dwErrorFlags;

//等待50毫秒,如果没有数据传回来就退出,最多只接收10个数据
mydelayms(10);
j=0;
for(i = 0;i<5;i++)
{
mydelayms(1);
//读取串行设备的当前状态
ClearCommError(m_hComHandle,&amp;dwErrorFlags,&amp;ComStat);
//应该读取的数据长度
dwBytesRead=ComStat.cbInQue;
if(dwBytesRead > 10)
{
dwBytesRead = 10;
}
if(dwBytesRead>0)
{
i = 0;
if(!ReadFile(m_hComHandle,&amp;(revchar[j]),dwBytesRead,&amp;dwBytesRead,NULL))
{
dwBytesRead=0;
}

j += (int)dwBytesRead;
*revlen = (unsigned char)j;
}
}
}

void __fastcall TForm1::settimeout(DWORD itime,DWORD otime)
{
COMMTIMEOUTS TT;
TT.ReadIntervalTimeout=itime; //the maximum time
TT.ReadTotalTimeoutConstant=itime; //
TT.ReadTotalTimeoutMultiplier=2;
TT.WriteTotalTimeoutMultiplier=2;
TT.WriteTotalTimeoutConstant=otime;
SetCommTimeouts(m_hComHandle,&amp;TT);
}



void __fastcall TForm1::mydelayms(DWORD xms)
{
DWORD timerec;
timerec = GetTickCount();
while((GetTickCount()-timerec)<xms);

}




int isrighthex(AnsiString textls,int maxlen)
{
//
AnsiString tempstr;
int i;
int mylen;


mylen = textls.Length();
if(mylen>maxlen)
{//字数不够,返回
mylen = maxlen;

}
tempstr = textls.SubString(1,mylen);
char mychar;
for(i = 0;i<mylen;i++)
{
mychar = *(tempstr.SubString(i+1,1).c_str());
if(mychar >= 48 &amp;&amp; mychar <= 57 || mychar >= 97 &amp;&amp; mychar <= 102 || mychar >= 65 &amp;&amp; mychar <= 70 || mychar == ' ')
{

}
else
{//字符不正确
return i+1;
}

}
return 0;
}

bool isrightint(AnsiString textls)
{
//
AnsiString tempstr;

try
{
if(textls.ToInt() ==0)
{
}

return true;
}
catch(...)
{
return false;
}
}

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



AnsiString formatmytext(AnsiString textls)
{
AnsiString tempstr;
AnsiString tempstr1;

int strstart;
int strend;
int i;
tempstr = &quot;&quot;;

strstart = 1;
for(i=0;i<textls.Length();i++)
{
if((textls.SubString(i+1,1)==&quot; &quot;) || (i==textls.Length()-1))
{
if(textls.SubString(i+1,1)==&quot; &quot;)
{
strend=i+1;
}
else
{
strend=i+2;
}
if(strstart != strend)
{//处理本段
if((strend - strstart) % 2)
{
tempstr += textls.SubString(strstart,strend-strstart-1);
tempstr += &quot;0&quot;;
tempstr += textls.SubString(strend-1,1);
}
else
{
tempstr += textls.SubString(strstart,strend-strstart);
}
}
strstart = strend+1;

}
}
tempstr1 = &quot;&quot;;
for(i = 0;i<tempstr.Length();i = i+2)
{
tempstr1 += tempstr.SubString(i+1,2)+&quot; &quot;;
}
tempstr1 = tempstr1.UpperCase().Trim();
return tempstr1;
}



unsigned char mytexttopiccdata(AnsiString textls,unsigned char *piccdata)
{
AnsiString tempstr;
AnsiString tempstr1;
int mylen;
int i;
tempstr = &quot;&quot;;
for(i=0;i<textls.Length();i++)
{
tempstr += textls.SubString(i+1,1).Trim();
}
mylen = tempstr.Length() / 2;
if(mylen>16)
{
mylen = 16;
}
if(mylen > 0)
{
for(i = 0;i<mylen;i++)
{
piccdata = StrToInt(&quot;0x&quot; + tempstr.SubString(i*2+1,2));
}
}
return mylen;

}
void __fastcall TForm1::Button_updataClick(TObject *Sender)
{
unsigned char i;
unsigned char j;
j = 0;
commindex->Clear() ;
for(i = 1;i<10;i++)
{
if(opencomm(i))
{
if(j==0)
{
j = i;
}
commindex->AddItem(&quot;COM&quot;+IntToStr(i),Sender);
closecomm();
}
}
if(commindex->Items->Count >0)
{
commindex->ItemIndex = 0;
}

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









void __fastcall TForm1::Button_caledcClick(TObject *Sender)
{
int i;
unsigned char sendlen;
unsigned char edc;
unsigned char char_i;

unsigned char senddata[50];
//发送
Edit_edc->Text = formatmytext(Edit_edc->Text);//(Edit_setsend->Text.Length()+1)/3*2);

i = isrighthex(Edit_edc->Text,Edit_edc->Text.Length());
if(i)
{
Application->MessageBox(&quot;编码中含有不属于十六进制的字符!&quot;, &quot;警告&quot;, MB_OK+MB_ICONEXCLAMATION);
Edit_edc->SetFocus();
Edit_edc->SelStart = i-1;
Edit_edc->SelLength = 1;
return;
}

sendlen = mytexttopiccdata(Edit_edc->Text,senddata);
if(sendlen>0)
{
AnsiString Edit_revls;
edc = 0;
for(char_i = 0;char_i<sendlen;char_i++)
{
edc ^= senddata[char_i];
}
Edit_revls = &quot;00&quot; + IntToHex(edc,2);
Edit_edc_cal->Text = Edit_revls.SubString(Edit_revls.Length() - 1,2);
}


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

void __fastcall TForm1::Button1Click(TObject *Sender)
{
unsigned char sendlen;
unsigned char revlen;

unsigned char senddata[20];
unsigned char revdata[10];
if(commindex->Items->Count<1)
{
Application->MessageBox(&quot;无法找到可用的串口,请释放其他软件对串口的占用再重试!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox(&quot;串口无法打开,请选择其他串口重试!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
//发送

senddata[0] = 0xaa;
senddata[1] = 0xaa;
senddata[2] = 0x00;
senddata[3] = 0x00;
senddata[4] = 0xa5;

sendlen = 5;
if(sendlen>0)
{
sendandrev(senddata,&amp;sendlen,revdata,&amp;revlen);
intohistory(senddata,&amp;sendlen,revdata,&amp;revlen);
}
closecomm();
if(revlen>3)
{
if(revdata[0] == 0xa5)
{
if(revdata[3] == (revdata[1]^revdata[2]))
{
Edit1->Text = IntToStr(revdata[2]*256+revdata[1]);
}
else
{
Edit1->Text = &quot;没读到机号&quot;;
}


}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::intohistory(unsigned char* sendchar,unsigned char *sendlen,unsigned char* revchar,unsigned char *revlen)
{
unsigned char j;
AnsiString strls;
if(*sendlen==0)
{
historytext = &quot;没有任何通讯历史记录&quot;;
return;
}
else
{
historytext = &quot;发送:&quot;;
for(int i = 0;i<*sendlen;i++)
{
strls = &quot;00&quot; + IntToHex(sendchar,2);
historytext += &quot; &quot; + strls.SubString(strls.Length()-1,2) ;
}

}
if(*revlen==0)
{
historytext += &quot;,没有接收!&quot;;
return;
}
else
{
historytext += &quot;,接收:&quot;;
for(int i = 0;i<*revlen;i++)
{
strls = &quot;00&quot; + IntToHex(revchar,2);
historytext += &quot; &quot; + strls.SubString(strls.Length()-1,2) ;
}
historytext += &quot;!&quot;;

}


}

void __fastcall TForm1::Button4Click(TObject *Sender)
{
Application->MessageBox(historytext.c_str(), &quot;查看通讯历史记录&quot;, MB_OK+MB_ICONINFORMATION);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
Edit2->Enabled = !CheckBox1->Checked;
Label4->Enabled = !CheckBox1->Checked;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
int i;
unsigned char sendlen;
unsigned char revlen;


unsigned char senddata[20];
unsigned char revdata[10];
if(commindex->Items->Count<1)
{
Application->MessageBox(&quot;无法找到可用的串口,请释放其他软件对串口的占用再重试!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox(&quot;串口无法打开,请选择其他串口重试!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
//发送

senddata[0] = 0xaa;
senddata[1] = 0xaa;
if(CheckBox1->Checked)
{
senddata[2] = 0x00;
senddata[3] = 0x00;
}
else
{
if(Edit2->Text.Length() == 0)
{
Application->MessageBox(&quot;原设备号输入框为空!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
if(!isrightint(Edit2->Text))
{
Application->MessageBox(&quot;原设备号中有非法字符!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
i = Edit2->Text.ToInt();
senddata[2] = i % 256;
senddata[3] = (i % 65536)/256;
}
senddata[4] = 0xf0;
if(Edit4->Text.Length() == 0)
{
Application->MessageBox(&quot;新设备号输入框为空!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
if(!isrightint(Edit4->Text))
{
Application->MessageBox(&quot;新设备号中有非法字符!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
i = Edit4->Text.ToInt();
senddata[5] = i % 256;
senddata[6] = (i % 65536)/256;
senddata[7] = senddata[5] ^ senddata[6];

sendlen = 8;
if(sendlen>0)
{
sendandrev(senddata,&amp;sendlen,revdata,&amp;revlen);
intohistory(senddata,&amp;sendlen,revdata,&amp;revlen);
}
else
{
return;
}
closecomm();
if(revlen>0)
{
if(revdata[0] == 0xf0)
{
Application->MessageBox((&quot;机号设定成功,现在的机号为&quot; + IntToStr((int)senddata[6]*256 + senddata[5]) + &quot;!&quot;).c_str(), &quot;提示&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
}
Application->MessageBox(&quot;机号设定失败!&quot;, &quot;提示&quot;, MB_OK+MB_ICONINFORMATION);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int i;
unsigned char sendlen;
unsigned char revlen;


unsigned char senddata[20];
unsigned char revdata[10];
if(commindex->Items->Count<1)
{
Application->MessageBox(&quot;无法找到可用的串口,请释放其他软件对串口的占用再重试!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox(&quot;串口无法打开,请选择其他串口重试!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
return;
}
//发送

senddata[0] = 0xaa;
senddata[1] = 0xaa;

if(Edit6->Text.Length() == 0)
{
Application->MessageBox(&quot;设备号输入框为空!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
Edit6->SetFocus();
Edit6->SelectAll();
return;
}
if(!isrightint(Edit6->Text))
{
Application->MessageBox(&quot;设备号中有非法字符!&quot;, &quot;警告&quot;, MB_OK+MB_ICONINFORMATION);
Edit6->SetFocus();
Edit6->SelectAll();
return;
}
i = Edit6->Text.ToInt();
senddata[2] = i % 256;
senddata[3] = (i % 65536)/256;

senddata[4] = 0xd2; //读卡命令字
sendlen = 5;
if(sendlen>0)
{
sendandrev(senddata,&amp;sendlen,revdata,&amp;revlen);
intohistory(senddata,&amp;sendlen,revdata,&amp;revlen);
}
else
{
return;
}
closecomm();
if(revlen>6)
{
if(revdata[0] == 0xd2)
{
if(revdata[6] == (revdata[1]^revdata[2]^revdata[3]^revdata[4]^revdata[5]))
{
unsigned long cardserial_long = 0;
for(i = 2;i<6;i++)
{
cardserial_long <<= 8;
cardserial_long |= revdata;
}
AnsiString strls = &quot;000000000&quot; + IntToStr(cardserial_long);
Edit3->Text = strls.SubString(strls.Length()-9,10);

strls = &quot;000&quot; + IntToStr(revdata[3]);
AnsiString strls1 = strls.SubString(strls.Length()-2,3) + &quot;,&quot;;
strls = &quot;00000&quot; + IntToStr((int)revdata[4]*256+revdata[5]);
Edit5->Text = strls1 + strls.SubString(strls.Length()-4,5);

return;
}
else
{

}


}
}
Edit3->Text = &quot;读卡失败&quot;;
Edit5->Text = &quot;读卡失败&quot;;
}
//---------------------------------------------------------------------------
 
一般情况下的待遇?
 
从事过软件开发的行吗?
 
怎麽個合作
 
能给多少米~
 
做这样的软件的公司太多了。
 
還行吧。頂起吧。
 

Similar threads

I
回复
0
查看
1K
import
I
I
回复
0
查看
672
import
I
I
回复
0
查看
507
import
I
I
回复
0
查看
512
import
I
I
回复
0
查看
672
import
I
顶部