你可以参考一下 下面的代码 function HexStrToStr(const S:string):string; var t:Integer; ts:string; M,Code:Integer; begin t:=1; Result:=''; while t<=Length(S) do begin while (t<=Length(S)) and (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;procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var i,k:integer; s, str1:string ; begin SetLength(Str1,BufferLength); move(buffer^,pchar(Str1)^,bufferlength); if length(trim(Str1)) <= 0 then Exit; for i := 1 to length(Str1) do s := s + inttohex(ord(Str1[i]), 2); ReturnStr:=HexStrToStr(s);
if pos('N',inputData)>pos(#10,inputData) then ///如果不是老机子() begin
while len>1 do //判断取得的arry的长度,和buf的长度是否一样。 // begin len:=length(cash); //数据长度 huiche:=pos(#10,cash); //换行的位置 第一行换行的位置 ,是第几个字符串 intbegin:=pos(#02,Cash); //begin 's position intend:=pos(#03,Cash); //end 's position cashtemp:=MidStr(Cash,intbegin,intend); insertdata(cashtemp );//数据处理 cash := MidStr(Cash,pos(#03,Cash)+2,(len-length(cashtemp))); if pos(#02,cash)=0 then begin len:=0; end ; end; end else begin len:=length(inputData); //数据长度 huiche:=pos(#10,inputData); //换行的位置 第一行换行的位置 ,是第几个字符串
while len>1 do //判断取得的arry的长度,和buf的长度是否一样。 begin len:=length(cash); //数据长度 huiche:=pos(#10,cash); //换行的位置 第一行换行的位置 ,是第几个字符串 intbegin:=pos(#02,Cash); //begin 's position intend:=pos(#03,Cash); //end 's position cashtemp:=MidStr(Cash,intbegin,intend); insertdata(cashtemp );//数据处理 cash := MidStr(Cash,pos(#03,Cash)+2,(len-length(cashtemp)));
if pos(#02,cash)=0 then begin len:=0; end ; end; end; BasicForm.listADOQuery.GotoBook(savePlace); //回到刷新前的位置 BasicForm.listADOQuery.FreeBook(savePlace); //释放标签
SetLength(Str1,BufferLength); move(buffer^,pchar(Str1)^,bufferlength); for i:=0 to bufferlength-1 do Str:=Str+chr(Str1[i]); ShowMessage(Str);
1.用数据位8,停止位2,波特率是4800,或者是1200,的,校验位是无校验 你接收的数据长度是多少? 考虑一下 comm1.ReadTotalTimeoutConstant这个问题,不存在啊,我才发送一个检验结果,256个字符这样,就是乱码,comm1.ReadTotalTimeoutConstant=1002. SetLength(Str1,BufferLength); move(buffer^,pchar(Str1)^,bufferlength); for i:=0 to bufferlength-1 do Str:=Str+chr(Str1[i]); ShowMessage(Str); 这个我实验看
2. SetLength(Str1,BufferLength); move(buffer^,pchar(Str1)^,bufferlength); for i:=0 to bufferlength-1 do Str:=Str+chr(Str1[i]); ShowMessage(Str); 这段代码有问题吧,Str:=Str+chr(Str1[i]); 这里执行不了啊
各位老大,请指教啊,还是不能解决问题啊,难道我要还mscomm控件?
看看这样收到了什么数据procedure Tfrmmain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); var rbuf:array of byte; i: integer; str: string; begin setLength(rbuf, BufferLength); move(Buffer^, PChar(rbuf)^, BufferLength); str:=''; for i:=0 to bufferlength-1 do begin str:=str+inttostr(rbuf[i]); end; memo1.lines.add(str); end;
function HexStrToStr(const S:string):string;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin
while (t<=Length(S)) and (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;procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i,k:integer;
s, str1:string ;
begin
SetLength(Str1,BufferLength);
move(buffer^,pchar(Str1)^,bufferlength); if length(trim(Str1)) <= 0 then Exit;
for i := 1 to length(Str1) do
s := s + inttohex(ord(Str1[i]), 2); ReturnStr:=HexStrToStr(s);
Memo1.Lines.Add('收到'+s);
END;
var
huiche,len,konghang:integer;
inputData,Cash,cashtemp:String;
pstr:PChar;
FStream: TIniFile;
filename:string;
s:string;
intbegin,intend:Integer;// 数组的长度。
label outer;begin
// 保存右边列表中的书签位置,//以便接收完数据刷新后回到原来的位置,不影响操作员的录入
savePlace:=BasicForm.listADOQuery.GetBook; pstr:=Buffer; //取得接收的 数据
inputData:=pstr; //取得接收的数据
Cash:=inputData; //缓存到一个变量,提供操作
ShowMessage('cash'+cash); //在这里提示,就是乱码了,下面的操作都是无意义的啦*******
len:=length(cash);
if pos('N',inputData)>pos(#10,inputData) then ///如果不是老机子()
begin
while len>1 do //判断取得的arry的长度,和buf的长度是否一样。 //
begin
len:=length(cash); //数据长度
huiche:=pos(#10,cash); //换行的位置 第一行换行的位置 ,是第几个字符串
intbegin:=pos(#02,Cash); //begin 's position
intend:=pos(#03,Cash); //end 's position
cashtemp:=MidStr(Cash,intbegin,intend);
insertdata(cashtemp );//数据处理
cash := MidStr(Cash,pos(#03,Cash)+2,(len-length(cashtemp)));
if pos(#02,cash)=0 then
begin
len:=0;
end ;
end;
end
else
begin
len:=length(inputData); //数据长度
huiche:=pos(#10,inputData); //换行的位置 第一行换行的位置 ,是第几个字符串
while len>1 do //判断取得的arry的长度,和buf的长度是否一样。
begin
len:=length(cash); //数据长度
huiche:=pos(#10,cash); //换行的位置 第一行换行的位置 ,是第几个字符串
intbegin:=pos(#02,Cash); //begin 's position
intend:=pos(#03,Cash); //end 's position cashtemp:=MidStr(Cash,intbegin,intend); insertdata(cashtemp );//数据处理 cash := MidStr(Cash,pos(#03,Cash)+2,(len-length(cashtemp)));
if pos(#02,cash)=0 then
begin
len:=0;
end ;
end;
end; BasicForm.listADOQuery.GotoBook(savePlace); //回到刷新前的位置 BasicForm.listADOQuery.FreeBook(savePlace); //释放标签
end;
得处理一下
你可以参考一下2楼的代码
move(buffer^,pchar(Str1)^,bufferlength);
Cash:=Str1;
ShowMessage('cash'+cash);一堆乱码
数据位8,停止位1,波特率是4800,或者是1200,的,校验位是无校验,
来接受数据,但是接受就是乱码,而且他用超级终端来接受就是可以正常的了,老说是我的软件问题,问题是我用CommReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
来接受,一接受就显示,就是乱码,我也无语了
但是
数据位8,停止位2,波特率是4800,或者是1200,的,校验位是无校验, 就可以接受数据,正常
请教了
DtrControl = DtrDisable
DsrSensitivity = False
TxContinueOnXoff = False
Outx_XonXoffFlow = False
Inx_XonXoffFlow = False
ReplaceWhenParityError = False
IgnoreNullChar = False
RtsControl = RtsDisable
DtrControl = DtrDisable
DsrSensitivity = False
TxContinueOnXoff = False
Outx_XonXoffFlow = False
Inx_XonXoffFlow = False
ReplaceWhenParityError = False
IgnoreNullChar = False
RtsControl = RtsDisable
然后还是错误哦啊晕倒了,是不是我的设置的问题啊,为什么超级终端可以接受列?
你接收的数据长度是多少?
考虑一下
comm1.ReadTotalTimeoutConstant
move(buffer^,pchar(Str1)^,bufferlength);
Cash:=chr(Str1);
ShowMessage('cash'+cash);
move(buffer^,pchar(Str1)^,bufferlength);
for i:=0 to bufferlength-1 do
Str:=Str+chr(Str1[i]);
ShowMessage(Str);
你接收的数据长度是多少?
考虑一下
comm1.ReadTotalTimeoutConstant这个问题,不存在啊,我才发送一个检验结果,256个字符这样,就是乱码,comm1.ReadTotalTimeoutConstant=1002.
SetLength(Str1,BufferLength);
move(buffer^,pchar(Str1)^,bufferlength);
for i:=0 to bufferlength-1 do
Str:=Str+chr(Str1[i]);
ShowMessage(Str);
这个我实验看
SetLength(Str1,BufferLength);
move(buffer^,pchar(Str1)^,bufferlength);
for i:=0 to bufferlength-1 do
Str:=Str+chr(Str1[i]);
ShowMessage(Str);
这段代码有问题吧,Str:=Str+chr(Str1[i]); 这里执行不了啊
看看这样收到了什么数据procedure Tfrmmain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
rbuf:array of byte;
i: integer;
str: string;
begin
setLength(rbuf, BufferLength);
move(Buffer^, PChar(rbuf)^, BufferLength);
str:='';
for i:=0 to bufferlength-1 do
begin
str:=str+inttostr(rbuf[i]);
end;
memo1.lines.add(str);
end;