Recent

Author Topic: TLazSerial : serial port component for Lazarus (windows and linux).  (Read 345199 times)

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #135 on: August 23, 2016, 06:26:22 am »
TLazSerial is nice if you just want a good, no fuss connection to a com port.
TDataPort looks like the go to if you want more hands on with it. Very complete set of components. Definitely more geared for uC's.

https://github.com/serbod/dataport

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #136 on: August 30, 2016, 06:57:20 pm »
Hi, got a specific question.

What statement do I have to use in Lazarus to find out if there's data ready (received) at the COM (RS232).

At the Arduino site I can check it, see flowchart. This works ok.
At the Lazarus site I just read and display it, but it's not synchrone.

Just by some lucky shots and a value send from Arduino is read and displayed by Lazares on pc-screen. ( This value is ok, has been send by Arduino. )

What 'code'do I need?

to DIY or not to DIY

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #137 on: August 30, 2016, 07:05:09 pm »
Extra Info:

Lazarus Send:
serial.WriteData( stOut);      string-OUT: always 6 bytes long.

Lazarus Receive:
stIn:= serial.ReadData;        string-IN: always 6 bytes long (send by Arduino).



I understand, the 'code' will need a time out to prevent endless checking for data.
« Last Edit: August 30, 2016, 08:55:36 pm by jb007 »
to DIY or not to DIY

PStechPaul

  • Jr. Member
  • **
  • Posts: 76
    • P S Technology, Inc.
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #138 on: August 30, 2016, 10:16:35 pm »
Here are some functions that should work:

Code: [Select]
    {:Returns the number of received bytes waiting for reading. 0 is returned
     when there is no data waiting.}
    function WaitingData: integer; virtual;

    {:Returns @True, if you can from read any data from the port. Status is
     tested for a period of time given by the Timeout parameter (in milliseconds).
     If the value of the Timeout parameter is 0, the status is tested only once
     and the function returns immediately. If the value of the Timeout parameter
     is set to -1, the function returns only after it detects data on the port
     (this may cause the process to hang).}
    function CanRead(Timeout: integer): boolean; virtual;

    {:This event is triggered when the communication status changes. It can be
     used to monitor communication status.}
    property OnStatus: THookSerialStatus read FOnStatus write FOnStatus;


These are found in "synaser.pas".

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #139 on: August 31, 2016, 09:24:17 am »
Tnx,

Sounds good!
Today's evening I'll try it.

I noticed CANread before but I tought it was for can-bus... :D

to DIY or not to DIY

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #140 on: August 31, 2016, 09:51:59 am »
You can use Synaser from Synapse and examine serial port buffer for waiting data by method WaitingData>0

Code: Pascal  [Select][+][-]
  1.   SerialPort:=TBlockSerial.Create;
  2.   SerialPort.Connect('/dev/ttyS0');
  3.   SerialPort.Config(9600,8,'N',SB1,False,False);
  4.   //wait for data test Waiting Data
  5.   if SerialPort.WaitingData>0 then SerialPort.RecvString(100);
  6.   FreeAndNil(SerialPort);
  7.  
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #141 on: September 01, 2016, 03:16:17 pm »
By the way, still waiting to receive my book Lazaus the complete guide.

Alltought I did some programming in Delphi years ago, I need a good base.
E.q. to get a good understanding between classes, methods, the whole Lazarus-IDE etc.
Programming itself is not the problem. ( I did a lot in Turbo Pascal 7.0 )

Tnx, PSechPaul and mig-31 for your replies!
Gonna work with it.
to DIY or not to DIY

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #142 on: September 01, 2016, 09:16:56 pm »
Sorry for my short of knowledge of Lazarus.

All done in Main


Have ad a if/waitingData, makes total:
======================
serial.writeData( stOut ); 

if ( serial.waitingData > 0 ) 
  then  stIn:= serial.readDate;

label4.caption:= stIn;
======================


Results in compile-error: identifier idents no member "waitingData"


So, serial at itself is "known" because serial.writeData and serial.readData are doing ok.


Tried the following:

Placed in interface-section of main: serial: TBlockSerial;

Results againin compile-error:  identifier idents no member "waitingData"

Then I removed 'serial: TBlockSerial;' out of interface-section.

Then placed in interface-section: myTestSerial: TBlockSerial;

And changed in the write-read code:

if ( serial.waitingData > 0 ) 
  then  stIn:= myTestSerial.readDate;

label4.caption:= stIn;


Now, I don't get compile-error!
But the programm hangs imideately, no response, and shows error as in the picture.
I can stop it with the red-stop-button in the LAzarus-IDE.

Anyone a hint for me?

 



« Last Edit: September 01, 2016, 09:20:10 pm by jb007 »
to DIY or not to DIY

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #143 on: September 01, 2016, 09:19:16 pm »
if wanted,i can start a topic for my own 'case' so I don't mess-up this actualy topic

"TLazSerial : serial port component for Lazarus (windows and linux)."
to DIY or not to DIY

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #144 on: September 02, 2016, 03:24:39 am »
hello,
waitingdata is a property of synaser tblockserial . So to use with tlazserial component, you must use it with the tblockserial part of tlazserial component (SynSer).

example to send data to a serial line and wait for at least 6 bytes response (Serial1 is a TLazserial component) :
Code: Pascal  [Select][+][-]
  1.   Serial1.WriteData(myData);
  2.    // Waiting for response ( at least 6 bytes)
  3.    While Serial1.SynSer.WaitingData < 6 do
  4.    begin
  5.      Application.ProcessMessages;
  6.      Sleep(10);
  7.    end;
  8.    Edit1.Text := Serial1.ReadData;  

Be carefull, if you don't receive response , you stay  in the loop.  Use a timeout code to prevent the user.
For example :
Code: Pascal  [Select][+][-]
  1. var timeout : integer;
  2. begin
  3.    timeout := 0;
  4.    Serial1.WriteData(mydata);
  5.    // Waiting for response
  6.    While Serial1.SynSer.WaitingData < 6 do
  7.    begin
  8.      Application.ProcessMessages;
  9.      Sleep(10);
  10.      inc(timeout);
  11.      if timeout > 1000 then        // 10 seconds
  12.        begin
  13.             ShowMessage('No response from the device');
  14.             Exit;
  15.        end;
  16.    end;
  17.    Edit1.Text := Serial1.ReadData;
  18. end;                  

Friendly, J.P
« Last Edit: September 02, 2016, 03:27:14 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

PStechPaul

  • Jr. Member
  • **
  • Posts: 76
    • P S Technology, Inc.
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #145 on: September 02, 2016, 03:56:53 am »
I also noticed a typo in your code:

Code: Pascal  [Select][+][-]
  1. if ( serial.waitingData > 0 )
  2.   then  stIn:= myTestSerial.readDate; //should be readData
  3.  
  4. label4.caption:= stIn;

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #146 on: September 02, 2016, 09:39:12 am »
GREAT! Tnx!

After placing using 

serial.synser.waitingData  instead of

serial.waitingdata thing rappidly improved!

Finaly I got:

Code: Pascal  [Select][+][-]
  1.    
  2. stOut:= (stNumberChangedScrollBar +  stTemp + 'el');
  3. serial.WriteData(stOut);
  4.  
  5. while (serial.synser.waitingData < 4)  
  6. {
  7.    6 bytes to Arduino including delimiter 'el'
  8.    4 bytes returned by Ardino, is ok without returning delimiter 'el'.
  9. }
  10.      do
  11.         begin
  12.           application.processMessages;
  13.           sleep(1);
  14.         end;
  15.       stIn:= serial.readData;
  16.       label4.caption:= stIn;
  17.       //FreeAndNil(Serial);

   
« Last Edit: September 02, 2016, 10:29:18 am by jb007 »
to DIY or not to DIY

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #147 on: September 02, 2016, 10:24:14 am »
Finaly got it stable, with:

Code: Pascal  [Select][+][-]
  1.           sleep(1);
  2.           inc(iTimeOut);
  3.           if (iTimeOut > 2) then exit    //  (iTimeOut > 1) is not working
  4.  



As written before: Arduino receives and imidiately sends back:

Code: Pascal  [Select][+][-]
  1. uart3_read_text(stRC, 'el', 7 );
  2. uart3_write_text(stRC);
  3.  


« Last Edit: September 02, 2016, 10:35:17 am by jb007 »
to DIY or not to DIY

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #148 on: September 02, 2016, 10:47:34 am »
Some explanation about values:

2063 means scrollBar #2 and value 63

Code: Pascal  [Select][+][-]
  1.  
  2. //  0   1   2    3     4     5   6    7  led
  3. //  1   2   4    8    16    32  64  128  bin
  4. //  0   1   3    7    15    31  63  127  dec
  5. //16   48   80  112  144   176 208  240 tres  
  6.  








« Last Edit: September 02, 2016, 10:52:07 am by jb007 »
to DIY or not to DIY

jb007

  • Full Member
  • ***
  • Posts: 145
Re: TLazSerial : serial port component for Lazarus (windows and linux).
« Reply #149 on: September 05, 2016, 07:06:59 pm »
Slowly I'm getting more understanding how things in al the units are 'connected' to eachother.

I'm trying to get multiple comports in Lazarus. I allready got 2 Arduino's at my pc and all
the 2 of them are communication well with lazarus, just 1 at a time!

I just changed proporty "device", and start debug/run. In my case: com13/com14.

The goal is to use more com's in the application.
to DIY or not to DIY

 

TinyPortal © 2005-2018