串口接受,发送问题(180分)

  • 主题发起人 主题发起人 tbase
  • 开始时间 开始时间
T

tbase

Unregistered / Unconfirmed
GUEST, unregistred user!
我有A B两台机器,A机器发送一串字符,

B机器得到buffer接受格式 array of char,得到,经过inttohex函数后得到字符串为 00 08 00 80 80.....
可是我用async32写的程序,例如
var
s:string;
procedure xxx.....
begin
s:= #0+#08+#00+144+#144+#0;
...
end;
1 然后发送可以吗,要不要加+#13+#10
2 为什么,我发的时候老是 DSR off
3 也就是说,我用A 机器发 而B器没反应.(我用同一个程序接受),而别人的软件就可以发.上面的
那串字符.我也可以收到.

 
string类型的字符串同C语言的字符串是不相同的。
将你的程序修改一下:
var
s: <font color =red>array[0..1023] of char;</font>
procedure xxx.....
begin
s:= #0+#08+#00+144+#144+#0;
...
end;

 
s:= #0+#08+#00+144+#144+#0;
第一个字符就是0,不就成了空字符串了吗!修改意见同上
 
sorry我的描述不够清晰
定义时就是 array[0..9999] of char;
#0 我知道是空字符,可是length的返回直正确。
有发送及接受的简单源码吗 在加100分
 
唉,只好把我这套老代码再发一次,红字部分为串口通讯的关键设置。
unit gps2;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, StdCtrls,
ComCtrls, Buttons, dialogs;

const MAXBLOCK = 2000;

var
CommHandle : THandle;
Connected : Boolean;
inchar :char;
vid :string;//协议报头
lxx,lyy :string;//截取的GPS经纬度坐标 lxx:纬度,lyy经度
number :string;//1或2,GPS已经定位;0,GPS尚未定位
utc :string;//时间
gpstext :string;//存盘所用的GPS信息($GPGGA,utc,lxx,N,lyy,E)
star :string;//定位星数

diaodlon ,diaodlat,oldlon, oldlat :double;
diaodflag :integer;

//
p: array[0..1] of TPOINT;
p1: TPOINT;
fx,fy :single;
x,y :integer;
j :integer;
old_p :Tpoint;
first_p :Boolean;
//

procedure getutc(var ginfo:string;var gutc:string);
procedure getvid(var ginfo:string;var gvid:string);
procedure getxy(var ginfo:string;var gxx,gyy:string);
procedure getnumber(var ginfo:string;var gnum:string);
procedure getstar(var ginfo:string;var gstar:string);

procedure gps2receivedata;
procedure gps2init;

implementation

uses mainshow,dll_shi,ts4;

procedure getutc(var ginfo:string;var gutc:string);
begin
if (pos('$GPGGA',ginfo)>0) then
gutc :=copy(ginfo,pos('$GPGGA',ginfo)+7,6);
end;

procedure getvid(var ginfo:string;var gvid:string);
begin
if (pos('$GPGGA',ginfo)>0) then
gvid :=copy(ginfo,pos('$GPGGA',ginfo),7);
end;

procedure getnumber(var ginfo:string;var gnum:string);
begin
if (pos(',E,',ginfo)>0) then
gnum:=copy(ginfo,pos(',E,',ginfo)+3,1);
end;

procedure getstar(var ginfo:string;var gstar:string);
begin
if (pos(',E,',ginfo)>0) then
gstar:=copy(ginfo,pos(',E,',ginfo)+5,2);
end;

procedure getxy(var ginfo:string;var gxx,gyy:string);
var
test :integer;
begin
if (pos('$GPGGA,',ginfo)>0)and(pos(',N,',ginfo)>0)
and(pos(',E,',ginfo)>0) then
begin
delete(ginfo,1,pos('$GPGGA',ginfo)+6);

if ((pos(',N,',ginfo)>0)and(pos(',E,',ginfo)>0)) then
begin
if (pos(',',ginfo)=1) then delete(ginfo,1,1)
else delete(ginfo,1,pos(',',ginfo));

test :=pos(',N,',ginfo);

gxx :=copy(ginfo,1,test-1);


if (pos(',E,',ginfo)>test) then
gyy :=copy(ginfo,test+3,pos(',E,',ginfo)-3-test);
end;
end;//if (pos('$GPGGA,',ginfo)>0) then
end;
<font color=red>
procedure gps2init;
var
CommTimeOut : TCOMMTIMEOUTS;
DCB : TDCB;
fRetVal : Boolean;
begin
CommHandle := CreateFile(PChar('com2'),GENERIC_READ or GENERIC_WRITE,0,nil,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,0);
if CommHandle = INVALID_HANDLE_VALUE then
begin
showmessage('GPS串口连接失败');
exit;
end;//if CommHandle = INVALID_HANDLE_VALUE then

CommTimeOut.ReadIntervalTimeout := MAXDWORD;
CommTimeOut.ReadTotalTimeoutMultiplier := 0;
CommTimeOut.ReadTotalTimeoutConstant := 0;
SetCommTimeouts(CommHandle, CommTimeOut);

GetCommState(CommHandle,DCB);
DCB.BaudRate := 4800;
DCB.ByteSize := 8;
DCB.Parity := NOPARITY;
DCB.StopBits := ONESTOPBIT;
fRetVal := SetCommState(CommHandle, DCB);
if (fRetVal) then
begin
Connected := TRUE;
mainshow.text_flag :=true;
first_p :=true;//
mainshow.Form1.Timer_gps.Interval :=1000;
mainshow.Form1.Timer_gps.Enabled :=true;
end// if (fRetVal) then begin
else
begin
Connected := FALSE;
CloseHandle(CommHandle);
end;
end;
</font>
//接收数据
procedure gps2receivedata;
var
dwErrorFlags,dwLength,read_len : DWORD;
ComStat : PComStat;
fReadStat : Boolean;

InChar : array[0..999]of char;
abin :string;
i:integer;

dd,mm :string;
test_star :integer;
begin
{ Place thread code here }
begin
GetMem(ComStat,SizeOf(TComStat));
ClearCommError(CommHandle, dwErrorFlags, ComStat);

if (dwErrorFlags > 0) then
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR));
// return 0;

dwLength := ComStat.cbInQue;
if (dwLength>0) then
begin
if dwlength>=1000 then
fReadStat := ReadFile(CommHandle, InChar, 1000,read_len, nil)
else
fReadStat := ReadFile(CommHandle, InChar, dwLength,read_len, nil);

if (fReadStat) then
begin
i:=0;
abin :='';

while i0) then
begin
try
getvid(abin,vid);
getutc(abin,utc);
getnumber(abin,number);
getstar(abin,star);
getxy(abin,lxx,lyy);
except
showmessage('please wait... ...');
sleep(50);
end;//try getxy(abin,lxx,lyy);

try
test_star :=strtoint(star);
except
test_star :=0;
end;//try

if ((number='1')or(number='2')or(number='0')) then
begin
try
dd :=floattostr(trunc(strtofloat(lyy)/100.0));
mm :=floattostr((strtofloat(lyy))/100.0-strtofloat(dd));
diaodlon :=strtofloat(dd)+strtofloat(mm)*5/3.0;

dd :=floattostr(trunc(strtofloat(lxx)/100.0));
mm :=floattostr((strtofloat(lxx))/100.0-strtofloat(dd));
diaodlat :=strtofloat(dd)+strtofloat(mm)*5/3.0;

gpstext :=vid+utc+','+lxx+',N,'+lyy+',E,';
if mainshow.text_flag then writeln(mainshow.textfile,gpstext);

if ((diaodlon>0) and (diaodlat>0))then
begin
if ((number='1') or (number='2') or (test_star>4)) then
begin
fx :=diaodlon;
fy :=diaodlat;
oldlon :=diaodlon;
oldlat :=diaodlat;
ts4.ts_x :=0;
ts4.ts_y :=0;
if number='2' then
begin
mainshow.Form1.Label2.Caption :='差分管用了耶';
mainshow.Form1.Label2.Visible :=true;
end;

end
else
begin
oldlon :=oldlon+ts4.ts_x;
oldlat :=oldlat+ts4.ts_y;
fx :=oldlon;
fy :=oldlat;

// +' ts_y:'+floattostr(ts4.ts_y);
ts4.ts_x :=0;
ts4.ts_y :=0;
end;

mainshow.Form1.StatusBar1.Panels[0].Text := floattostr(fx);
mainshow.Form1.StatusBar1.Panels[1].Text := floattostr(fy);

mainshow.form1.Canvas.Brush.Color :=rgb(0,192,192);
Getxy_JW( pGisShow, psid(@X), psid(@Y), fx, fy );
p1.x := X;
p1.y := Y;

windows.DPtoLP(mainshow.form1.canvas.Handle, p1, 1 );
windows.SetRop2(mainshow.form1.Canvas.Handle,R2_XORPEN);

if not first_p then
begin
mainshow.form1.Canvas.Ellipse(old_p.x-5,old_p.y-5,old_p.x+5,old_p.y+5);
mainshow.form1.Canvas.Ellipse(p1.x-5,p1.y-5,p1.x+5,p1.y+5);
end
else
begin
mainshow.form1.Canvas.Ellipse(p1.x-5,p1.y-5,p1.x+5,p1.y+5);
first_p :=false;
end;

old_p:=p1;
end;

except

sleep(10);
end;//try
end;
end;
end
else
begin
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR));
end;//if (fReadStat) then
end;
FreeMem(ComStat);
end;{while}
end;

end.
 
虽然没有解决,
但为了感谢您的支持.分配如下.
 

Similar threads

回复
0
查看
864
不得闲
S
回复
0
查看
829
SUNSTONE的Delphi笔记
S
S
回复
0
查看
788
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部