问题
I'm using delphi xe3 to write a program which can read object's value but don't know how to change it?
Click SAVE button to set new Power Level value
Symbol Rate is read only

回答1:
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;
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;
来源:https://stackoverflow.com/questions/23010142/snmp-how-to-set-objects-value