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

📄 com_send.pas

📁 串口数据传输,用Tcomm或MSComm控件
💻 PAS
📖 第 1 页 / 共 5 页
字号:
   showmessage( '此端口不存在或正在为其他程序占用!' ) ;
   exit;
 end
 else
 begin
    PurgeComm( hNewCommFile, PURGE_TXABORT or PURGE_RXABORT or  PURGE_TXCLEAR or PURGE_RXCLEAR ) ;    //  by hs
    CloseHandle( hNewCommFile );
    hNewCommFile := 0 ;
//    comm1.portopen:=false;
 end;
    sendbuf[0] := strtoint(edhouse.Text);
    sendbuf[1] := strtoint(edadress.Text);
    sendbuf[2] := $20;
    sendbuf[3] := $00;
    sendbuf[4] := $00;
    sendbuf[5] := $00;
    checksum;
    if comm1.PortOpen=false then
    comm1.PortOpen:=true;
    outputdata ;
    info_type :=$20;
 except               // by hs
  ;
 end;
 }
end;

procedure TForm1.Button2Click(Sender: TObject);
var
   i:integer;
   ComStr:String;
   hNewCommFile:   THandle;
   j : integer;
   k : integer;

   get_flag : boolean;
   SCommandInfo,SErrorInfo : string;
   power1,power2,power3,power4,power5,power6,power7,power8 : real;
   state1,state2,state3,state4,state5, state6,state7,state8 : integer;
   info_s : string;
begin
   allfalse;
   memo1.Clear;
   get_flag := ZjCommFun.get_single_board_state_power(cocomm.Text, strtoint(edhouse.Text), strtoint(edadress.Text),
                                       power1,power2,power3,power4,power5,power6,power7,power8,
                                       state1,state2,state3,state4,state5, state6,state7,state8 ,
                                       SCommandInfo,SErrorInfo);

   if get_flag = true then
   begin
     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率1:       ' +  floattostr(power1));
     memo1.Lines.Add('状态1:       ');
     {
     if ( state1 and 128 ) = 0 then info_s := info_s + 'E2正确   '
                               else info_s := info_s + 'E2错误   ' ;
     if ( state1 and 64 ) = 0 then info_s := info_s + '关断正确   '
                              else info_s := info_s + '关断失败   ' ;
     if ( state1 and 32 ) = 0 then info_s := info_s + '未透支   '
                              else info_s := info_s + '透支   ' ;
     if ( state1 and 16 ) = 0 then info_s := info_s + '不定时   '
                              else info_s := info_s + '定时   ' ;
     if ( state1 and 8 ) = 0 then info_s := info_s + '阻性   '
                             else info_s := info_s + '非阻性   ' ;
     if ( state1 and 4 ) = 0 then info_s := info_s + '定时开   '
                             else info_s := info_s + '定时关   ' ;
     if ( state1 and 2 ) = 0 then info_s := info_s + '不过载   '
                             else info_s := info_s + '过载   ' ;
     if ( state1 and 1 ) = 0 then info_s := info_s + '不欠费   '
                             else info_s := info_s + '欠费   ' ;
                             }

     if ( state1 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state1 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state1 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state1 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state1 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state1 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state1 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state1 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');

     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率2:       ' +  floattostr(power2));
     memo1.Lines.Add('状态2:       ');
     if ( state2 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state2 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state2 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state2 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state2 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state2 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state2 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state2 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');

     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率3:       ' +  floattostr(power3));
     memo1.Lines.Add('状态3:       ');
     if ( state3 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state3 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state3 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state3 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state3 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state3 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state3 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state3 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');

     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率4:       ' +  floattostr(power4));
     memo1.Lines.Add('状态4:       ');
     if ( state4 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state4 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state4 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state4 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state4 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state4 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state4 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state4 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');

     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率5:       ' +  floattostr(power5));
     memo1.Lines.Add('状态5:       ');
     if ( state5 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state5 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state5 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state5 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state5 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state5 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state5 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state5 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');

     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率6:       ' +  floattostr(power6));
     memo1.Lines.Add('状态6:       ');
     if ( state6 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state6 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state6 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state6 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state6 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state6 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state6 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state6 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');


     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率7:       ' +  floattostr(power7));
     memo1.Lines.Add('状态7:       ');
     if ( state7 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state7 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state7 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state7 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state7 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state7 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state7 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state7 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');

     //===========================================================
     info_s := '';
     memo1.Lines.Add('功率8:       ' +  floattostr(power8));
     memo1.Lines.Add('状态8:       ');
     if ( state8 and 128 ) = 0 then info_s := info_s + '0'
                               else info_s := info_s + '1' ;
     if ( state8 and 64 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state8 and 32 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state8 and 16 ) = 0 then info_s := info_s + '0'
                              else info_s := info_s + '1' ;
     if ( state8 and 8 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state8 and 4 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state8 and 2 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     if ( state8 and 1 ) = 0 then info_s := info_s + '0'
                             else info_s := info_s + '1' ;
     memo1.Lines.Add(info_s);
     memo1.Lines.Add('   ');
     memo1.Lines.Add('第8位(E2错误):  0-正确,  1-失败');
     memo1.Lines.Add('第7位(关断效果):0-正确,  1-失败');
     memo1.Lines.Add('第6位(透支用电):0-未透支,1-透支');
     memo1.Lines.Add('第5位(定时设置):0-不定时,1-定时');
     memo1.Lines.Add('第4位(阻性状态):0-阻性,  1-非阻性');
     memo1.Lines.Add('第3位(定时开关):0-开,    1-关');
     memo1.Lines.Add('第2位(过载状态):0-不过载,1-过载');
     memo1.Lines.Add('第1位(欠费状态):0-不欠费,1-欠费');

     memo2.Lines.Add('OK');
   end
   else  memo2.Lines.Add(SErrorInfo);

   alltrue;
{
  clear;
  try          // by hs
  if comm1.PortOpen = true then
    comm1.PortOpen := false;

  if coComm.text='com1' then
  begin
    comm1.CommPort:=pnCOM1;
  end;

  if coComm.text='com2' then
  begin
    comm1.CommPort:=pnCOM2;
  end;

  if coComm.text='com3' then
  begin
    comm1.CommPort:=pnCOM3;
  end;

  if coComm.text='com4' then
  begin
    comm1.CommPort:=pnCOM4;
  end;

  if coComm.text='com5' then
  begin
    comm1.CommPort:=pnCOM5;
  end;

 ComStr:=cocomm.Text;
 hNewCommFile := CreateFile( PChar(ComStr),
                             GENERIC_READ or GENERIC_WRITE,
                             0, {not shared}
                        //     nil, {no security ??}
                        //     OPEN_EXISTING,
                        //     0,{No Overlapped}
                        //     0 {template} );
 {
 if hNewCommFile = INVALID_HANDLE_VALUE then
 begin
   showmessage( '此端口不存在或正在为其他程序占用!' ) ;
   exit;
 end
 else
 begin
    PurgeComm( hNewCommFile, PURGE_TXABORT or PURGE_RXABORT or  PURGE_TXCLEAR or PURGE_RXCLEAR ) ;    //  by hs
    CloseHandle( hNewCommFile );
    hNewCommFile := 0 ;
//    comm1.portopen:=false;
 end;
    sendbuf[0] := strtoint(edhouse.Text);
    sendbuf[1] := strtoint(edadress.Text);
    sendbuf[2] := $21;
    sendbuf[3] := $00;
    sendbuf[4] := $00;
    sendbuf[5] := $00;
    checksum;
    if comm1.PortOpen=false then
    comm1.PortOpen:=true;
    outputdata ;
    info_type :=$21;
 except               // by hs
  ;
 end;
 }
end;


procedure TForm1.Button4Click(Sender: TObject);
var
   i:integer;
   ComStr:String;
   hNewCommFile:   THandle;
   j : integer;
   k : integer;
   count_s : string;
   count : integer;
   hei : string;
   low : string;

   get_flag : boolean;
   SCommandInfo,SErrorInfo : string;
begin
    allfalse;
    if trim(edcount.Text) = '' then
    begin
      showmessage('请输入设定值');
      edcount.SetFocus;
      alltrue;
      exit;
    end;


   get_flag := ZjCommFun.setroom_quantity(cocomm.Text, strtoint(edhouse.Text), strtoint(edadress.Text),
                              strtoint(edchannel.Text), strtofloat(edcount.Text),
                              SCommandInfo,SErrorInfo );

   if get_flag = true then memo2.Lines.Add('OK')
   else  memo2.Lines.Add(SErrorInfo);
   alltrue;
{
  clear;
  try          // by hs
  if comm1.PortOpen = true then
    comm1.PortOpen := false;

  if coComm.text='com1' then
  begin
    comm1.CommPort:=pnCOM1;
  end;

  if coComm.text='com2' then
  begin
    comm1.CommPort:=pnCOM2;
  end;

  if coComm.text='com3' then
  begin
    comm1.CommPort:=pnCOM3;
  end;

  if coComm.text='com4' then
  begin
    comm1.CommPort:=pnCOM4;
  end;

  if coComm.text='com5' then
  begin
    comm1.CommPort:=pnCOM5;
  end;

 ComStr:=cocomm.Text;
 hNewCommFile := CreateFile( PChar(ComStr),
                             GENERIC_READ or GENERIC_WRITE,
                             0, {not shared}
                  //           nil, {no security ??}
                  //           OPEN_EXISTING,
                  //           0,{No Overlapped}
                  //           0 {template} );
 {
 if hNewCommFile = INVALID_HANDLE_VALUE then
 begin
   showmessage( '此端口不存在或正在为其他程序占用!' ) ;
   exit;
 end
 else
 begin
    PurgeComm( hNewCommFile, PURGE_TXABORT or PURGE_RXABORT or  PURGE_TXCLEAR or PURGE_RXCLEAR ) ;    //  by hs
    CloseHandle( hNewCommFile );
    hNewCommFile := 0 ;
//    comm1.portopen:=false;
 end;
    if trim(edcount.Text) = '' then
    begin
      showmessage('请输入设定值');
      edcount.SetFocus;
      exit;
    end;
    count := strtoint(trim(edcount.Text));
    count_s := inttohex(count,4);
    if length(count_s) > 4 then
    begin
      showmessage('输入范围过大,请重输!!');
      edcount.SetFocus;
      exit;
    end;

    hei := '$' + copy(count_s,1,2);
    low := '$' + copy(count_s,3,2);

    sendbuf[0] := strtoint(edhouse.Text);
    sendbuf[1] := strtoint(edadress.Text);
    sendbuf[2] := $26;
    sendbuf[3] := strtoint(edchannel.Text);
    sendbuf[4] := strtoint(hei);
    sendbuf[5] := strtoint(low);
    checksum;
    if comm1.PortOpen=false then
    comm1.PortOpen:=true;

    outputdata;
    info_type :=$26;


 except               // by hs
  ;
 end;
 }
end;


procedure TForm1.Comm1ReceiveData(Sender: TObject);
{var

⌨️ 快捷键说明

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