+ -
当前位置:首页 → 问答吧 → Delphi SPComm 自发自收出现乱码,求助。。。

Delphi SPComm 自发自收出现乱码,求助。。。

时间:2011-12-04

来源:互联网

我这里设置了一个memo,一个edit,edit输入的可以认作是ASCII也可以认作是16进制,接收到的数据可以直接在memo里以ASCII显示,可以认为是16进制数,转换成相应的字符,再在memo里显示。

但是,目前仅就ASCII发ASCII收,就碰到了很大的问题,[是直接把RXD和TXD连起来的。
收发如下:



SPComm的各项设置如下:



收发procedure如下:


串口接收到数据的处理进程
Delphi(Pascal) code

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
  BufferLength: Word);
var
    rbuf:array[1..256] of char;
    rcvstring:string;
    i,j:integer;
begin
    SetLength(rcvstring,BufferLength);
    move (buffer^,rbuf,Bufferlength);
    if memochar.Checked then
    for i:=1 to BufferLength do
            begin
            rcvstring[i]:=rbuf[i];
            end;
    if memo16.Checked then
      begin
        rcvstring:='';
        for j:=1 to BufferLength do
            begin
            rcvstring:=rcvstring+inttohex(integer(rbuf[j]),2);
            end;
      end;
   memo.Lines.Add('Receive: '+rcvstring);
end;



串口发数据的procedure
Delphi(Pascal) code
procedure TForm1.sendClick(Sender: TObject);
var
    sbuf:array[1..128] of integer;
    command,sendtext:string;
    flag:boolean;
    i,j:integer;
begin
if  portopen then
begin
    command:='';
    command:=edit.Text;
    if edit16.Checked then
        begin
        for i:=1 to Length(edit.Text) div 2 do
            begin
            try
            sbuf[i]:= StrToInt('$'+command[2*i-1]+command[2*i]);
            except
            on Exception : EConvertError do
                begin
                ShowMessage(Exception.Message);
                exit;
                end;
            end;
            end;
        command:='';
        for j:=1 to Length(edit.Text) div 2 do
            command:=command+Char(sbuf[j] mod 256);
        end;

    sendtext:=command;
    if autoreturn.Checked then
        sendtext:=sendtext+#13#10;

    flag:=Comm1.WriteCommData(@sendtext,Length(sendtext));
    if flag=false then
        begin
        showmessage('Êý¾Ý·¢ËÍʧ°Ü');
        exit;
        end;
    memo.Lines.Add('Send: '+command);
end
else
showmessage('Çë´ò¿ª´®¿Ú');
end;

问题出在哪里,为什么会乱码。。。。

作者: veronica_rabbit   发布时间: 2011-12-04

乱码的图贴不出来,形容一下吧,
就是比如我发送helloworld,然后就会收到乱七八糟,我都不认识的中文字符
但是发相同内容,收到的乱码也是相同的。。。

作者: veronica_rabbit   发布时间: 2011-12-04

热门下载

更多