代码如下:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool __fastcall isrightint(AnsiString textls);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button_updataClick(TObject *Sender)
{
int j;
int i;
j = 0;
commindex->Clear() ;
HANDLE (__stdcall *PortOpen)(int comport);
bool (__stdcall *PortClose)(HANDLE ComportHandle);
//Judges the DLL(Dynamic Link Library) whether exists
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "sycomdll.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在当前目录下找到sycomdll.dll文件!");
return;
}
//to get function pointers
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
PortOpen = (HANDLE (__stdcall *)(int comport))GetProcAddress(hDll,"PortOpen");
PortClose = (bool (__stdcall *)(HANDLE ComportHandle))GetProcAddress(hDll,"PortClose");
for(i = 1;i<10;i++)
{
Comport_Handle = PortOpen(i);
if(INVALID_HANDLE_VALUE != Comport_Handle)
{
if(j==0)
{
j = i;
}
commindex->AddItem("COM"+IntToStr(i),Sender);
PortClose(Comport_Handle);
}
}
if(commindex->Items->Count >0)
{
commindex->ItemIndex = 0;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Button_updata->Click();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char lll[100];
char *jhls;
HANDLE (__stdcall *PortOpen)(int comport);
bool (__stdcall *PortClose)(HANDLE ComportHandle);
void (__stdcall *Delay_ms)(DWORD xms);
int (__stdcall *GetDeviceNo)(HANDLE ComportHandle,LPSTR JihaoString);
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "sycomdll.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在当前目录下找到sycomdll.dll文件!");
return;
}
//to get function pointers
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
PortOpen = (HANDLE (__stdcall *)(int comport))GetProcAddress(hDll,"PortOpen");
PortClose = (bool (__stdcall *)(HANDLE ComportHandle))GetProcAddress(hDll,"PortClose");
Delay_ms = (void (__stdcall *)(DWORD xms))GetProcAddress(hDll,"Delay_ms");
GetDeviceNo = (int (__stdcall *)(HANDLE ComportHandle,LPSTR JihaoString))GetProcAddress(hDll,"GetDeviceNo");
if(commindex->Items->Count<1)
{
Application->MessageBox("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
Comport_Handle = PortOpen(commindex->ItemIndex+1);
if(Comport_Handle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("串口无法打开,请选择其他串口重试!","警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
Delay_ms(8);//如果是使用无源485网卡,这个延时一定要加
jhls = lll;
status = GetDeviceNo(Comport_Handle,jhls); //读设备号
PortClose(Comport_Handle);
if(status ==0)
{ //成功
Edit1->Text = jhls;
Edit2->Text = Edit1->Text;
Edit6->Text = Edit1->Text;
Edit7->Text = Edit1->Text;
Edit16->Text = Edit1->Text;
Edit17->Text = Edit1->Text;
Edit18->Text = Edit1->Text;
Edit19->Text = Edit1->Text;
Edit22->Text = Edit1->Text;
edt_5->Text = Edit1->Text;
}
else
{
Edit1->Text = "没读到机号";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
HANDLE (__stdcall *PortOpen)(int comport);
bool (__stdcall *PortClose)(HANDLE ComportHandle);
void (__stdcall *Delay_ms)(DWORD xms);
int (__stdcall *SetDeviceNo)(HANDLE ComportHandle,int Jihao_old,int Jihao_new);
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "sycomdll.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在当前目录下找到sycomdll.dll文件!");
return;
}
//to get function pointers
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
PortOpen = (HANDLE (__stdcall *)(int comport))GetProcAddress(hDll,"PortOpen");
PortClose = (bool (__stdcall *)(HANDLE ComportHandle))GetProcAddress(hDll,"PortClose");
Delay_ms = (void (__stdcall *)(DWORD xms))GetProcAddress(hDll,"Delay_ms");
SetDeviceNo = (int (__stdcall *)(HANDLE ComportHandle,int Jihao_old,int Jihao_new))GetProcAddress(hDll,"SetDeviceNo");
if(Edit2->Text.Length() == 0)
{
Application->MessageBox("原设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit2->SelectAll();
return;
}
if(!isrightint(Edit2->Text))
{
Application->MessageBox("原设备号中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit2->SetFocus();
Edit2->SelectAll();
return;
};
if(Edit4->Text.Length() == 0)
{
Application->MessageBox("新设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit4->SelectAll();
return;
}
if(!isrightint(Edit4->Text))
{
Application->MessageBox("新设备号中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit4->SetFocus();
Edit4->SelectAll();
return;
}
if(StrToInt(Trim(Edit4->Text))>65535)
{
Application->MessageBox("新设备号中机号不能大于65535!", "警告", MB_OK+MB_ICONINFORMATION);
Edit4->SetFocus();
Edit4->SelectAll();
return;
}
if(commindex->Items->Count<1)
{
Application->MessageBox("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
Comport_Handle = PortOpen(commindex->ItemIndex+1);
if(Comport_Handle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
Delay_ms(8);//如果是使用无源485网卡,这个延时一定要加
status = SetDeviceNo(Comport_Handle,StrToInt(Edit2->Text),StrToInt(Edit4->Text)); //读设备号
PortClose(Comport_Handle);
if(status ==0)
{ //成功
AnsiString strls = "机号设定成功,现在的机号为" + Edit4->Text + "!";
Application->MessageBox(strls.c_str(), "提示", MB_OK+MB_ICONINFORMATION);
}
else
{
Application->MessageBox("机号设定失败!", "提示", MB_OK+MB_ICONINFORMATION);
}
}
//---------------------------------------------------------------------------
bool __fastcall isrightint(AnsiString textls)
{
try
{
if(StrToInt(textls)==0)
{
}
return true;
}
catch(...)
{
return false;
}
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
HANDLE (__stdcall *PortOpen)(int comport);
bool (__stdcall *PortClose)(HANDLE ComportHandle);
void (__stdcall *Delay_ms)(DWORD xms);
int (__stdcall *SetDevicePWD)(HANDLE ComportHandle,int Jihao,LPSTR PWDString);
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "sycomdll.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在当前目录下找到sycomdll.dll文件!");
return;
}
//to get function pointers
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
PortOpen = (HANDLE (__stdcall *)(int comport))GetProcAddress(hDll,"PortOpen");
PortClose = (bool (__stdcall *)(HANDLE ComportHandle))GetProcAddress(hDll,"PortClose");
Delay_ms = (void (__stdcall *)(DWORD xms))GetProcAddress(hDll,"Delay_ms");
SetDevicePWD = (int (__stdcall *)(HANDLE ComportHandle,int Jihao,LPSTR PWDString))GetProcAddress(hDll,"SetDevicePWD");
if(Edit7->Text.Length() == 0)
{
Application->MessageBox("设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit7->SelectAll();
return;
}
if(!isrightint(Edit7->Text))
{
Application->MessageBox("原设备号中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit7->SetFocus();
Edit7->SelectAll();
return;
}
if(Edit9->Text.Length() == 0)
{
Application->MessageBox("密码输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit9->SetFocus();
return;
}
if(!isrightint(Edit9->Text))
{
Application->MessageBox("密码输入框中有法字符,密码只能为0~9数字!", "警告", MB_OK+MB_ICONINFORMATION);
Edit9->SetFocus();
Edit9->SelectAll();
return;
}
if(Trim(Edit9->Text).Length() !=6)
{
Application->MessageBox("密码只能为6位!", "警告", MB_OK+MB_ICONINFORMATION);
Edit9->SetFocus();
Edit9->SelectAll();
return;
}
if(commindex->Items->Count<1)
{
Application->MessageBox("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
Comport_Handle = PortOpen(commindex->ItemIndex+1);
if(Comport_Handle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
Delay_ms(8);//如果是使用无源485网卡,这个延时一定要加
status = SetDevicePWD(Comport_Handle,StrToInt(Edit7->Text),Edit9->Text.c_str()); //读设备号
PortClose(Comport_Handle);
if(status ==0)
{ //成功
AnsiString strls = "机号["+Edit7->Text+"]管理员密码更改成功!";
Application->MessageBox(strls.c_str(),"提示", MB_OK+MB_ICONINFORMATION);
return;
}
else
{
Application->MessageBox(("管理员密码更改失败!错误号:"+IntToStr(status)).c_str(), "提示", MB_OK+MB_ICONINFORMATION);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
AnsiString strls;
int status;
char lll[100];
char *jhls;
int jihao;
int beepforxms;
HANDLE (__stdcall *PortOpen)(int comport);
bool (__stdcall *PortClose)(HANDLE ComportHandle);
void (__stdcall *Delay_ms)(DWORD xms);
int (__stdcall *SetBeep)(HANDLE ComportHandle,int Jihao,int BeepXms);
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "sycomdll.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在当前目录下找到sycomdll.dll文件!");
return;
}
//to get function pointers
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
PortOpen = (HANDLE (__stdcall *)(int comport))GetProcAddress(hDll,"PortOpen");
PortClose = (bool (__stdcall *)(HANDLE ComportHandle))GetProcAddress(hDll,"PortClose");
Delay_ms = (void (__stdcall *)(DWORD xms))GetProcAddress(hDll,"Delay_ms");
SetBeep = (int (__stdcall *)(HANDLE ComportHandle,int Jihao,int BeepXms))GetProcAddress(hDll,"SetBeep");
if(commindex->Items->Count<1)
{
Application->MessageBox("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(CheckBox1->Checked)
{
jihao = 0;
}
else
{
if(Edit16->Text.Length() == 0)
{
Application->MessageBox("设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit16->SetFocus();
return;
}
if(!isrightint(Edit16->Text))
{
Application->MessageBox("设备号输入框中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit16->SetFocus();
Edit16->SelectAll();
return;
}
jihao = StrToInt(Edit16->Text);
}
if(Edit12->Text.Length() == 0)
{
Application->MessageBox("声长输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit12->SetFocus();
return;
}
if(!isrightint(Edit12->Text))
{
Application->MessageBox("声长输入框中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit12->SetFocus();
Edit12->SelectAll();
return;
}
Comport_Handle = PortOpen(commindex->ItemIndex+1);
if(Comport_Handle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
Delay_ms(8);//如果是使用无源485网卡,这个延时一定要加
beepforxms = StrToInt(Edit12->Text);
SetBeep(Comport_Handle,jihao,beepforxms);
PortClose(Comport_Handle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
AnsiString strls;
int status;
char lll[100];
char *jhls;
int jihao;
int beepforxms;
int unbeepforxms;
int times;
HANDLE (__stdcall *PortOpen)(int comport);
bool (__stdcall *PortClose)(HANDLE ComportHandle);
void (__stdcall *Delay_ms)(DWORD xms);
int (__stdcall *SetBeepEx)(HANDLE ComportHandle,int Jihao,int beeptimes,int beepinterval,int loopcount);
AnsiString FileName=ExtractFilePath(Application->ExeName);
if(FileName.SubString(FileName.Length(),1) != "\\")
{
FileName += "\\";
}
FileName += "sycomdll.dll";
if(!FileExists(FileName))
{
ShowMessage("无法在当前目录下找到sycomdll.dll文件!");
return;
}
//to get function pointers
HINSTANCE hDll;
hDll=LoadLibrary(FileName.c_str());
PortOpen = (HANDLE (__stdcall *)(int comport))GetProcAddress(hDll,"PortOpen");
PortClose = (bool (__stdcall *)(HANDLE ComportHandle))GetProcAddress(hDll,"PortClose");
Delay_ms = (void (__stdcall *)(DWORD xms))GetProcAddress(hDll,"Delay_ms");
SetBeepEx = (int (__stdcall *)(HANDLE ComportHandle,int Jihao,int beeptimes,int beepinterval,int loopcount))GetProcAddress(hDll,"SetBeepEx");
if(commindex->Items->Count<1)
{
Application->MessageBox("无法找到可用的串口,请释放其他软件对串口的占用再重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
if(CheckBox2->Checked)
{
jihao = 0;
}
else
{
if(Edit17->Text.Length() == 0)
{
Application->MessageBox("设备号输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit17->SetFocus();
return;
}
if(!isrightint(Edit17->Text))
{
Application->MessageBox("设备号输入框中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit17->SetFocus();
Edit17->SelectAll();
return;
}
jihao = StrToInt(Edit17->Text);
}
if(Edit13->Text.Length() == 0)
{
Application->MessageBox("声长输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit13->SetFocus();
return;
}
if(!isrightint(Edit13->Text))
{
Application->MessageBox("声长输入框中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit13->SetFocus();
Edit13->SelectAll();
return;
}
beepforxms = StrToInt(Edit13->Text);
if((Edit14->Text).Length() == 0)
{
Application->MessageBox("停顿输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit14->SetFocus();
return;
}
if(!isrightint(Edit14->Text))
{
Application->MessageBox("停顿输入框中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit14->SetFocus();
Edit14->SelectAll();
return;
}
unbeepforxms = StrToInt(Edit14->Text);
//次数
if((Edit15->Text).Length() == 0)
{
Application->MessageBox("循环次数输入框为空!", "警告", MB_OK+MB_ICONINFORMATION);
Edit15->SetFocus();
return;
}
if(!isrightint(Edit15->Text))
{
Application->MessageBox("循环次数输入框中有非法字符!", "警告", MB_OK+MB_ICONINFORMATION);
Edit15->SetFocus();
Edit15->SelectAll();
return;
}
times = StrToInt(Edit15->Text);
Comport_Handle = PortOpen(commindex->ItemIndex+1);
if(Comport_Handle == INVALID_HANDLE_VALUE)
{
Application->MessageBox("串口无法打开,请选择其他串口重试!", "警告", MB_OK+MB_ICONINFORMATION);
return;
}
//发送
Delay_ms(8);//如果是使用无源485网卡,这个延时一定要加
SetBeepEx(Comport_Handle,jihao,beepforxms,unbeepforxms,times);
PortClose(Comport_Handle);
}