Delphi SPComm 自发自收出现乱码,求助。。。
我这里设置了一个memo,一个edit,edit输入的可以认作是ASCII也可以认作是16进制,接收到的数据可以直接在memo里以ASCII显示,可以认为是16进制数,转换成相应的字符,再在memo里显示。
但是,目前仅就ASCII发ASCII收,就碰到了很大的问题,[是直接把RXD和TXD连起来的。
收发如下:
SPComm的各项设置如下:
收发procedure如下:
串口接收到数据的处理进程
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 TForm1.sendClick(Sender: TObject);var sbuf:array[1..128] of integer; command,sendtext:string; flag:boolean; i,j:integer;beginif portopen thenbegin 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);endelseshowmessage('Çë´ò¿ª´®¿Ú');end;