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

📄 全局变量.txt

📁 使用Delphi 6.0开发用于控制空调的程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:

实现代码就不贴了。 


再示范一个从基类派生的最简单的应用:


  TSimplyGY = class(TCustomGY)
  private
    FData:TarrayByte;       //已处理好的Data,需要反转的数据已经反转
  protected
    procedure SetData(const Value: string); override; //设置数据区
    function ReadData:string; override; //ReadData
  public
  end;


子类中实现基类规定的虚函数。 


有了这样一个类,就可以派生Data区不同的子类了。所要做的处理就是处理Data数据。不同的命令Data域的格式不同。 


 Data数据域的处理,其原理和上面所讲的类似,无非都是字节比较判断。 


再分享几个自己在处理串口通讯中比较常用的函数,有兴趣的可以看看,给初学者参考下: 


 function StrToHexStr(const S:string):string;


//字符串转换成16进制字符串

var
  I:Integer;
begin
  for I:=1 to Length(S) do
  begin
    if I=1 then
      Result:=IntToHex(Ord(S[1]),2)
    else Result:=Result+' '+IntToHex(Ord(S[I]),2);
  end;
end;




function HexStrToStr(const S:string):string;
//16进制字符串转换成字符串
var
  t:Integer;
  ts:string;
  M,Code:Integer;
begin
  t:=1;
  Result:='';
  while t<=Length(S) do
  begin
    while not (S[t] in ['0'..'9','A'..'F','a'..'f']) do
      inc(t);
    if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
      ts:='$'+S[t]
    else
     ts:='$'+S[t]+S[t+1];
    Val(ts,M,Code);
    if Code=0 then
      Result:=Result+Chr(M);
    inc(t,2);
  end;
end;




function StrToTenStr(const S:string):string;

//字符串转换成10进制字符串
var
  I:Integer;
begin
  for I:=1 to Length(S) do
  begin
    if I=1 then
      Result:=IntTostr(Ord(S[1]))
    else Result:=Result+' '+IntToStr(Ord(S[I]));
  end;
end;


function StrToByteArray(const S:string):TarrayByte;
//字符串转换成相应Byte
var
  I:Integer;
begin
  SetLength(Result,Length(S));
  for I:=1 to Length(S) do
    Result[I-1]:=Ord(S[I]);
end;

function ByteArrayToStr(const aB:TarrayByte):string;//Byte转换成相应字符串
var
  I:Integer;
begin
  SetLength(Result,High(aB)+1);
  for I:=0 to High(aB) do
    Result[I+1]:=Chr(aB[I]);
end;

function StrToBcdByteArray(const S:string; const Rotate:Boolean=False):TarrayByte; //字符串转换成相应BCDByte
var
  ts:string;
  I:Integer;
begin
  if Odd(Length(S)) then
    ts:='0'+S
  else ts:=S;
  SetLength(Result,Length(ts) div 2);
  for I:=0 to High(Result) do
  begin
    if Rotate then
      Result[High(Result)-I]:=StrToInt('$'+ts[2*I+1]+ts[2*I+2])
    else
      Result[I]:=StrToInt('$'+ts[2*I+1]+ts[2*I+2]);
  end;
end;

function BcdByteArrayToStr(const aNum:TarrayByte; const Rotate:Boolean=False):string; //BCDByte转换成相应字符串
var
  I:Integer;
  t:string;
begin
  SetLength(Result,2*(High(aNum)+1));
  for I:=0 to High(aNum) do
  begin
    t:=IntToHex(aNum[I],2);
    if Rotate then
    begin
      Result[2*(High(aNum)-I)+1]:=t[1];
      Result[2*(High(aNum)-I)+2]:=t[2];
    end
    else begin
      Result[2*I+1]:=t[1];
      Result[2*I+2]:=t[2];
    end;
  end;
end;




//集合形式转换成数组


function SetToByteArray(s:array of const):TarrayByte;
var
  I:Byte;
begin
  SetLength(Result,High(s)+1);
  for I:=0 to High(s) do
    Result[I]:=S[I].VInteger;
end;




function SetToWordArray(s:array of const):TarrayWord;
var
  I:Byte;
begin
  SetLength(Result,High(s)+1);
  for I:=0 to High(s) do
    Result[I]:=S[I].VInteger;
end;




function StrToBinStr(const S:string):string;
//字符串转换为2进制字符串
var
  I:Integer;
begin
  Result:='';
  for I:=1 to Length(S) do
  begin
    if I=1 then
      Result:=BinStrArray[Ord(S[1])]
    else Result:=Result+' '+BinStrArray[Ord(S[I])];
  end;
end;
function ByteToBinStr(const B:Byte):string;
//Byte型转换为二进制字符串
var
  I:Integer;
begin
  Result:='';
  for I:=7 downto 0 do
    Result:=Result+Chr(((B shr I) and 1)+48);
end;

function BinStrToByte(const S:string):Byte;
//二进制字符串转成数字
var
  S1:string[8];
  L:Integer;
  I:Integer;
begin
  S1:='00000000';
  L:=min(8,Length(S));
  for I:=L downto 1 do
    if S[I]='1' then
      S1[8+I-L]:=S[I]
    else
      S1[8+I-L]:='0';
  Result:=0;
  for I:=0 to 7 do
    Result:=Result+(((Ord(S1[I+1])-48) shl (7-I)));
end;

function T2ByteToWord(const T:T2Byte;const LowFront:Boolean=False):Word;
//T2Byte类型转换成Word类型
var
  a1,a2:Byte;
begin
  if LowFront then
  begin
    a1:=T[1];
    a2:=T[0];
  end
  else begin
    a1:=T[0];
    a2:=T[1];
  end;
  Result:=a1;
  Result:=(Result shl 8) + a2;
end;


function WordToT2Byte(const aNum:Word;const LowFront:Boolean=False):T2Byte;//Word类型转换成T2Byte类型
var
  a1,a2:Byte;
begin
  a1:=aNum;
  a2:=aNum shr 8;
  if LowFront then
  begin
    Result[0]:=a1;
    Result[1]:=a2;
  end
  else begin
    Result[0]:=a2;
    Result[1]:=a1;
  end;
end;

function ByteToBCDByte(const B:Byte;const aType:TBCDType=bt8421):Byte;
//Byte转换成BCD码
var
  B1,B2:Byte;
  function T2421(const P:Byte):Byte;
  begin
    if P<5 then
      Result:=P
    else Result:=P+6;
  end;
begin
  if B>99 then
    Result:=0
  else begin
    B1:=B div 10;
    B2:=B mod 10;
    case aType of
      bt8421:begin
        Result:=(B1 shl 4)+B2;
      end;
      bt2421:begin
        Result:=(T2421(B1) shl 4)+T2421(B2);
      end;
      btR3:begin
        Result:=((B1+3) shl 4)+(B2+3)
      end;
      else Result:=(B1 shl 4)+B2;
    end;
  end;
end;

function BCDByteToByte(const B:Byte;const aType:TBCDType=bt8421):Byte;
//BCD转成Byte型
var
  B1,B2:Byte;
  function Ts2421(const P:Byte):Byte;
  begin
    if P<5 then
      Result:=P
    else Result:=P-6;
  end;
begin
  B1:=(B and $F0) shr 4;
  B2:=B and $0F;
  case aType of
    bt8421: begin
      Result:=B1*10+B2;
    end;
    bt2421: begin
      Result:=(Ts2421(B1)*10)+Ts2421(B2);
    end;
    btR3: begin
      Result:=((B1-3)*10)+(B2-3)
    end;
    else Result:=B1*10+B2;
  end;
end;

function BCDByteToByteFF(const B:Byte;Auto:Boolean=True):Byte;
//BCD转成Byte型 FF-->FF 8421码
var
  B1,B2:Byte;
begin
  if (B=$FF)or(Auto and (B>=$AA)) then
    Result:=B
  else begin
    B1:=(B and $F0) shr 4;
    B2:=B and $0F;
    Result:=B1*10+B2;
  end;
end;

function ByteToBCDByteFF(const B:Byte;Auto:Boolean=True):Byte;
//Byte转换成BCD码 FF-->FF 8421码
var
  B1,B2:Byte;
begin
  if (B=$FF)or(Auto and (B>=$AA)) then
    Result:=B
  else if B>99 then
    Result:=0
  else begin
    B1:=B div 10;
    B2:=B mod 10;
    Result:=(B1 shl 4)+B2;
  end;
end; 


 6.结束语

⌨️ 快捷键说明

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