LazSerial with no form lazarus / 21-General ---------------------------------------------------------------------- arneolav · 2023-08-15 20:54 Hi! With use of LazSerial I'm reading some sensorvalues from ArduinoMega. This is working great. But I have to run LazSerial in a thread 24/7 and want to avoid the form. Therefore I should like a LazSerial with no GUI. I'm looking at unit LazSerial and can get it to compile without form. Before i'm spending more time on this; May be there are some other/better solution? Suggestions? ---------------------------------------------------------------------- tetrastes · 2023-08-15 21:02 Original synaser.pas from https://sourceforge.net/p/synalist/code/HEAD/tree/trunk/ ---------------------------------------------------------------------- jamie · 2023-08-15 21:14 There is the SERIAL unit. Run that in a thread and post results to a message pump to the main thread via a USER message. ---------------------------------------------------------------------- arneolav · 2023-08-15 21:25 Thanks both of you, I'll do a closer look it! ---------------------------------------------------------------------- arneolav · 2023-08-16 20:39 Hi! I'v done a simple test of Serial, but seems I'm missing something. The handle seems ok, but no response on Serial.SerRead. Repeat..until does not stop. Comport is not available to others but after close, LazSerial and same setup, comport, values are recieved ok.) 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 procedure TForm1.ButOpenClick(Sender: TObject); var ComPort  : String;begin  ComPort:= 'COM3';  SerialHandle := Serial.SerOpen(ComPort);  Sleep(1000);  LabelHandle.Caption:= inttostr(SerialHandle);  Serial.Se rSetParams(SerialHandle,57600,8,NoneParity,1,[]);end; procedure TForm1.ButReadClick(Sender: TObject);Var Buf : String;  Count: Integer;begin  Buf:='';  repeat    Count:= Serial.SerRead(SerialHandle, Buf, 10000);    Incomingstring := Incomingstring  +  Buf;  until Count>10;    Label1.Caption:=(inttostr(Count));    Memo1.Lines.Add(Incomingstring);end; ---------------------------------------------------------------------- jamie · 2023-08-16 21:06 if you are going to use a STRING as the return storage, you need to make sure it has storage. SetLength(S, 10000); and when passing it to the SerRead,  the Buffer is like this    SetLengthLength(Buf, 10000);    SetLength(Buf, SerRead(SerialHandle, PChar(Buf)^, 10000));      So you can check the length of the string now because it should be shorter than 10000. ---------------------------------------------------------------------- Jurassic Pork · 2023-08-16 22:08 Hello,  You can use synaser for this. if you are on windows there is a small windows daemon project (windows service) to listen COM3 serial port for gps information here The incoming data are written in a log file. Friendly, J.P ---------------------------------------------------------------------- arneolav · 2023-08-16 22:24 Thanks Jurassic, I'v look at it. ------------- Tested a little more on Serial. This does work but only a few records and the Access violation. Tried renew buffer but seems no help. 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 procedure TForm1.ButReadClick(Sender: TObject);Var Buf, s : String;  Count, i: Integer;begin  Buf:='';  SetLength(Buf, 10000);  SetLength(Buf, SerRead(SerialHandle, PChar(Buf)^, 10000) );  repeat    Count:= Serial.SerRead(SerialHandle, Buf, 10000);    Incomingstring := Incomingstring  +  Buf;     if (Incomingstring[Length(Incomingstring)] = #10 )    then                        //Assume end of block is recieved    begin       for i := Low(Incomingstring) to High(Incomingstring) do      begin        if ((Incomingstring[i] <> AnsiChar($0A)) and (Incomingstring[i] <> AnsiChar($0D))) then        s := s + Incomingstring[i];         if ((Incomingstring[i] = AnsiChar($0D)) and (s <> '')) then        begin           // DecodeRecord(s);                          // <<----------------------- Main decode            Memo1.Lines.add(s);                    // Display the records for inspection            s := '';        end;    end;       Incomingstring := '';      Buf:='';    //  SetLength(Buf, 0);    //  SetLength(Buf, 10000);    //  SetLength(Buf, SerRead(SerialHandle, PChar(Buf)^, 10000) );      //Serial.SerFlush(SerialHandle);         // Empty buffer, avoid repeat of same input      Sleep(1500);                             // Do not read too often    end;  until Count>10;end; Attach: Result ---------------------------------------------------------------------- jamie · 2023-08-16 22:43 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 procedure TForm1.ButReadClick(Sender: TObject);Var Buf, s : String;  Count, i: Integer;begin  Repeat   SetLength(Buf, 10000);   SetLength(Buf, SerRead(SerialHandle, PChar(Buf)^, 10000) );   Count := Length(Buf);   Incomingstring := Incomingstring  +  Buf;     if (Incomingstring[Length(Incomingstring)] = #10 )    then                      //Assume end of block is recieved    begin       for i := Low(Incomingstring) to High(Incomingstring) do      begin        if ((Incomingstring[i] <> AnsiChar($0A)) and (Incomingstring[i] <> AnsiChar($0D))) then          s := s + Incomingstring[i];         if ((Incomingstring[i] = AnsiChar($0D)) and (s <> '')) then        begin           // DecodeRecord(s);                          // <<----------------------- Main decode            Memo1.Lines.add(s);                  // Display the records for inspection            s := '';        end;      end;       Incomingstring := '';      Buf:='';  //  SetLength(Buf, 0);    //  SetLength(Buf, 10000);    //  SetLength(Buf, SerRead(SerialHandle, PChar(Buf)^, 10000) );      //Serial.SerFlush(SerialHandle);         // Empty buffer, avoid repeat of same input      Sleep(1500);                             // Do not read too often    end;  until Count>10;end;  You need to reset your buffer each cycle. ---------------------------------------------------------------------- arneolav · 2023-08-17 18:56 Thanks jamie, that did the trick! There are an important difference from LazSerial to Serial: LazSerial is reporting a long string containing CR/LF and then need to scan the string to split into records. Serial is reporting separate records; More simple 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 procedure TForm1.ButReadClick(Sender: TObject);Var Buf, s : String;  Count, i: Integer;begin  Repeat   SetLength(Buf, 10000);   SetLength(Buf, SerRead(SerialHandle, PChar(Buf)^, 10000) );   if Length(Buf) > 0 then                        // Avoid diplay empty Buf     Memo1.Lines.add(Buf);                        // Display the records for inspection     //if Length(Buf) > 0 then                        // Avoid decode empty Buf      //DecodeRecord(Buf);                         // <<----------------------- Main decode    Sleep(3000);                               // Do not read too often  until xClose;end; This code have to run 24/7, but should have a nice way to stop the Repeat, Ideas? ---------------------------------------------------------------------- TRon · 2023-08-17 19:01 Quote from: arneolav on August 17, 2023, 08:56:24 pmThis code have to run 24/7, but should have a nice way to stop the Repeat, Ideas? Set a boolean when a button was pressed, a amount of time has elapsed or otherwise and test for that in the loop ? What kind of variable is xClose, how/why/when do you set it ? ---------------------------------------------------------------------- jamie · 2023-08-17 21:11 i don't know about the LazSerial having EOL data. Maybe its adding it at the end of each read? The serial com shouldn't be trimming it, it's a very simple read/write system in the OS. ---------------------------------------------------------------------- arneolav · 2023-08-17 21:17 Quote from: jamie on August 17, 2023, 11:11:50 pmi don't know about the LazSerial having EOL data. Maybe its adding it at the end of each read? It seems so, yes. ---------------------------------------------------------------------- arneolav · 2023-08-17 21:19 Quote from: TRon on August 17, 2023, 09:01:17 pmQuote from: arneolav on August 17, 2023, 08:56:24 pmThis code have to run 24/7, but should have a nice way to stop the Repeat, Ideas? Set a boolean when a button was pressed, a amount of time has elapsed or otherwise and test for that in the loop ? What kind of variable is xClose, how/why/when do you set it ? It's a boolean, set when button close is clicked. but may need a test on valid handle. ---------------------------------------------------------------------- jamie · 2023-08-17 21:25 There is a RcvLineCRLF property in there, maybe play with that one. ---------------------------------------------------------------------- arneolav · 2023-08-17 21:36 Quote from: Jurassic Pork on August 17, 2023, 12:08:09 amHello,  You can use synaser for this. if you are on windows there is a small windows daemon project (windows service) to listen COM3 serial port for gps information here The incoming data are written in a log file. Friendly, J.P This daemon is exactly what I need, running like a charm, and com read in a thread! Installed the daemon ok, but did not run due to some win security. Start in the service is ok. Soon did I get a lot of sensor values in the logfile, so then I have to do a small change; Remove the write to the logfile and replace by my own. I'll put all my stuff in a separated unit. Thanks Jurassic!