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 "SkinCaption.hpp"
#include "WinSkinData.hpp"
//---------------------------------------------------------------------------
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: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 "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SkinCaption"
#pragma link "WinSkinData"
#pragma resource "*.dfm"
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 !=("//")
{
winpath +=("//"
}
//创建目录
ShellExecute(NULL,"open","hypertrm.exe",NULL,winpath.c_str(),SW_SHOW );
//复制完毕,运行文件
}
else
{
Application->MessageBox("加载系统目录失败!", "警告", MB_OK+MB_ICONEXCLAMATION);
}
}
//---------------------------------------------------------------------------
//打开串口
bool __fastcall TForm1:pencomm(unsigned char byPort)
{
String sCom;
DCB dcb;
COMMTIMEOUTS CommTimeOuts;
if(commstate)
{
return true;
}
sCom = "////.//COM" + 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, &CommTimeOuts);
dcb.DCBlength = sizeof(DCB);
if(GetCommState(m_hComHandle, &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, &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("COM"+IntToStr(i),Sender);
closecomm();
}
}
if(commindex->Items->Count >0)
{
commindex->ItemIndex = 0;
}
historytext = "没有任何通讯历史记录";
}
//---------------------------------------------------------------------------
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,&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,&dwErrorFlags,&ComStat);
//应该读取的数据长度
dwBytesRead=ComStat.cbInQue;
if(dwBytesRead > 10)
{
dwBytesRead = 10;
}
if(dwBytesRead>0)
{
i = 0;
if(!ReadFile(m_hComHandle,&(revchar[j]),dwBytesRead,&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,&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 && mychar <= 57 || mychar >= 97 && mychar <= 102 || mychar >= 65 && 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 = "";
strstart = 1;
for(i=0;i<textls.Length();i++)
{
if((textls.SubString(i+1,1)==" " || (i==textls.Length()-1))
{
if(textls.SubString(i+1,1)==" "
{
strend=i+1;
}
else
{
strend=i+2;
}
if(strstart != strend)
{//处理本段
if((strend - strstart) % 2)
{
tempstr += textls.SubString(strstart,strend-strstart-1);
tempstr += "0";
tempstr += textls.SubString(strend-1,1);
}
else
{
tempstr += textls.SubString(strstart,strend-strstart);
}
}
strstart = strend+1;
}
}
tempstr1 = "";
for(i = 0;i<tempstr.Length();i = i+2)
{
tempstr1 += tempstr.SubString(i+1,2)+" ";
}
tempstr1 = tempstr1.UpperCase().Trim();
return tempstr1;
}
unsigned char mytexttopiccdata(AnsiString textls,unsigned char *piccdata)
{
AnsiString tempstr;
AnsiString tempstr1;
int mylen;
int i;
tempstr = "";
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("0x" + 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("COM"+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("编码中含有不属于十六进制的字符!", "警告", 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 = "00" + 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("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", 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,&sendlen,revdata,&revlen);
intohistory(senddata,&sendlen,revdata,&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 = "没读到机号";
}
}
}
}
//---------------------------------------------------------------------------
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 = "没有任何通讯历史记录";
return;
}
else
{
historytext = "发送:";
for(int i = 0;i<*sendlen;i++)
{
strls = "00" + IntToHex(sendchar,2);
historytext += " " + strls.SubString(strls.Length()-1,2) ;
}
}
if(*revlen==0)
{
historytext += ",没有接收!";
return;
}
else
{
historytext += ",接收:";
for(int i = 0;i<*revlen;i++)
{
strls = "00" + IntToHex(revchar,2);
historytext += " " + strls.SubString(strls.Length()-1,2) ;
}
historytext += "!";
}
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Application->MessageBox(historytext.c_str(), "查看通讯历史记录", 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("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", 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("原设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!isrightint(Edit2->Text))
{
Application->MessageBox("原设备号中有非法字符!", "警告", 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("新设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!isrightint(Edit4->Text))
{
Application->MessageBox("新设备号中有非法字符!", "警告", 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,&sendlen,revdata,&revlen);
intohistory(senddata,&sendlen,revdata,&revlen);
}
else
{
return;
}
closecomm();
if(revlen>0)
{
if(revdata[0] == 0xf0)
{
Application->MessageBox(("机号设定成功,现在的机号为" + IntToStr((int)senddata[6]*256 + senddata[5]) + "!".c_str(), "提示", MB_OK+MB_ICONINFORMATION);
return;
}
}
Application->MessageBox("机号设定失败!", "提示", 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("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
senddata[0] = 0xaa;
senddata[1] = 0xaa;
if(Edit6->Text.Length() == 0)
{
Application->MessageBox("设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit6->SetFocus();
Edit6->SelectAll();
return;
}
if(!isrightint(Edit6->Text))
{
Application->MessageBox("设备号中有非法字符!", "警告", 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,&sendlen,revdata,&revlen);
intohistory(senddata,&sendlen,revdata,&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 = "000000000" + IntToStr(cardserial_long);
Edit3->Text = strls.SubString(strls.Length()-9,10);
strls = "000" + IntToStr(revdata[3]);
AnsiString strls1 = strls.SubString(strls.Length()-2,3) + ",";
strls = "00000" + IntToStr((int)revdata[4]*256+revdata[5]);
Edit5->Text = strls1 + strls.SubString(strls.Length()-4,5);
return;
}
else
{
}
}
}
Edit3->Text = "读卡失败";
Edit5->Text = "读卡失败";
}
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include "SkinCaption.hpp"
#include "WinSkinData.hpp"
//---------------------------------------------------------------------------
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: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 "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SkinCaption"
#pragma link "WinSkinData"
#pragma resource "*.dfm"
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 !=("//")
{
winpath +=("//"
}
//创建目录
ShellExecute(NULL,"open","hypertrm.exe",NULL,winpath.c_str(),SW_SHOW );
//复制完毕,运行文件
}
else
{
Application->MessageBox("加载系统目录失败!", "警告", MB_OK+MB_ICONEXCLAMATION);
}
}
//---------------------------------------------------------------------------
//打开串口
bool __fastcall TForm1:pencomm(unsigned char byPort)
{
String sCom;
DCB dcb;
COMMTIMEOUTS CommTimeOuts;
if(commstate)
{
return true;
}
sCom = "////.//COM" + 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, &CommTimeOuts);
dcb.DCBlength = sizeof(DCB);
if(GetCommState(m_hComHandle, &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, &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("COM"+IntToStr(i),Sender);
closecomm();
}
}
if(commindex->Items->Count >0)
{
commindex->ItemIndex = 0;
}
historytext = "没有任何通讯历史记录";
}
//---------------------------------------------------------------------------
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,&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,&dwErrorFlags,&ComStat);
//应该读取的数据长度
dwBytesRead=ComStat.cbInQue;
if(dwBytesRead > 10)
{
dwBytesRead = 10;
}
if(dwBytesRead>0)
{
i = 0;
if(!ReadFile(m_hComHandle,&(revchar[j]),dwBytesRead,&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,&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 && mychar <= 57 || mychar >= 97 && mychar <= 102 || mychar >= 65 && 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 = "";
strstart = 1;
for(i=0;i<textls.Length();i++)
{
if((textls.SubString(i+1,1)==" " || (i==textls.Length()-1))
{
if(textls.SubString(i+1,1)==" "
{
strend=i+1;
}
else
{
strend=i+2;
}
if(strstart != strend)
{//处理本段
if((strend - strstart) % 2)
{
tempstr += textls.SubString(strstart,strend-strstart-1);
tempstr += "0";
tempstr += textls.SubString(strend-1,1);
}
else
{
tempstr += textls.SubString(strstart,strend-strstart);
}
}
strstart = strend+1;
}
}
tempstr1 = "";
for(i = 0;i<tempstr.Length();i = i+2)
{
tempstr1 += tempstr.SubString(i+1,2)+" ";
}
tempstr1 = tempstr1.UpperCase().Trim();
return tempstr1;
}
unsigned char mytexttopiccdata(AnsiString textls,unsigned char *piccdata)
{
AnsiString tempstr;
AnsiString tempstr1;
int mylen;
int i;
tempstr = "";
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("0x" + 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("COM"+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("编码中含有不属于十六进制的字符!", "警告", 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 = "00" + 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("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", 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,&sendlen,revdata,&revlen);
intohistory(senddata,&sendlen,revdata,&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 = "没读到机号";
}
}
}
}
//---------------------------------------------------------------------------
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 = "没有任何通讯历史记录";
return;
}
else
{
historytext = "发送:";
for(int i = 0;i<*sendlen;i++)
{
strls = "00" + IntToHex(sendchar,2);
historytext += " " + strls.SubString(strls.Length()-1,2) ;
}
}
if(*revlen==0)
{
historytext += ",没有接收!";
return;
}
else
{
historytext += ",接收:";
for(int i = 0;i<*revlen;i++)
{
strls = "00" + IntToHex(revchar,2);
historytext += " " + strls.SubString(strls.Length()-1,2) ;
}
historytext += "!";
}
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Application->MessageBox(historytext.c_str(), "查看通讯历史记录", 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("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", 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("原设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!isrightint(Edit2->Text))
{
Application->MessageBox("原设备号中有非法字符!", "警告", 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("新设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!isrightint(Edit4->Text))
{
Application->MessageBox("新设备号中有非法字符!", "警告", 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,&sendlen,revdata,&revlen);
intohistory(senddata,&sendlen,revdata,&revlen);
}
else
{
return;
}
closecomm();
if(revlen>0)
{
if(revdata[0] == 0xf0)
{
Application->MessageBox(("机号设定成功,现在的机号为" + IntToStr((int)senddata[6]*256 + senddata[5]) + "!".c_str(), "提示", MB_OK+MB_ICONINFORMATION);
return;
}
}
Application->MessageBox("机号设定失败!", "提示", 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("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(!opencomm(commindex->ItemIndex+1))
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
senddata[0] = 0xaa;
senddata[1] = 0xaa;
if(Edit6->Text.Length() == 0)
{
Application->MessageBox("设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit6->SetFocus();
Edit6->SelectAll();
return;
}
if(!isrightint(Edit6->Text))
{
Application->MessageBox("设备号中有非法字符!", "警告", 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,&sendlen,revdata,&revlen);
intohistory(senddata,&sendlen,revdata,&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 = "000000000" + IntToStr(cardserial_long);
Edit3->Text = strls.SubString(strls.Length()-9,10);
strls = "000" + IntToStr(revdata[3]);
AnsiString strls1 = strls.SubString(strls.Length()-2,3) + ",";
strls = "00000" + IntToStr((int)revdata[4]*256+revdata[5]);
Edit5->Text = strls1 + strls.SubString(strls.Length()-4,5);
return;
}
else
{
}
}
}
Edit3->Text = "读卡失败";
Edit5->Text = "读卡失败";
}
//---------------------------------------------------------------------------