串口通讯中比较常用的函数
作者:admin 日期:2006-07-25
分享几个自己在处理串口通讯中比较常用的函数,有兴趣的可以看看,给初学者参考下:
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;
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;
评论: 0 | 引用: 4378 | 查看次数: 24257
发表评论
你没有权限发表留言!