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

📄 zddqsju.pas

📁 在delphi下对串口编程,实现与下位计的通信,并实时接收下位计数据,并保存到数据库中
💻 PAS
字号:
unit zddqsju;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBTables, DB, Grids, DBGrids, ExtCtrls, IBSQLMonitor,
  ComCtrls, OleCtrls, MSCommLib_TLB;

type
  Tzddqsj = class(TForm)
    Label1: TLabel;
    Bevel1: TBevel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Bevel2: TBevel;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Table1: TTable;
    Query1: TQuery;
    Bevel3: TBevel;
    Button1: TButton;
    Animate1: TAnimate;
    bh: TEdit;
    xh: TEdit;
    ch: TEdit;
    dw: TEdit;
    mz: TEdit;
    pz: TEdit;
    jz: TEdit;
    rq: TEdit;
    ybxh: TEdit;
    ybcs: TEdit;
    dwmc: TEdit;
    bzcs: TEdit;
    sby: TEdit;
    ykl: TEdit;
    MSComm1: TMSComm;
    Label15: TLabel;
    hwmc: TEdit;
    zhsjk: TDatabase;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure MSComm1Comm(Sender: TObject);
    procedure zdxsj;
    procedure zdfz;
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  zddqsj: Tzddqsj;

implementation

  uses publicfuctionu;
   var qbc0:string;
       ss1:string;
{$R *.dfm}

procedure Tzddqsj.Button1Click(Sender: TObject);
begin
  if mscomm1.PortOpen=true then
    mscomm1.PortOpen:=false;
  close;
end;
procedure Tzddqsj.FormActivate(Sender: TObject);
  var  cs0:string;
       SDATE_str:pchar;
       SSHORTDATE_str:pchar;
       STIME_str:pchar;
       STIMEFORMAT_str:pchar;
       length0:integer;
       modify0:boolean;
begin
  qbc0:='';
  cs0:='';
  modify0:=false;//建立运行环境
  length0:=30;
  getmem(SDATE_str,30);
  getmem(SSHORTDATE_str,30);
  getmem(STIME_str,30);
  getmem(STIMEFORMAT_str,30);
  GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SDATE,SDATE_str,length0);
  GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SSHORTDATE,SSHORTDATE_str,length0);
  GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_STIME,STIME_str,length0);
  GetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_STIMEFORMAT,STIMEFORMAT_str,length0);
  if SDATE_str<>'-' then
    begin
      SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SDATE,'-');
      modify0:=true;
    end;
  if SSHORTDATE_str<>'yyyy-MM-dd' then
    begin
      SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SSHORTDATE,'yyyy-MM-dd');
      modify0:=true;
    end;
  if STIME_str<>':' then
    begin
      SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_STIME,':');
      modify0:=true;
    end;
  if STIMEFORMAT_str<>'HH:mm:ss' then
    begin
      SetLocaleInfo(LOCALE_SYSTEM_DEFAULT,LOCALE_STIMEFORMAT,'HH:mm:ss');
      modify0:=true;
    end;
  if modify0=true then
    sendmessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,0);
  freemem(SDATE_str);
  freemem(SSHORTDATE_str);
  freemem(STIME_str);
  freemem(STIMEFORMAT_str);
  if mscomm1.PortOpen=true then
    mscomm1.PortOpen:=false;
  mscomm1._CommPort:=strtoint(qdkcs('commport'));
  mscomm1.InBufferSize:=strtoint(qdkcs('inbuffersize'));
  mscomm1.InputLen:=strtoint(qdkcs('inputlen'));
  cs0:=trim(qdkcs('波特率'));
  cs0:=cs0+','+trim(qdkcs('奇偶校验'));
  cs0:=cs0+','+trim(qdkcs('数据位'));
  cs0:=cs0+','+trim(qdkcs('停止位'));
  mscomm1.Settings:=cs0;
  if mscomm1.PortOpen=false then
  mscomm1.PortOpen:=true;
  query1.close;
  query1.SQL.Clear;
  query1.SQL.Add('delete table from 过磅登记临时表');
  query1.ExecSQL;
  if table1.Active=false  then
    table1.Active:=true;
end;

procedure Tzddqsj.MSComm1Comm(Sender: TObject);//自动读取数据
  var //bh0,xh0,ch0,dw0,rq0,ybxh0,ybcs0,dwmc0,bzcs0,sby0:string;
      //编号序号车号单位日期仪表序号仪表参数单位名称标准参数司磅员
      //mz0,pz0,jz0,ykl0:integer;
      //毛重皮重净重盈亏量
      //ysdwbm0,hwmcbm0,chbm0,sbybm0:string;
      //运输单位编码货物名称编码车号编码司磅员编码
      ss0:string;//串口新的输入
      i:integer;
      //ss1:string;//截取使用
      j:integer;
      k:integer;

begin
  flashwindow(self.Handle,true);
  //读取数据
  if mscomm1.CommEvent<>2 then
    begin
      exit;
    end;
  ss0:=mscomm1.Input;
  //分解各变量
  if  length(qbc0)>0 then//补打上次
    begin
      ss1:=qbc0+ss0;
      k:=pos(chr(01),ss1);
      if (k<>0)  and  (copy(ss1,k+38,1)=chr(03)) then
        begin
          xh.Text:=copy(ss1,k+1,6);
          ch.Text:=copy(ss1,k+8,5);
          pz.Text:=copy(ss1,k+13,6);
          mz.Text:=copy(ss1,k+19,7);
          hwmc.Text:=copy(ss1,k+28,2);
          dwmc.Text:=copy(ss1,k+30,2);
          ybcs.Text:=copy(ss1,k+33,5);
          ss1:='';
          qbc0:='';
          zdfz;
          zdxsj;
        end;
    end;
  i:=pos(chr(01),ss0);
  if (i=0) then  //无打印信号
    begin
      j:=pos(chr(04),ss0);
      if (j>0) and (copy(ss0,j+38,1)=chr(03)) then
        begin
          xh.Text:=copy(ss0,j+1,6);
          ch.Text:=copy(ss0,j+8,5);
          pz.Text:=copy(ss0,j+13,6);
          mz.Text:=copy(ss0,j+19,7);
          hwmc.Text:=copy(ss0,j+28,2);
          dwmc.Text:=copy(ss0,j+30,2);
          ybcs.Text:=copy(ss0,j+33,5);
          exit;
        end;
    end;
  if (i>0)  then
    begin
      if (length(copy(ss0,i+1,37))=37) and  (copy(ss0,i+38,1)=chr(03)) then  ////截取到一个正串
        begin
          xh.Text:=copy(ss0,i+1,6);       //xh.Text:=copy(ss1,3,6);
          ch.Text:=copy(ss0,i+8,5);       //ch.Text:=copy(ss1,10,5);
          pz.Text:=copy(ss0,i+13,6);      //pz.Text:=copy(ss1,15,6);
          mz.Text:=copy(ss0,i+19,7);      //mz.Text:=copy(ss1,21,7);
          hwmc.Text:=copy(ss0,i+28,2);    //hwmc.Text:=copy(ss1,30,2);
          dwmc.Text:=copy(ss0,i+30,2);    //dwmc.Text:=copy(ss1,32,2);
          ybcs.Text:=copy(ss0,i+33,5);    //ybcs.Text:=copy(ss1,35,5);
          zdfz;
          zdxsj;
          //判断是否有打印信号
          if (pos(chr(01),copy(ss0,i+38,45-i))<>0) then  //截取后半串
            begin
              k:=pos(chr(01),copy(ss0,i+38,45-i));
              qbc0:=copy(ss0,k,83-k);//包含chr(01)
            end;
        end
      else
        begin//无整串
          qbc0:=copy(ss0,i,83-i);//包含chr(2)
          //j:=pos(chr(04),ss0);
          //if (j>0) and (copy(ss0,j+38,1)=chr(03)) then
          //  begin
              //xh.Text:=copy(ss0,j+1,6);
              //xh.Text:=formatfloat('000000',(strtoint(xh.Text)+1));
              //ch.Text:=copy(ss0,j+8,5);
              //pz.Text:=copy(ss0,j+13,6);
              //mz.Text:=copy(ss0,j+19,7);
              //hwmc.Text:=copy(ss0,j+28,2);
              //dwmc.Text:=copy(ss0,j+30,2);
              //ybcs.Text:=copy(ss0,j+33,5);
              //zdfz;
              //zdxsj;
          //  end;
        end;
    end;
end;

procedure Tzddqsj.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if mscomm1.PortOpen=true then
  mscomm1.PortOpen:=false;
end;
procedure Tzddqsj.zdxsj;
  var str0:string;
  begin
    str0:='insert into 过磅登记表 values ('''+trim(bh.Text)+''','''+trim(xh.Text)
            +''','''+trim(dwmc.Text)+''','''+trim(hwmc.Text)+''','''+trim(ch.Text)
            +''','''+trim(pz.Text)+''','''+trim(mz.Text)+''','+trim(jz.Text)+','''+trim(rq.Text)
            +''','''+trim(ybxh.Text)+''','''+trim(ybcs.Text)+''','''+trim(bzcs.Text)+''','''+trim(dw.Text)
            +''','''+sby.Text+''','+trim(ykl.Text)+',''无'')';
    gxcsz('过磅登记表编号');
    query1.Close;
    query1.SQL.Clear;
    query1.SQL.Add(str0);
    query1.ExecSQL;
    str0:='insert into 过磅登记临时表 values ('''+trim(bh.Text)+''','''+trim(xh.Text)
            +''','''+trim(dwmc.Text)+''','''+trim(hwmc.Text)+''','''+trim(ch.Text)
            +''','''+trim(pz.Text)+''','''+trim(mz.Text)+''','+trim(jz.Text)+','''+trim(rq.Text)
            +''','''+trim(ybxh.Text)+''','''+trim(ybcs.Text)+''','''+trim(bzcs.Text)+''','''+trim(dw.Text)
            +''','''+sby.Text+''','+trim(ykl.Text)+',''无'')';
    query1.Close;
    query1.SQL.Clear;
    query1.SQL.Add(str0);
    query1.ExecSQL;
    table1.Refresh;
    table1.Last;
  end;
procedure Tzddqsj.zdfz;
  begin
    ybxh.Text:='1';
    bh.Text:=qcsz(10,'过磅登记表编号'); //编号
    dw.Text:='公斤';
    dw.Text:=qmc('系统设置','计量单位','参数名称','参数值');
    rq.Text:=datetimetostr(now);
    hwmc.Text:=qmc('货物编号表',hwmc.Text,'货物名称编码','货物名称');//货物名称
    dwmc.Text:=qmc('运输单位编号表',dwmc.Text,'运输单位编码','运输单位');//运输单位名称
    sby.Text:=qmc('司磅员编号表',sby.Text,'司磅员编码','司磅员');
    ch.Text:=qmc('车号编号表',ch.Text,'车号编码','车号');
    bzcs.Text:=qcsz(5,'仪表标准参数');
    try
      strtoint(mz.Text);
    except
      mz.Text:='0';
    end;
    try
      strtoint(pz.Text);
    except
      pz.Text:='0';
    end;
    try
      jz.Text:=inttostr(strtoint(mz.Text)-strtoint(pz.Text));
    except
      jz.Text:='0';
    end;
    try
      ykl.Text:=inttostr(trunc((strtofloat(bzcs.Text)-strtofloat(ybcs.Text))/(strtofloat(bzcs.Text))*(strtofloat(jz.Text))));
    except
      ykl.Text:='0';
    end;
  end;
procedure Tzddqsj.FormCreate(Sender: TObject);
 // var i:integer;
 //     str0:string;
 //     dd:Myaccess;
begin
  //dd.dataname:='磅房数据';      //extractfilepath(application.ExeName)
  //dd.pathname:=extractfilepath(application.ExeName)+'date\数据库.mdb';
  //dd.username:='';
  //dd.password:='';
  //ODBCACCESS(dd);
  try
    zhsjk.Connected:=false;
    zhsjk.Connected:=true;
  except
    application.MessageBox('数据库未连接,请连接数据库','提示',mb_ok);
    application.Terminate;
  end;
end;

end.

⌨️ 快捷键说明

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