Hi!
Ich arbeite mir der vielleicht bekannten Komponente 'comm_async' um Daten, die mein Microcontroler an den ComPort sendet, an Delphi zu übergeben. Die Daten die jener MC sendet sind $ldx$rdx$lix$rix (x ist ein integer zwischen 0 und 255) und werden aller 200ms gesendet.
Wenn ein Zeichen ankommt (OnCharRecieve), wird es der Variable buf übergeben, welche ein char ist.
Die Daten werden, schreibt man sie bspweise in ein Memofeld, einwandfrei übertragen!
Nur wenn ich die Daten weiterverarbeiten will, gibt es jede menge fehler:
Hier der Code den ich zum trennen und schreiben in eine dynamische array benutze:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64:
| [...] type TTable = array of array[1..4] of integer;
var Form1: TForm1; acs: string; i,j : integer; robotable : TTable; ctrl:boolean;
[...]
procedure TForm1.Comm1CharReceived(Sender: TObject; cbInQue: Cardinal); var buf:char; tempacs1:string; tempacs2:integer; begin COMM1.Read(@buf,1); if buf='$' then ctrl:=true; if (buf<>'$') and ctrl then acs:=acs+buf;
if (buf='$') and (acs<>'') and ctrl then begin tempacs1:=acs[1]+acs[2];
case length(acs) of 3:tempacs2:=StrToInt(acs[3]); 4:tempacs2:=StrToInt(acs[3]+acs[4]); 5:tempacs2:=StrToInt(acs[3]+acs[4]+acs[5]); end;
if (tempacs1 <> 'ld') and (tempacs1 <> 'rd') and (tempacs1 <> 'li') and (tempacs1 <> 'ri') then begin end else begin SetLength(robotable,j); if tempacs1 = 'ld' then robotable[1,j]:=tempacs2; if tempacs1 = 'rd' then robotable[2,j]:=tempacs2; if tempacs1 = 'li' then robotable[3,j]:=tempacs2; if tempacs1 = 'ri' then robotable[4,j]:=tempacs2;
memo.SelText:= IntToStr(robotable[1,j])+' - '+ IntToStr(robotable[2,j])+' - '+ IntToStr(robotable[3,j])+' - '+ IntToStr(robotable[4,j])+' -- ';
inc(i); if i=4 then begin inc(j); i:=0; end; end; acs:=''; ctrl:=false; end; end; |
Wenn ich es nach einem neustart ausführe, schreibt er manchmal die richtigen, oft aber auch die falschen zahlen in die tabelle (oder zeigt sie zumindest beim ausgeben), und stürzt dann ab. Führe ich es danach nochmal aus, zeigt er nur noch wilde zeichen, und man kann sich außerdem irgendwie vor allen mgl fehlern nicht mehr retten...
bitte helft mir,
thx
Moderiert von
raziel: Code- durch Delphi-Tags ersetzt