TMemo lazarus / 61-Beginners ---------------------------------------------------------------------- BLL · 2016-06-20 15:44 Hi, I am adding data from a USB/serial converter to a TMemo. The unit connected (a Chronverter) will respond to sent commands. = + puts in command mode, Thhnnss + sets its time etc. When I issue commands from my program, the Memo shows what I expect. I then tried to extract the Memo's contents to a String, using the code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sTemp := StringReplace(Copy(Memo1.Lines.Text,1,Length(Memo1.Lines.Text)-1),  #13#10,QuotedStr(''),[rfReplaceAll]); sTemp is empty, despite there being contents visible! In order to get the text, I had to put the above code in the Memo's change function. Why please? Brian ---------------------------------------------------------------------- Bart · 2016-06-20 15:55 Not sure what you want to achieve but: My memo has Code: [Select]foo bar last sTemp wil be: Code: [Select]foo''bar''''las Why Code: [Select]Copy(Memo1.Lines.Text,1,Length(Memo1.Lines.Text)-1) If you want to get rid of the last trailing line-ending in the memo use TrimLeft(Memo1.Lines.Text). Now you loose the last character of the last line ("las" instead of "last" in my example) if the memo does not end in a trailing empty line. Also use LineEnding instead of #13#10, it is portable (and it makes for better reading of the code). Bart ---------------------------------------------------------------------- rvk · 2016-06-20 16:01 Was this for the same code as in this topic: http://forum.lazarus.freepascal.org/index.php/topic,33018.msg213328.ht ml#msg213328 I thought you had solved that? When I look at that code you think that at the end of updateChronverter() the TMemo had content. It doesn't. But because the program is still running the content is filled in right AFTER the updateChronverter() function. But because we're talking about a split second here you don't notice it. If you want to make sure your TMemo is filled in do the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Showmessage('Memo = ' + Memo1.Lines.Text);sTemp := StringReplace(Copy(Memo1.Lines.Text,1,Length(Memo1.Lines.Text)-1),  #13#10,QuotedStr(''),[rfReplaceAll]);Can you tell us what the showmessage gives you? If it's also empty (Memo =) then you need to show us you complete code again because you didn't fix the code from your other topic. ---------------------------------------------------------------------- BLL · 2016-06-20 19:12 Hi both I have tried both approaches and the results are interesting! With either method, on 1st click on  the CON button (this is the only button function I have modified), the memo gets the text, but the Edit box doesn't. If I put a ShowMessage prior to the Copy command  or StringReplace then on 1st click, ShowMessage is empty, but the Edit box now gets the text! Thereafter, the Show Message is populated. This is only intended as a rough test program before working code goes into the main app. I emphasise that all works if I use the OnChange function to process the transfer of the data) Thanks both for your interest. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit main; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  ExtCtrls, Buttons, SdpoSerial, crt; { TMainForm } type  TMainForm = class(TForm)    SetEU: TBitBtn;    COn: TBitBtn;    COff: TBitBtn;    SetDT: TBitBtn;    SetUK: TButton;    Result: TEdit;    ExitBtn: TButton;    Label1: TLabel;    Memo1: TMemo;    Comport3: TSdpoSerial;    Timer1: TTimer;    procedure COffClick(Sender: TObject);    procedure COnClick(Sender: TObject);    procedure SetDTClick(Sender: TObject);    procedure SetEUClick(Sender: TObject);    procedure SetUKClick(Sender: TObject);    procedure ExitBtnClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure Memo1Change(Sender: TObject);    procedure OnPacketReceive1(Sender: TObject);    procedure updateChronverter;  private    { private declarations }  public    { public declarations }  end; var  MainForm: TMainForm;  PortCom3: TSdpoSerial;  dataString, RxData, sTemp: String;  TimeToSend:String;  Thismoment:TDateTime; implementation {$R *.lfm} { TMainForm }procedure TMainForm.FormCreate(Sender: TObject);begin DefaultFormatSettings.DateSeparator := '/'; DefaultFormatSettings.ShortDateFormat := 'dd-mm-yyyy'; //set up port PortCom3 := TSdpoSerial.Create(nil); PortCom3.BaudRate := br__4800; PortCom3.DataBits := db8bits; PortCom3.Parity := pNone; PortCom3.StopBits := sbOne; PortCom3.FlowControl := fcNone; PortCom3.OnPacket := @OnPacketReceive1; PortCom3.StopString := '>'; //symbolic name for Chronverter PortCom3.Device := '/dev/DCF';end; procedure TMainForm.Memo1Change(Sender: TObject);//all works if I use this code//var  //dateString: String;begin {sTemp := StringReplace(Copy(Memo1.Lines.Text,1,Length(Memo1.Lines.Text)-1),  LineEnding,QuotedStr(''),[rfReplaceAll]); TimeToSend := 'Time: ' + FormatDateTime('hh:nn:ss', ThisMoment); dateString := 'Date: ' + FormatDateTime('ddd DD/MM/YY', Date); Result.Text := ''; Result.Text := sTemp;}end; procedure TMainForm.OnPacketReceive1(Sender: TObject);begin RxData := PortCom3.ReadPacket; dataString := RxData; Memo1.Lines.Add(dataString);end;procedure TMainForm.ExitBtnClick(Sender: TObject);begin Close;end; procedure TMainForm.SetEUClick(Sender: TObject);begin PortCom3.Open; Memo1.Clear; //set Region to UK //put device into command mode PortCom3.WriteData('=' + #13); Delay(100);  PortCom3.WriteData('REU' + #13);  Delay(100); //exit command mode PortCom3.WriteData('X' + #13); Delay(100); Result.Text := ''; Result.Text := dataString;end; procedure TMainForm.SetDTClick(Sender: TObject);begin  updateChronverter;end; //NB. This is the only function I have modifiedprocedure TMainForm.COnClick(Sender: TObject);var  dateString: String;begin PortCom3.Open; //Memo1.Clear; //set Region to UK //put device into command mode PortCom3.WriteData('=' + #13); Delay(100);  PortCom3.WriteData('CON' + #13);  Delay(100); //exit command mode PortCom3.WriteData('X' + #13); Delay(100);// ShowMessage(Memo1.Lines.Text); sTemp := StringReplace(Copy(Memo1.Lines.Text,1,Length(Memo1.Lines.Text)-1),  LineEnding,QuotedStr(''),[rfReplaceAll]); //empty on 1st click, then OK TimeToSend := 'Time: ' + FormatDateTime('hh:nn:ss', ThisMoment); dateString := 'Date: ' + FormatDateTime('ddd DD/MM/YY', Date);// Result.Text := ''; //Result.Text := sTemp; //sTemp :=Copy(Memo1.Lines.Text,1,Length(Memo1.Lines.Text)-1);  Result.Text := ''; Result.Text := sTemp;end; procedure TMainForm.COffClick(Sender: TObject);begin PortCom3.Open; Memo1.Clear; //set Region to UK //put device into command mode PortCom3.WriteData('=' + #13); Delay(100);  PortCom3.WriteData('COFF' + #13);  Delay(100); //exit command mode PortCom3.WriteData('X' + #13); Delay(100); Result.Text := ''; Result.Text := dataString;end; procedure TMainForm.SetUKClick(Sender: TObject);begin PortCom3.Open; Memo1.Clear; //set Region to UK //put device into command mode PortCom3.WriteData('=' + #13); Delay(100);  PortCom3.WriteData('RUK' + #13);  Delay(100); //exit command mode PortCom3.WriteData('X' + #13); Delay(100); Result.Text := ''; Result.Text := dataString;end; procedure TMainForm.updateChronverter;begin try  PortCom3.Open; except; end; if PortCom3.DevicePresent = false then  Label1.Caption := 'Port not found'; Memo1.Clear; //put device into command mode PortCom3.WriteData('=' + #13); Delay(100); //send system date PortCom3.WriteData('D' + FormatDateTime('DDMMYY', Date) + #13); Delay(100); //send system time now ThisMoment := Now; TimeToSend := FormatDateTime('hhnnss', ThisMoment); PortCom3.WriteData('T' + TimeToSend + #13); Delay(100); //exit command mode PortCom3.WriteData('X' + #13); Delay(100); Result.Text := ''; Result.Text := dataString; end;//end;end. ---------------------------------------------------------------------- rvk · 2016-06-20 19:20 I think the problem lies in these snippets (in all procedures): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  PortCom3.WriteData('X' + #13); Delay(100); Result.Text := ''; Result.Text := dataString;You write a command to PortCom3, do a delay and directly expect dataString to be filled in in the background. I don't think the communication works like that. You delay just stops your program and nothing happens in the background. dataString is still empty, and you assign that empty string to Result.Text. Then the event ends and Windows can process the given command and go from there. AFTER that your dataString gets filled in by OnPacketReceive1. But that's well AFTER you've given the command and the event has ended. You need to split the sending of the command and the reading of the dataString. These can't happen in the same event. For example... if you set Result.Text in OnPacketReceive1() it should work (but don't expect to send a command and have a the dataString filled in the background after delay()). ---------------------------------------------------------------------- BLL · 2016-06-20 20:37 Ah, that makes sense. I found a delay was necessary between commands or mall you got back was gibberish! Well, I'll just stick with using the OnChange method as it seems completely reliable. Good to know at last what was causing the problem though. Thanks both for your help. Brian