0

After trying for a very long time .... decided to ask for help.

I'm trying to read the last line sent to a Tmemo in Delphi. I'm sending lines of code one by one to a dev. board the dev. board needs different lines of code sent to it every time. My end goal is to read back the last line the dev. board sends back.

E.G

Set ATT = 7 --->> \sent to dev. board

Dev. Board replies

O.K <----- \ received from dev. board

send next line of code.

Or

E.R.R

send "set att = 7" command again.


So far, I've got most of what I need working. I just can't get Delphi to read the last line of the tmemo.

I have tried

procedure TReaderProgrammer.Button3Click(Sender: TObject );
var 
  RxData : string; 
  LL : string; 
  ll2: system.integer;
begin
  LL:= memorxdata.lines.count.ToHexString;
  LL2:=memorxdata.Lines.Count;
  if ComPort1.Connected then
  begin
     showmessage(ll);
     ComPort1.WriteStr(memorxdata.Lines[ll2]+#13+#10);
  end;
end;

The showmessage is only there for my own reference... I know it's bouncing the data it receives back again just for reference.

The odd thing is it works sometimes, and that lines. Count bounces back letters sometimes as well so I think I'm going about this the complete wrong way...

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
Studying.com
  • 109
  • 1
  • 4
  • 10

1 Answers1

6

You're reading past the end of MemoRxData.Lines, as it's zero-based:

ll2 := MemoRxData.Lines.Count - 1;

ComPort1.WriteStr(MemoRxData.Lines[ll2] + #13#10;

(Your variable names are terrible, BTW. ll2 is simply horrendous to read. You should use meaningful, easy to read variable names instead of such terrible shortcuts.)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 3
    Ken is right. And be sure that the ll2 is alway larger than 0 – asir6 Nov 07 '14 at 23:43
  • Hi Ken , thanks cant believe I didn't try that ... was so tired last night when I posted my question and LL stands for LastLine I had the Variable name as LastLine but was getting tired of typing LastLine all the time while trying different options so changed it to LL but its back to last line now . thank you very much. – Studying.com Nov 08 '14 at 10:10
  • Hi Sertac , .Lines[0] is the first line in the memo as it counts 0,1,2,3,4, --- so on. – Studying.com Nov 08 '14 at 10:13
  • @Chewy The IDE can auto-complete variable names (and other identifiers) for you. If you type Ctrl+Space at any point, it will bring up a list, and as you type it narrows it down. You can use the up and down arrows to select something, or press Enter to accept the selected identifier, or just keep typing. So, for example, you could type 'La', Ctrl+Space, Enter to fill in 'LastLine'. Ditto for method names after you type a dot, class names, etc. There are options for sorting the results etc in the IDE Options dialog. – David Nov 08 '14 at 13:57
  • @Chewy - It still can be the last line, if there's only one line in the memo, no? – Sertac Akyuz Nov 08 '14 at 14:30
  • @Sertac - yes from what I have seen even if there is only one line in the memo it will still see it as the last. – Studying.com Nov 08 '14 at 15:39