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

📄 main.~pas

📁 对于初学者很实用的实力开发程序
💻 ~PAS
字号:
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CPortCtl, CPort, ExtCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    GDate: TLabel;
    Lat: TLabel;
    Long: TLabel;
    Label16: TLabel;
    SysTimeshow: TLabel;
    Label18: TLabel;
    Timer1: TTimer;
    ComPort1: TComPort;
    ComDataPacket1: TComDataPacket;
    GTime: TLabel;
    ComComboBox1: TComComboBox;
    ComComboBox2: TComComboBox;
    ComComboBox3: TComComboBox;
    ComComboBox4: TComComboBox;
    ComComboBox5: TComComboBox;
    CheckBox1: TCheckBox;
    Label12: TLabel;
    GroupBox5: TGroupBox;
    Label14: TLabel;
    GTimeshow: TLabel;
    Label17: TLabel;
    GDateshow: TLabel;
    SysDateshow: TLabel;
    Label13: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ComComboBox1Change(Sender: TObject);
    procedure ComComboBox2Change(Sender: TObject);
    procedure ComComboBox3Change(Sender: TObject);
    procedure ComComboBox4Change(Sender: TObject);
    procedure ComComboBox5Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ComDataPacket1Packet(Sender: TObject; const Str: String);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Systime: TDateTime;
  Year, Month, Day, Hour, Min , Sec, Msec: Word;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  comdatapacket1.StopString :=#13 ;
end;

procedure TForm1.ComComboBox1Change(Sender: TObject);
begin
  ComPort1.Port:=ComComboBox1.Text;                     //ComComboBox1的参数值改变时将改变的值重新赋给comport1.Port
end;

procedure TForm1.ComComboBox2Change(Sender: TObject);
begin
  ComPort1.BaudRate:=StrToBaudRate(ComComboBox2.Text);  //ComComboBox2的参数值改变时将改变的值重新赋给comport1.BaudRate
end;

procedure TForm1.ComComboBox3Change(Sender: TObject);
begin
  ComPort1.DataBits:=StrToDataBits(ComComboBox3.Text);  //ComComboBox3的参数值改变时将改变的值重新赋给comport1.DataBits
end;

procedure TForm1.ComComboBox4Change(Sender: TObject);
begin
  ComPort1.StopBits:=StrToStopBits(ComComboBox4.Text);  //ComComboBox4的参数值改变时将改变的值重新赋给comport1.StopBits
end;

procedure TForm1.ComComboBox5Change(Sender: TObject);
begin
  ComPort1.Parity.Bits:=StrToParity(ComComboBox5.Text); //ComComboBox5的参数值改变时将改变的值重新赋给comport1.Parity.Bits
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  comport1.Open;                                       // 打开串口
  button1.Enabled:=false;                              // button1无效
  button2.Enabled:=true;                               // button2有效
  button3.Enabled:=true;
  ComComboBox1.Enabled:=false;
  ComComboBox2.Enabled:=false;
  ComComboBox3.Enabled:=false;
  ComComboBox4.Enabled:=false;
  ComComboBox5.Enabled:=false;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  comport1.Close;                                      //关闭串口
  button1.Enabled:=true;
  button2.Enabled:=false;
  button3.Enabled:=false;
  ComComboBox1.Enabled:=true;
  ComComboBox2.Enabled:=true;
  ComComboBox3.Enabled:=true;
  ComComboBox4.Enabled:=true;
  ComComboBox5.Enabled:=true;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Comport1.WriteStr(Edit1.Text+#13+#10);               //将Edit1.Text内容加上回车换行写入comport1
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Systime:=now;                                        //获得系统时间
  DecodeTime(Systime,Hour,Min,Sec,Msec);               //将TDateTime型态的时间变数,转为Word型态
  SysTimeshow.Caption:=IntToStr(Hour)+' 时 '+IntToStr(Min)+' 分 '+IntToStr(Sec)+' 秒';
  DecodeDate(Systime, Year, Month, Day);               //将TDateTime型态的日期变数,转为Word型态
  SysDateshow.Caption:=IntToStr(Year)+' 年 '+IntToStr(Month)+' 月 '+IntToStr(Day)+' 日';
end;

procedure TForm1.ComDataPacket1Packet(Sender: TObject; const Str: String);
var
  systemtime: TSystemTime;
  aa,  bb, cc, hh, dw, wxtime, wxdate: string;
  key, key1: char;
  i , j, k, m: Integer;
  Comma :array[1..12]of integer;                       //定义Comma数组,用来记录逗号在字符串中的位置
begin
  key1:='*';
  i:=1;
  m:=ord('G') xor ord('P') xor ord('R') xor ord('M') xor ord('C');    //将字符GPRMC逐个异或,所得值赋给m
    While str[i]<>key1 do                              //将字符串中的字符逐个与星号字符比较,如果不是星号字符,就将其与m异或,所得值重新赋给m
      begin
        m:=m xor ord(str[i]);
        i:=i+1;
      end;
  hh:=copy(str,i+1,2);                               //将校验和取出赋给hh,i记录的是星号字符的位置
  if m=strtoint('$'+hh) then                         //将hh加$变为十进制再转换成整型并与m进行比较,如果相等就进行以下代码
    begin
      key:=',';
      i:=1;
      j:=1;
      while Str[i]<>key1 do
        begin
          if str[i]=key  then                           //将字符串中的字符逐个与逗号字符比较,如果是就将逗号的位置放入数组中
            begin
              Comma[j]:=i;
              j:=j+1;
            end;
          i:=i+1;
        end;

      GTime.Caption:=copy(Str,Comma[1]+1,Comma[2]-Comma[1]-1);  //数据显示区显示
      GDate.Caption:=copy(Str,Comma[9]+1,Comma[10]-Comma[9]-1);
      Lat.Caption:=copy(Str,Comma[3]+1,Comma[4]-Comma[3]-1);
      Long.Caption:=copy(str,Comma[5]+1,Comma[6]-Comma[5]-1);
      aa:=copy(Str,Comma[4]+1,Comma[5]-Comma[4]-1);
      bb:=copy(Str,Comma[6]+1,Comma[7]-Comma[6]-1);
      if aa='N'  then  Label12.Caption:='北纬'
        else Label12.Caption:='南纬';
      if bb='E'  then  Label13.Caption:='东经'
        else  Label13.Caption:='西经';

      wxtime:=copy(Str,Comma[1]+1,Comma[2]-Comma[1]-1);        //卫星时间日期显示
      wxdate:=copy(Str,Comma[9]+1,Comma[10]-Comma[9]-1);
      GTimeshow.Caption:=inttostr(strtoint(copy(wxtime,1,2)))+' 时 '+inttostr(strtoint(copy(wxtime,3,2)))+' 分 '+inttostr(strtoint(copy(wxtime,5,2)))+' 秒';
      k:=strtoint(copy(wxdate,5,2));
      if k<=54  then cc:='20'                                  //用千年解释法判断年份
        else cc:='19';
      GDateshow.Caption:=cc+copy(wxdate,5,2)+' 年 '+inttostr(strtoint(copy(wxdate,3,2)))+' 月 '+inttostr(strtoint(copy(wxdate,1,2)))+' 日';

      dw:=copy(str,Comma[2]+1,Comma[3]-Comma[2]-1);
      if (dw='A') and (checkbox1.Checked=true) then            //如果卫星处于定位状态且checkbox1选中则动态修改系统时间
        begin
          systemtime.wYear:=strtoint(cc+copy(wxdate,5,2));        //设置系统时间和日期
          systemtime.wMonth:=strtoint(copy(wxdate,3,2));
          systemtime.wDay:=strtoint(copy(wxdate,1,2));
          systemtime.wHour:=strtoint(copy(wxtime,1,2));
          systemtime.wMinute:=strtoint(copy(wxtime,3,2));
          systemtime.wSecond:=strtoint(copy(wxtime,5,2));
          SetLocalTime(systemtime);
          Systime:=now;                                        //保持本机日期时间与卫星的同步更新
          DecodeTime(Systime,Hour,Min,Sec,Msec);
          SysTimeshow.Caption:=IntToStr(Hour)+' 时 '+IntToStr(Min)+' 分 '+IntToStr(Sec)+' 秒';
          DecodeDate(Systime, Year, Month, Day);
          SysDateshow.Caption:=IntToStr(Year)+' 年 '+IntToStr(Month)+' 月 '+IntToStr(Day)+' 日';
        end;
    end;
end;

end.



⌨️ 快捷键说明

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