自己写的一个程序里边摘出来的,SPCOMM接收串口送进来的16进制数据,是数传GPS系统的代码。希望有用
procedure TBaseStation.TReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
var
gbuf:array[1..128] of byte;
i:integer;
tmpi:Cardinal;
gbyte:byte;
valid,tmpstr,sqlstr:String;
GPSInsert,SqlOpr:TADOQuery;
ADateAndTime: TDateTime;
AYear, AMonth, ADay, AHour, AMinute, ASecond: Word;
begin
//立即获得当前缓冲区数据
if BufferLength<>25 then exit;
for i:=1 to 128 do
begin
gbuf:=0;
end;
Move(Buffer^, gbuf, BufferLength);
if (gbuf[1]<>$48) or (gbuf[2]<>$53) or (gbuf[3]<>$54) then exit;
if gbuf[5] = $41 then valid := '1' else valid := '0';
//处理移动台ID
i:=gbuf[6]*256 + gbuf[7];
SqlOpr := TADOQuery.Create(nil);
SqlOpr.Connection := GTKCENTRE.GPSBuffLinker;
SqlOpr.SQL.Text :='select * from MobileStation where StationID=' + IntToStr(i);
SqlOpr.Open;
if SqlOpr.Eof then//如果移动台事先未注册,则忽略其信息
begin
SqlOpr.Close;
SqlOpr.Free;
Exit;
end;
//如果移动台为在其正确的注册基地台上回传信息,则忽略
if SqlOpr.FieldByName('BASEID').AsInteger <> (Sender as TBaseStation).BASEID then
begin
SqlOpr.Close;
SqlOpr.Free;
Exit;
end;
SqlOpr.Close;
SqlOpr.Free;
sqlstr :='insert into GPSbuff values (''';
//处理时间
if valid = '1' then
begin
tmpi:=gbuf[12]*256*256*256 + gbuf[11]*256*256 + gbuf[10]*256 + gbuf[9];
gbyte := (tmpi shr 26) and 63; //取得日数
ADay:=gbyte;
//if gbyte<10 then tmpstr:='0' + IntToStr(gbyte) else tmpstr:= IntToStr(gbyte);
gbyte := (tmpi shr 22) and 15; //取得月数
AMonth:=gbyte;
//if gbyte<10 then tmpstr:='0' + IntToStr(gbyte) + tmpstr else tmpstr:= IntToStr(gbyte) + tmpstr;
gbyte := (tmpi shr 17) and 31; //取得年数
AYear:=gbyte+2000;
//if gbyte<10 then tmpstr:='200' + IntToStr(gbyte) + tmpstr else tmpstr:= '20' + IntToStr(gbyte) + tmpstr;
gbyte := (tmpi shr 11) and 63; //取得秒数
ASecond:=gbyte;
//if gbyte<10 then tmpstr1:='0' + IntToStr(gbyte) else tmpstr1:= IntToStr(gbyte);
gbyte := (tmpi shr 5) and 63; //取得分数
AMinute:=gbyte;
//if gbyte<10 then tmpstr1:='0' + IntToStr(gbyte) + tmpstr1 else tmpstr1:= IntToStr(gbyte) + tmpstr1;
gbyte := tmpi and 31; //取得时数
AHour:=gbyte;
//if gbyte<10 then tmpstr1:='0' + IntToStr(gbyte) + tmpstr1 else tmpstr1:= IntToStr(gbyte) + tmpstr1;
//tmpstr:=tmpstr+tmpstr1;
ADateAndTime := EncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, 0);
ADateAndTime :=ADateAndTime + 0.333333;//转换为UMT+8时区
DateTimeToString(tmpstr, 'YYYYMMDDHHMMSS', ADateAndTime);
tmpstr:=tmpstr+ FormatDateTime('zzz', now);
end
else
begin
tmpstr:=tmpstr+ FormatDateTime('YYYYMMDDHHMMSSZZZ', now);
end;
sqlstr := sqlstr + tmpstr +''',';
//处理移动台ID
i:=gbuf[6]*256 + gbuf[7];
sqlstr:=sqlstr + inttostr(i) +',''';
//处理纬度
i:=gbuf[13];
tmpstr:=intTostr(i);
if i<10 then tmpstr := '0' + tmpstr;
i:=gbuf[14];
if i<10 then tmpstr := tmpstr + '0' + inttostr(i) else tmpstr:=tmpstr + inttostr(i);
i:=gbuf[15]+gbuf[16]*256;
tmpstr:=tmpstr + '.' + inttostr(i);
sqlstr := sqlstr + tmpstr +''',''';
//处理经度
i:=gbuf[17];
tmpstr:=inttostr(i);
i:=gbuf[18];
if i<10 then
begin
tmpstr := tmpstr + '0' + inttostr(i);
end
else
begin
tmpstr := tmpstr + inttostr(i);
end;
i:=gbuf[19] + gbuf[20]*256;
tmpstr:=tmpstr + '.' + inttostr(i);
sqlstr := sqlstr + tmpstr +''',''';
//处理速度
i:=gbuf[21]+gbuf[22]*256;
tmpstr:=inttostr(i);
i:=length(tmpstr);
if i>1 then insert('.', tmpstr, i-1);
sqlstr := sqlstr + tmpstr +''',''';
//处理角度
i:=gbuf[23]+gbuf[24]*256;
tmpstr:=inttostr(i);
i:=length(tmpstr);
if i>1 then insert('.', tmpstr, i-1);
sqlstr := sqlstr + tmpstr +''','''+ valid +''')';
//写入定位信息表
GPSInsert:=TADOQuery.Create(nil);
GPSInsert.Connection := GTKCENTRE.GPSBuffLinker;
GPSInsert.SQL.Text := sqlstr;
GPSInsert.ExecSQL;
GPSInsert.Free;
end;