问题描述
我正在与Delphi做一个项目。我要编写一个函数。我需要将浮点数据放入字节数组(TArray)中。此函数的c#代码如下。
public void SetCalibrationValue(byte channel,float value)
{
if (!serialPort.IsOpen)
return;
byte[] b = new byte[] {0x7E,0x0B,0x04,0x01,0x05,channel,0x00,0x00};
Array.copy(BitConverter.GetBytes(value),b,0x07,sizeof(float));
serialPort.Write(b,b.Length);
if (OnDataTransmit != null)
OnDataTransmit(new DataTransmitEventArgs(b));
}
我想用delphi编写此代码。到目前为止,我已经完成了,但是我无法继续。 这是我的代码:
procedure PListenerx.SetCalibrationValue (channel:Byte;value:single);
var
b:TArray<Byte>;
c:TArray<Byte>;
begin
//Comport kapalıysa kontrolü yapılacak
b[0]:=$7E;
b[1]:=$0B;
b[2]:=$04;
b[3]:=$01;
b[4]:=$05;
b[5]:=channel;
b[6]:=$00;
b[7]:=$00;
b[8]:=$00;
b[9]:=$00;
b[10]:=$00;
end;
解决方法
Move(value,b[7],SizeOf(Single));
将以`value
填充b
数组的末尾
请注意,Delphi 7中没有通用数组,因此代码看起来像
procedure PListenerx.SetCalibrationValue (channel:Byte;value:single);
var
b: array of Byte;
begin
SetLength(b,11);
//Comport kapalıysa kontrolü yapılacak
b[0]:=$7E;
b[1]:=$0B;
b[2]:=$04;
b[3]:=$01;
b[4]:=$05;
b[5]:=channel;
b[6]:=$00;
//no need to fill array end with junk
Move(value,SizeOf(Single));
//now send b[] to the port
end;