要求建立TCP连接,改传输数据的格式如:>位置,,鄂A12345,,本地,武汉,000003$GPRMC 114229.487 A 2832.2475 N 12058.3416 E 0.13 309.62 120598  *10#1 ,<
另外据说ClientSocket2.Socket.SendBuf(SendGpsMsg,SizeOf(SendGpsMsg));是传不了数据的``
总程序在下面
procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var
   tt,gpsmsg,sTdate:string;
begin
  tt:=Socket.Receivetext;
   Memo1.Text:=Memo1.Text+#10#13+tt;
   if pos('位置',tt)<>0 then
    begin
     separateData(tt);    //State:=#0;
    Move(SendGpsMsg.CarNumber,str[3],SizeOf(SendGpsMsg.CarNumber));
   // SendGpsMsg.CarNumber:=ASCtoString(IntToHex(byte(str[3])));
    gpsmsg:=str[6]+' '+str[3]+' '+str[7];
    PutReviewInfo(gpsmsg);
      if S[5]='A' then
     SendGpsMsg.GpsState:=byte('1')
    // Move(SendGpsMsg.GpsState,'1',SizeOf(SendGpsMsg.GpsState));
    else if S[5]='V' then
    SendGpsMsg.GpsState:=byte('0');
    sTdate:='20'+copy(S[12],5,2)+copy(S[12],3,2)+copy(S[12],1,2)+copy(S[4],1,6);
    Move(SendGpsMsg.Tdate,sTdate,SizeOf(SendGpsMsg.Tdate));
    SendGpsMsg.Abcd:= GetLon;   // htonl(GetLon);
   // Move(SendGpsMsg.Abcd,htonl(GetLon),SizeOf(SendGpsMsg.Abcd));
    SendGpsMsg.Abcde:= GetLat;  //htonl(GetLat);
    SendGpsMsg.Speed:= floattostr(GetSpeed);
    SendGpsMsg.Angle:= S[11];
    SendGpsMsg.Alarm:= byte('0');
    SendGpsMsg.CarState:= byte('0');    ClientSocket2.Socket.SendBuf(SendGpsMsg,SizeOf(SendGpsMsg));
    end;
end;
function TForm1.GetSpeed:double;
var
 Speed,data:string;
begin  if S[10]='' then S[10]:='0';
   try    Speed:=FloatToStr(StrToFloat(S[10])*1.852);
    data:='.';
    if StrPos(pChar(Speed),pChar(data)) = nil then
    Result:=StrToFloat(Speed)
    else Result:=StrToFloat(copy(Speed,1,pos('.',Speed)+1));
   
    except
       Result:=0.0;
    end;
end;
function TForm1.GetLon:double;
var I,J:integer;
 Lon:string;
 LonDeg:pChar;
 LonMin:pChar;
begin     Lon:=string(S[8]);
     if Lon<>'' then
     begin
     GetMem(LonDeg,Length('000'));
     StrCopy(LonDeg,pChar(string(Lon[1])));
     for I:=2 to 3 do
     begin      StrCat(LonDeg,pChar(string(Lon[I])));
      end;      GetMem(LonMin,Length('000'));
      StrCopy(LonMin,pChar(string(Lon[4])));
     for J:=5 to Length(Lon) do
     begin      StrCat(LonMin,pChar(string(Lon[J])));
      end;
     try
        Result:=StrToFloat(string(LonDeg))
              +StrToFloat(string(LonMin))/60 ;
        if S[9]='W' then Result:=Result*(-1);
     except
         Result:=0.0;
     end;
     FreeMem(LonDeg);
      FreeMem(LonMin);
     end
      else   Result:=0.0;
end;
{}
function TForm1.GetLat:double;
var I,J:integer;
 Lat:string;
 LatDeg:pChar;
 LatMin:pChar;
begin     Lat:=string(S[6]);
     if  Lat<>'' then
      begin
     GetMem(LatDeg,Length('000'));
     StrCopy(LatDeg,pChar(string(Lat[1])));
      StrCat(LatDeg,pChar(string(Lat[2])));
      GetMem(LatMin,Length('000'));
      StrCopy(LatMin,pChar(string(Lat[3])));
     for J:=4 to Length(Lat) do
     begin      StrCat(LatMin,pChar(string(Lat[J])));
      end;
     try
        Result:=StrToFloat(string(LatDeg))
              +StrToFloat(string(LatMin))/60 ;
        if S[7]='s' then Result:=Result*(-1);
     except
         Result:=0.0;
     end;
     FreeMem(LatDeg);
     FreeMem(LatMin);
     end
     else Result:=0.0;
end;
procedure TForm1.separateData(var pData:string);
var
   A,B,I:integer;
   Data:string;
begin
     A:=0;
     B:=2;
     Data:=pData;
     for I:=2 to Length(Data) do
        begin
          if (Data[I]=',') or (Data[I]='<') then
           begin
           A:=A+1;
           str[A]:=Copy(Data,B,I-B);
           B:=I+1;           end
        end;
end;
procedure TForm1.PutReviewInfo(var pInfo:string);
var
    info:string;
    I,A,B:integer;
begin      A:=0;
     B:=1;
     info:=pInfo;
     for I:=1 to Length(info) do
        begin
          if (info[I]=' ') or (info[I]='<') then
           begin
           A:=A+1;
           S[A]:=Copy(info,B,I-B);
           B:=I+1;
           end
        end;end;procedure TForm1.FormShow(Sender: TObject);
begin
  ClientSocket1.Close;
  ClientSocket1.Address:=Edit1.Text;
  ClientSocket1.Port:=StrToInt(Edit2.Text);  ClientSocket1.Open;
   ClientSocket2.Close;
  ClientSocket2.Address:=Edit3.Text;
  ClientSocket2.Port:=StrToInt(Edit4.Text);  ClientSocket2.Open;
end;procedure TForm1.ClientSocket2Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
   Label6.Caption:='连接正常';
   Timer2.Enabled:=False;
end;procedure TForm1.ClientSocket2Disconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
   Label6.Caption:='连接失败';
     ClientSocket2.Close;
     Timer2.Enabled:=True;
end;procedure TForm1.ClientSocket2Error(Sender: TObject;
  Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
  var ErrorCode: Integer);
begin
   Label6.Caption:='连接错误';    ErrorCode:=0;
    ClientSocket2.Close;
    Timer2.Enabled:=True;
end;procedure TForm1.Timer2Timer(Sender: TObject);
begin
  ClientSocket2.Close;
  ClientSocket2.Address:=Edit3.Text;
  ClientSocket2.Port:=StrToInt(Edit4.Text);  ClientSocket2.Open;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
  ClientSocket1.Close;
  ClientSocket1.Address:=Edit1.Text;
  ClientSocket1.Port:=StrToInt(Edit2.Text);  ClientSocket1.Open;
   ClientSocket2.Close;
  ClientSocket2.Address:=Edit3.Text;
  ClientSocket2.Port:=StrToInt(Edit4.Text);  ClientSocket2.Open;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
    if Edit10.Text<>'' then
   begin
    SendCarMsg.Carnumber:=Edit10.Text;
    SendCarMsg.Scompany:=Edit11.Text;
    SendCarMsg.Acode:= Edit12.Text;
    SendCarMsg.Color:= Edit13.Text;
    SendCarMsg.Seat:= Edit14.Text;
    SendCarMsg.Kind:= byte(pchar(Edit15.Text));
    SendCarMsg.GpsKind:= Edit16.Text;
    SendCarMsg.Gpsdate:= Edit17.Text;
    ClientSocket2.Socket.SendBuf(SendCarMsg,SizeOf(SendCarMsg));
    Edit10.Text:='';
   end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
   if Edit5.Text<>'' then
   begin
    SendCompanyMsg.Name:=Edit5.Text;
    SendCompanyMsg.Address:=Edit6.Text;
    SendCompanyMsg.Smen:= Edit7.Text;
    SendCompanyMsg.Phone:= Edit8.Text;
    SendCompanyMsg.Kind:= byte(pchar(Edit9.Text));
    ClientSocket2.Socket.SendBuf(SendCompanyMsg,SizeOf(SendCompanyMsg));
    Edit5.Text:='';
   end;
end;end.
谁能帮我啊``

解决方案 »

  1.   

    你的SendGpsMsg是不是一个结构体?还是只是个字符串?
    如果是字符串,不要用SendBuf,要用SendText
      

  2.   

    如果可能的话,这路数据最好用web service来传比较好
      

  3.   

    SendGpsMsg.Speed:= floattostr(GetSpeed);这句有问题!由于你发送的数据包是结构体,而string的长度是不可预知的,从而令到整个数据包的长度也是不固定的.把string改成byte[](数组大小自己根据需要确定).切忌,发送端和接收端的数据包结构务必一致.