本文介绍了(SNMP)如何设置对象的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用delphi xe3编写一个程序,该程序可以读取对象的值但不知道如何更改它?

I'm using delphi xe3 to write a program which can read object's value but don't know how to change it?

单击保存按钮以设置新的Power级别值

Click SAVE button to set new Power Level value

符号率是只读的

推荐答案

QuickSend( )仅用于检索值。没有等效的 QuickSend()来设置值。您将必须直接使用 SendQuery(),例如:

QuickSend() is only for retrieving values. There is no equivalent QuickSend() for setting values. You will have to use SendQuery() directly for that, eg:

procedure TForm1.BtnEnterClick(Sender: TObject);
var
  SNMP: TIdSNMP;
  dn, PLevel, SRate: string;
  p: Extended;
begin
  SNMP := TIdSNMP.Create(nil);
  try
    SNMP.Host := Trim(HostIP.Text);
    SNMP.Community := Trim(ComString.Text);
    if SNMP.Host = '' then begin
      MessageDlg('Chưa nhập IP của thiết bị!', mtError, [mbOK], 0);
      Exit;
    end;
    if SNMP.Community = '' then begin
      MessageDlg('Chưa nhập SNMP read community string', mtError, [mbOK], 0);
      Exit;
    end;
    PowerLevelValue.Clear;
    SymbolRateValue.Clear;
    SNMP.ReceiveTimeout := 1000;
    if SNMP.QuickSend('1.3.6.1.2.1.1.1.0', SNMP.Community, SNMP.Host, dn) then
      DeviceName.Caption := dn;
    if SNMP.QuickSend('1.3.6.1.4.1.6247.24.1.2.2.10.0', SNMP.Community, SNMP.Host, PLevel) then
    begin
      p := Abs(StrToFloat(Plevel))/10;
      Plevel := FloatToStr(p);
      PowerLevelValue.Text := Plevel;
    end;
    if SNMP.QuickSend('1.3.6.1.4.1.6247.24.1.2.2.12.0', SNMP.Community, SNMP.Host, SRate) then
      SymbolRateValue.Text := SRate;
  finally
    SNMP.Free;
  end;
end;

procedure TForm1.BtnSaveClick(Sender: TObject);
var
  SNMP: TIdSNMP;
  PLevel: string;
  p: Extended;
begin
  Plevel := PowerLevelValue.Text;
  p := StrToFloat(Plevel);
  PLevel := FloatToStr(Abs(p*10));

  SNMP := TIdSNMP.Create(nil);
  try
    SNMP.Host := Trim(HostIP.Text);
    SNMP.Community := Trim(ComString.Text);
    if SNMP.Host = '' then begin
      MessageDlg('Chưa nhập IP của thiết bị!', mtError, [mbOK], 0);
      Exit;
    end;
    if SNMP.Community = '' then begin
      MessageDlg('Chưa nhập SNMP read community string', mtError, [mbOK], 0);
      Exit;
    end;
    SNMP.ReceiveTimeout := 1000;
    SNMP.Query.Clear;
    SNMP.Query.PDUType := PDUSetRequest;
    SNMP.Query.MIBAdd('1.3.6.1.2.1.1.1.0', DeviceName.Caption);
    SNMP.Query.MIBAdd('1.3.6.1.4.1.6247.24.1.2.2.10.0', PLevel);
    SNMP.Query.MIBAdd('1.3.6.1.4.1.6247.24.1.2.2.12.0', SymbolRateValue.Text);
    SNMP.SendQuery;
  finally
    SNMP.Free;
  end;
end;

如您所见, SendQuery()支持多个OID,因此您可以将 QuickSend()替换为 SendQuery(),这样您只发送了1个查询,而不是3个查询:

As you can see, SendQuery() supports multiple OIDs, so you could replace QuickSend() with SendQuery() so you are only sending 1 query instead of 3 queries:

procedure TForm1.BtnEnterClick(Sender: TObject);
var
  SNMP: TIdSNMP;
  PLevel: string;
  p: Extended;
begin
  SNMP := TIdSNMP.Create(nil);
  try
    SNMP.Host := Trim(HostIP.Text);
    SNMP.Community := Trim(ComString.Text);
    if SNMP.Host = '' then begin
      MessageDlg('Chưa nhập IP của thiết bị!', mtError, [mbOK], 0);
      Exit;
    end;
    if SNMP.Community = '' then begin
      MessageDlg('Chưa nhập SNMP read community string', mtError, [mbOK], 0);
      Exit;
    end;
    PowerLevelValue.Clear;
    SymbolRateValue.Clear;
    SNMP.ReceiveTimeout := 1000;
    SNMP.Query.Clear;
    SNMP.Query.PDUType := PDUGetRequest;
    SNMP.Query.MIBAdd('1.3.6.1.2.1.1.1.0', '');
    SNMP.Query.MIBAdd('1.3.6.1.4.1.6247.24.1.2.2.10.0', '');
    SNMP.Query.MIBAdd('1.3.6.1.4.1.6247.24.1.2.2.12.0', '');
    if SNMP.SendQuery then
    begin
      DeviceName.Caption := SNMP.Reply.MIBGet('1.3.6.1.2.1.1.1.0');

      PLevel := SNMP.Reply.MIBGet('1.3.6.1.4.1.6247.24.1.2.2.10.0');
      p := Abs(StrToFloat(Plevel))/10;
      Plevel := FloatToStr(p);
      PowerLevelValue.Text := Plevel;

      SymbolRateValue.Text := SNMP.Reply.MIBGet('1.3.6.1.4.1.6247.24.1.2.2.12.0');
    end;
  finally
    SNMP.Free;
  end;
end;

这篇关于(SNMP)如何设置对象的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 01:36