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

📄 unit1.~pas

📁 求是科技出版的《Delphi串口通信工程开发实例导航》所有的源代码。是一本很好的书。拿出来与大家共享。
💻 ~PAS
📖 第 1 页 / 共 3 页
字号:
     begin
        with pnlLight do
        begin
           BevelInner := bvRaised;
           Color:=bkUpRgb;
        end;
     end;

     //如果是"开雨刷"按钮
     if (index=35) then
     begin
        with pnlWaterBrush do
        begin
           BevelInner := bvRaised;
           Color:=bkUpRgb;
        end;
     end;
end;

//*******************************************
//功能:调用ConfIOForm窗台,便于用户改变并口和串口的配置
//      如果用户点击“确定”将更新配置
//      如果用户点击“取消”将不作处理 
//*******************************************
procedure TMainForm.btConfIOClick(Sender: TObject);
var
    ConfIOForm:TConfIOForm;
begin
    //动态生成该窗体
    ConfIOForm:=TConfIOForm.Create(Application);
    try
        //如果用户是点击“确定”按钮,将改变设置
        if ConfIOForm.ShowModal =mrOk then
        begin
            bSerial:=ConfIOForm.rbSerial.Checked;
            IoPort:=StrToInt(ConfIOForm.edtIO.Text);
            conSerialPort.PortNr:=ConfIOForm.cbCom.Text;
            conSerialPort.BaudRate:=ConfIOForm.cbBaudRate.Text;
            conSerialPort.DataBits:=ConfIOForm.cbDatabits.Text;
            conSerialPort.Parity:=ConfIOForm.cbParity.Text;
            conSerialPort.StopBits:=ConfIOForm.cbStopBits.Text;
            if bSerial then
                StatusBar1.SimpleText:='串口'+conSerialPort.PortNr+'打开'
            else
                StatusBar1.SimpleText:='并口'+IntToStr(IoPort)+'打开';            
        end;
    finally
        //释放该窗体
        ConfIOForm.Free;
        ConfIOForm:=nil;
    end;
end;

//*****************************************
//功能:云台程序主窗体释放时的处理过程
//      释放动态生成云台按钮对象,保存当前程序的配置
//*****************************************
procedure TMainForm.FormDestroy(Sender: TObject);
var
    i:Integer;
begin
    //保存当前的配置
    SaveConf;

    //释放32个动态生成的按钮
    for i:=1 to 32 do
        Panel[i].Free;
    //关闭WinIo
    ShutdownWinIo;            
end;

//*****************************************
//功能:确定是第一次运行本程序以及相关文件是否存在
//*****************************************
procedure TMainForm.FileTest;
var
    FSearchRec:TSearchRec;
    FindError:Integer;
    tmpStr:String;
begin
    tmpStr:=AppPath+'winio.sys';
    FindError:=FindFirst(tmpStr,faAnyFile,FSearchRec);
    if FindError<>0 then
    begin
        ShowMessage('找不到文件WinIo.sys,请确定与本应用程序在同一目录下!');
        Application.Terminate;
    end;
    tmpStr:=AppPath+'winio.dll';
    FindError:=FindFirst(tmpStr,faAnyFile,FSearchRec);
    if FindError<>0 then
    begin
        ShowMessage('找不到文件WinIo.dll,请确定与本应用程序在同一目录下!');
        Application.Terminate;
    end;
    tmpStr:=AppPath+'winio.vxd';
    FindError:=FindFirst(tmpStr,faAnyFile,FSearchRec);
    if FindError<>0 then
    begin
        ShowMessage('找不到文件WinIo.vxd,请确定与本应用程序在同一目录下!');
        Application.Terminate;
    end;
end;

//*****************************************
//功能:
//      从注册表读入信息判断WinIO已经是否已经安装
//      如果未安装,则安装WinIO
//      如果已安装,则初始化WinIO
//      如果安装或是初始化失败,使用串口发送指令
//*****************************************
procedure TMainForm.WinIoInit;
var
    RegF:TRegistry;
    st,WinioPath,AppPath,MsgStr:string;
    isInit,isInstalled:Boolean;

begin
    RegF:=TRegistry.Create;
    try
        //读注册表的键值
        RegF.RootKey :=HKEY_LOCAL_MACHINE;
        RegF.OpenKey('SOFTWARE\Yutai\setting',TRUE);
        st := RegF.ReadString('IsInstalled');
        WinioPath := RegF.ReadString('WinioPath');
        RegF.CloseKey;
        if st<>'1' then
        begin
             //安装WinIO        
            isInstalled:=InstallWinIoDriver((AppPath+'winio.sys'),False);
            if isInstalled then
            begin
                //安装WinIO成功后写入注册表的键值            
                RegF.OpenKey('SOFTWARE\Yutai\setting',TRUE);
                RegF.WriteString('IsInstalled','1');
                RegF.WriteString('WinioPath',AppPath);
                RegF.CloseKey;
                //WinIo库安装成功,需要系统重新启动
                showmessage('WinIo库安装成功,需要系统重新启动');
                SystemReboot;
            end
            else
            begin
                //WinIo库安装失败,删去WinIo
                bSerial:=True;                
                RemoveWinIoDriver;
                MsgStr:='WinIo库安装失败,请确认是否有管理员权限,详细问题请与管理员联系';
                MsgStr:=MsgStr+chr(13)+'警告:WinIo库安装失败的情况下只可以使用串口通讯,并口无法使用!';
                showmessage(MsgStr);
            end;
        end
        else
        begin
             //初始化WinIo   
             isInit:=InitializeWinIo();
             if not isInit then
             begin
                 showmessage('WinIo初试话失败');
                 bSerial:=True;
             end;    
        end;     
    finally
        RegF.Free;
    end;
end;

//*****************************************
//功能:本程序时从程序目录下载入配置文件YuntaiConfig.ini
//      如果该文件不存在就载入默认的配置
//*****************************************
procedure TMainForm.InitConf;
var
    iniPath,tmpStr:String;
    iniFile:TIniFile;
    i:Integer;
begin
    AppPath:=ExtractFilePath(Application.ExeName);              //获取程序所在目录
    iniPath:=AppPath+iniFileName;                               //指定配置文件
    //打开配置文件,载入配置。
    //如果该文件不存在,就会生成该文件,并载入默认的配置。
    iniFile:=TIniFile.Create(iniPath);
    with iniFile do
    begin
        //并串口配置
        bSerial:=ReadBool('IO','bSerial',True);
        conSerialPort.PortNr:=ReadString('IO','PortNr','1');
        conSerialPort.BaudRate:=ReadString('IO','BaudRate','9600');
        conSerialPort.DataBits:=ReadString('IO','DataBits','8');
        conSerialPort.StopBits:=ReadString('IO','StopBits','1');
        conSerialPort.Parity:=ReadString('IO','Parity','E');
        IoPort:=ReadInteger('IO','IoPort',956);
        //镜头控制指令集
        ByteCodeMir[0]:=ReadInteger('Mir','ByteCodeMir0',($80+0));
        ByteCodeMir[1]:=ReadInteger('Mir','ByteCodeMir1',($80+1));
        ByteCodeMir[2]:=ReadInteger('Mir','ByteCodeMir2',($80+2));
        ByteCodeMir[3]:=ReadInteger('Mir','ByteCodeMir3',($80+3));
        ByteCodeMir[4]:=ReadInteger('Mir','ByteCodeMir4',($80+4));
        ByteCodeMir[5]:=ReadInteger('Mir','ByteCodeMir5',($80+5));
        //状态控制指令集
        ByteCodeStatus[0]:=ReadInteger('Status','ByteCodeStatus0',($c0+2*0));
        ByteCodeStatus[1]:=ReadInteger('Status','ByteCodeStatus1',($c0+2*1));
        ByteCodeStatus[2]:=ReadInteger('Status','ByteCodeStatus2',($c0+2*2));
        //动作控制指令集
        ByteCodeAct[0]:=ReadInteger('Act','ByteCodeAct0',($40+0));
        ByteCodeAct[1]:=ReadInteger('Act','ByteCodeAct1',($40+1));
        ByteCodeAct[2]:=ReadInteger('Act','ByteCodeAct2',($40+2));
        ByteCodeAct[3]:=ReadInteger('Act','ByteCodeAct3',($40+3));
        //reset
        ByteCodeAct[4]:=ReadInteger('Act','Reset',0);
        //所有云台的状态集合
        for i:=1 to 32 do
        begin
             tmpStr:='ByteNumChoose'+IntToStr(i);
             ByteCodeChoose[i]:=ReadInteger('Code',tmpStr,i);
        end;
    end;
    //释放配置文件
    IniFile.Free;
end;

//*****************************************
//功能:保存程序当前配置到配置文件YuntaiConfig.ini
//*****************************************
procedure TMainForm.SaveConf;
var
    iniPath,tmpStr:String;
    iniFile:TIniFile;
    i:Integer;
begin
    iniPath:=AppPath+iniFileName;
    iniFile:=TIniFile.Create(iniPath);                          //指定配置文件

    //保存程序当前配置到配置文件
    with iniFile do
    begin
        //并串口配置
        WriteBool('IO','bSerial',bSerial);
        WriteString('IO','PortNr',conSerialPort.PortNr);
        WriteString('IO','BaudRate',conSerialPort.BaudRate);
        WriteString('IO','DataBits',conSerialPort.DataBits);
        WriteString('IO','StopBits',conSerialPort.StopBits);
        WriteString('IO','Parity',conSerialPort.Parity);
        WriteInteger('IO','IoPort',IoPort);
        //镜头控制指令集
        WriteInteger('Mir','ByteCodeMir0',ByteCodeMir[0]);
        WriteInteger('Mir','ByteCodeMir1',ByteCodeMir[1]);
        WriteInteger('Mir','ByteCodeMir2',ByteCodeMir[2]);
        WriteInteger('Mir','ByteCodeMir3',ByteCodeMir[3]);
        WriteInteger('Mir','ByteCodeMir4',ByteCodeMir[4]);
        WriteInteger('Mir','ByteCodeMir5',ByteCodeMir[5]);
        //状态控制指令集
        WriteInteger('Status','ByteCodeStatus0',ByteCodeStatus[0]);
        WriteInteger('Status','ByteCodeStatus1',ByteCodeStatus[1]);
        WriteInteger('Status','ByteCodeStatus2',ByteCodeStatus[2]);
        //动作控制指令集
        WriteInteger('Act','ByteCodeAct0',ByteCodeAct[0]);
        WriteInteger('Act','ByteCodeAct1',ByteCodeAct[1]);
        WriteInteger('Act','ByteCodeAct2',ByteCodeAct[2]);
        WriteInteger('Act','ByteCodeAct3',ByteCodeAct[3]);
        //reset
        WriteInteger('Act','Reset',ByteCodeAct[4]);
        //所有云台的状态集合
        for i:=1 to 32 do
        begin
             tmpStr:='ByteCodeChoose'+IntToStr(i);
             WriteInteger('Code',tmpStr,ByteCodeChoose[i]);
        end;
    end;
    //释放配置文件    
    IniFile.Free;    
end;

//*****************************************
//功能:云台程序主窗体创建时的处理过程
//      载入程序配置,配置串口和并口,动态生成云台按钮
//*****************************************
procedure TMainForm.FormCreate(Sender: TObject);
var
    i:Integer;
begin
        //载入原有的程序配置
        InitConf;

        //预先配置串口
        SetSerialPort;

        //检测WinIo的三个文件是否在该程序的相同目录下
        FileTest;

        //如果WinIO初试化失败,bSerial:=True;故在InitConf函数之后
        WinIoInit;
        
        //在状态栏显示当前底层使用的是并口或是串口
        if bSerial then
            StatusBar1.SimpleText:='串口'+conSerialPort.PortNr+'打开'
        else
            StatusBar1.SimpleText:='并口'+IntToStr(IoPort)+'打开';

        //生成32个云台选择按钮
        DrawChooseButtom;
        
        //缺省地选中云台1
        pnlAutoActClick(panel[1]);
end;

//*****************************************
//功能:根据记录conSerialPort设置串口的配置
//      如果配置不成功,使用默认的配置
//注意:串口始终是使用文本格式发送数据      
//*****************************************
procedure  TMainForm.SetSerialPort;
begin
     try
        begin
           with conSerialPort do
           begin
              MSComm1.CommPort:=StrToInt(PortNr);
              MSComm1.InputMode:=comInputModeText;
              MSComm1.Settings:=BaudRate+','+Parity+','+DataBits+','+StopBits;
              MSComm1.PortOpen :=True;
           end;
        end;
     except
        begin
           showmessage('设置串口失败,使用缺省设置');
           MSComm1.CommPort:=1;
           MSComm1.InputMode:=comInputModeText;
           MSComm1.Settings:='9600,n,8,1';
           MSComm1.PortOpen :=True;
        end;
     end;   
end;

//*******************************************
//功能:响应对云台选择按钮和状态按钮的单击响应
//参数:tmpNo:被点击的按钮的tag属性值,用于确定按钮
//*******************************************
procedure TMainForm.pnlAutoActClick(Sender: TObject);
var
    tmpNo,i:Integer;
    Code:Byte;

begin
     {如果不是云台的TPaner按钮,则不处理}
     if  ( Sender is  TPanel ) then
     begin
       {获取按钮的tag属性值}
        tmpNo:= (Sender as TPanel).Tag;

        {选择云台}
        if (tmpNo>0) and (tmpNo<33) then

⌨️ 快捷键说明

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