送分拉,串口通信的问题,谢谢大家了~~~~~~~~~~~~ ( 积分: 50 )

W

wwq_80

Unregistered / Unconfirmed
GUEST, unregistred user!
我做的是用mscomm控件的串口通信的程序,主要是实现通过选择发送方式为ascii或者十六进制,在memo1中显示出接收到的数据ascii形式和十六进制形式。这个程序在vb中已经调通了,可是到了delphi里面遇到了不少问题,我觉得是一下两个函数的问题,其中动态数组的问题多,请大家多多帮忙,谢谢!!


Function TForm1.ConvertHexChr(str:String): integer;
var
test:Integer;
begin

test:=ord(str);
if (test >= ord('0')) And (test <= ord('9')) Then
test:= test - ord('0')
Else If (test >= ord('a')) And (test <= ord('f')) Then
test:= test - ord('a') + 10
Else If (test >= ord('A')) And (test <= ord('F')) Then
test:= test - ord('A') + 10
Else
test:= -1;

ConvertHexChr:= test;
{test:= Asc(str);
If test >= Asc(&quot;0&quot;) And test <= Asc(&quot;9&quot;) Then
test:= test - Asc(&quot;0&quot;);
ElseIf test >= Asc(&quot;a&quot;) And test <= Asc(&quot;f&quot;) Then
test:= test - Asc(&quot;a&quot;) + 10;
ElseIf test >= Asc(&quot;A&quot;) And test <= Asc(&quot;F&quot;) Then
test:= test - Asc(&quot;A&quot;) + 10 ;
Else
test:= -1

ConvertHexChr:= test
}

End;


//字符串表示的十六进制数据转化为相应的字节串


Function TForm1.strHexToByteArray(strText:String;bytByte:array of Byte):Integer;
var
HexData:Integer
//十六进制(二进制)数据字节对应值
hstr:String
//高位字符
lstr:String
//低位字符
highhexdata:Integer
//高位数值
lowhexdata:Integer
//低位数值
hexdatalen:Integer
//字节数
stringlen:Integer
//字符串长度
account:Integer
//计数
n:integer;

begin
hexdatalen:= 0;
strHexToByteArray:= 0;
n:=1;
stringlen:= Length(strText);
account:= stringlen div 2 ;
setlength(bytByte,account);

while (n>= 1) and (n<=stringlen) do
begin

repeat
hstr:= copy(strText, n, 1);
n:= n + 1;
If (n - 1) > stringlen Then
begin
hexdatalen:= hexdatalen - 1;

Exit;
end;

until not( hstr=' ');

repeat

lstr:= copy(strText, n, 1);
n:= n + 1;
If (n - 1) > stringlen Then
begin
hexdatalen:= hexdatalen - 1 ;

Exit;
end;
until not(lstr =' ');
n := n - 1;
If n > stringlen Then
begin
hexdatalen:= hexdatalen - 1;
Exit;
end
else
begin
highhexdata:= ConvertHexChr(hstr)
//获得高位值
lowhexdata:= ConvertHexChr(lstr)
//获得低位值
end;

If (highhexdata = -1) Or (lowhexdata = -1) Then //遇到非法字符中断转化
begin
hexdatalen := hexdatalen - 1;
Exit;
end
Else
begin
HexData:= highhexdata * 16 + lowhexdata;
bytByte[hexdatalen] := HexData;
hexdatalen:= hexdatalen + 1;
end;
n:=n+1;
end;

If hexdatalen > 0 Then //修正最后一次循环改变的数值
begin
hexdatalen:= hexdatalen - 1;
setlength(bytByte,hexdatalen);
end
Else
begin
setlength(bytByte,0);
end;


If (stringlen= 0) Then //如果是空字符串,则不会进入循环体
strHexToByteArray:= 0
Else
strHexToByteArray:= hexdatalen + 1;

end;
 
我做的是用mscomm控件的串口通信的程序,主要是实现通过选择发送方式为ascii或者十六进制,在memo1中显示出接收到的数据ascii形式和十六进制形式。这个程序在vb中已经调通了,可是到了delphi里面遇到了不少问题,我觉得是一下两个函数的问题,其中动态数组的问题多,请大家多多帮忙,谢谢!!


Function TForm1.ConvertHexChr(str:String): integer;
var
test:Integer;
begin

test:=ord(str);
if (test >= ord('0')) And (test <= ord('9')) Then
test:= test - ord('0')
Else If (test >= ord('a')) And (test <= ord('f')) Then
test:= test - ord('a') + 10
Else If (test >= ord('A')) And (test <= ord('F')) Then
test:= test - ord('A') + 10
Else
test:= -1;

ConvertHexChr:= test;
{test:= Asc(str);
If test >= Asc(&quot;0&quot;) And test <= Asc(&quot;9&quot;) Then
test:= test - Asc(&quot;0&quot;);
ElseIf test >= Asc(&quot;a&quot;) And test <= Asc(&quot;f&quot;) Then
test:= test - Asc(&quot;a&quot;) + 10;
ElseIf test >= Asc(&quot;A&quot;) And test <= Asc(&quot;F&quot;) Then
test:= test - Asc(&quot;A&quot;) + 10 ;
Else
test:= -1

ConvertHexChr:= test
}

End;


//字符串表示的十六进制数据转化为相应的字节串


Function TForm1.strHexToByteArray(strText:String;bytByte:array of Byte):Integer;
var
HexData:Integer
//十六进制(二进制)数据字节对应值
hstr:String
//高位字符
lstr:String
//低位字符
highhexdata:Integer
//高位数值
lowhexdata:Integer
//低位数值
hexdatalen:Integer
//字节数
stringlen:Integer
//字符串长度
account:Integer
//计数
n:integer;

begin
hexdatalen:= 0;
strHexToByteArray:= 0;
n:=1;
stringlen:= Length(strText);
account:= stringlen div 2 ;
setlength(bytByte,account);

while (n>= 1) and (n<=stringlen) do
begin

repeat
hstr:= copy(strText, n, 1);
n:= n + 1;
If (n - 1) > stringlen Then
begin
hexdatalen:= hexdatalen - 1;

Exit;
end;

until not( hstr=' ');

repeat

lstr:= copy(strText, n, 1);
n:= n + 1;
If (n - 1) > stringlen Then
begin
hexdatalen:= hexdatalen - 1 ;

Exit;
end;
until not(lstr =' ');
n := n - 1;
If n > stringlen Then
begin
hexdatalen:= hexdatalen - 1;
Exit;
end
else
begin
highhexdata:= ConvertHexChr(hstr)
//获得高位值
lowhexdata:= ConvertHexChr(lstr)
//获得低位值
end;

If (highhexdata = -1) Or (lowhexdata = -1) Then //遇到非法字符中断转化
begin
hexdatalen := hexdatalen - 1;
Exit;
end
Else
begin
HexData:= highhexdata * 16 + lowhexdata;
bytByte[hexdatalen] := HexData;
hexdatalen:= hexdatalen + 1;
end;
n:=n+1;
end;

If hexdatalen > 0 Then //修正最后一次循环改变的数值
begin
hexdatalen:= hexdatalen - 1;
setlength(bytByte,hexdatalen);
end
Else
begin
setlength(bytByte,0);
end;


If (stringlen= 0) Then //如果是空字符串,则不会进入循环体
strHexToByteArray:= 0
Else
strHexToByteArray:= hexdatalen + 1;

end;
 
不是有现成的串口调试助手吗,何必浪费精力啊?
 
MSCOMM不熟悉,不过我做过的串口也有收发二进制的东西的,不需要楼主这么复制,换SPCOMM看看

http://www.delphibbs.com/delphibbs/dispq.asp?LID=2599952
标题: SPComm读取数据问题
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
780
import
I
I
回复
0
查看
713
import
I
顶部