Recent

Author Topic: Serial read and database  (Read 10275 times)

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Serial read and database
« on: February 10, 2016, 09:32:43 pm »
Hello, I'm a new user of Lazarus and Pascal (I'm a C++ user )
I would like to know what is the better solution( and faster) to read and then write datas from LazSerial to a dataset.(TDbf)
The datas are coming from a microsystem so I can format the coming strings as needed.
Can I fill easily the fields of the dataset during the serial read or is it necessary to use a temporary file to make this operations ?
Sorry for my very poor english !!  %)
Many thanks !


BlueIcaro

  • Hero Member
  • *****
  • Posts: 792
    • Blog personal
Re: Serial read and database
« Reply #1 on: February 10, 2016, 10:25:51 pm »
Hello, here you have a tutorial about Tdbf
http://wiki.lazarus.freepascal.org/Lazarus_Tdbf_Tutorial

Here is a library to use serial port
http://synapse.ararat.cz/doku.php

If you want a component based in the previus libray, here you have a component to use serial port
http://forum.lazarus.freepascal.org/index.php/topic,20481.0.html

/Blueicaro

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Serial read and database
« Reply #2 on: February 11, 2016, 09:13:14 am »
It's depending how your serial data is read. If the microsystem is sending data continues, I don't think dbf files is correct way of storing data. Dbf is the old fasion way of using pointers on the file that's open on the drive.

I know marcov is using kind of this and maybe he has the right answer.
I should reading the data in a memory dataset and on a idle moment put the data in any kind of database.

But that's my opinion.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Re: Serial read and database
« Reply #3 on: February 11, 2016, 09:55:25 pm »
Thank you for these first answers.
Effectively, actually, the microsystem sends data continues on the serial port but I can modify this way with an
handshaking procedure for example.
Now, I'm trying to use a temporary file in a first time(it's ok for me) and now I'm searching for to write in my database with.
Sorry for my basic questions but is there other solution for to write data directly in the fields of a database than use a TDB component ?

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: Serial read and database
« Reply #4 on: February 12, 2016, 10:10:51 am »
Hi,

Read data from serial port by Synaser (TBlockSerial class) from Synapse library and save it to dbf file by using TDbf.

dbf file (Dbase, FoxPro etc.) is old fashion. It better to use embeded database such as Firebird or SQLLite insteand of TDbf. Both are supported by FPC/Laazarus.
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: Serial read and database
« Reply #5 on: February 12, 2016, 10:19:05 am »
to write data directly in the fields of a database than use a TDB component ?

What you mean directly write to database?

1. Create dbf file (DBase, FoxPro) with required table structure by TDbf.
2. Open dfb file by TDbf.
3. Create connection to serial device TBlockSerial (SynaSer.pas from Synapse library) to read data.
4. Write data to dbf file by TDbf.
5. Repeat steps 3,4.
6. Close dbf file.


 
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Serial read and database
« Reply #6 on: February 12, 2016, 10:44:53 am »
I think he uses DB GUI components.
You can directly save to the dataset by using fieldbyname(<fieldname>) or fields[<index>.

Look at these examples ( http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/DB_TDataSet_FieldByName.htmlhttp://wiki.freepascal.org/SqlDBHowto) and search for fieldbyname.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Re: Serial read and database
« Reply #7 on: February 12, 2016, 08:59:43 pm »
Hi !
Ok, I understand that TDbf is the most easy way to write data to dbf and I'm testing actually the same method than you explain mig-31.
I will have a look to your example mangakissa because any information is well come for me !
Many thanks !

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Re: Serial read and database
« Reply #8 on: June 03, 2016, 05:16:44 pm »
Hi all !

I have a problem for to send many data series with Lazserial.WriteData : packets are truncated (why not) but some of them are missing when my database is increased...a problem with windows priorities ? ( I control this with an data analysis on the serial port)
Many thanks for any idea...

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Serial read and database
« Reply #9 on: June 06, 2016, 02:16:34 pm »
I have a problem for to send many data series with Lazserial.WriteData : packets are truncated
Instead of this "for i_loop := 1 to NbrRecords do" it would be better to use "while not EOF do". Your loop does not allow form to get updated because messages are not processed, so that's how your data might get truncated. You would probably not have this problem if you have been reading DB fields directly. However if you have to show your data in the form while you are sending it through serial, then insert "Application.ProcessMessages' after line with "Dbf1.Next".
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Re: Serial read and database
« Reply #10 on: June 10, 2016, 08:19:41 pm »
Hello avra !

Many thanks !
I'm trying immediatly your suggest and I hold you informed !

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Serial read and database
« Reply #11 on: June 11, 2016, 09:19:11 am »
hi! if you are using realtime data just rum 2 more threads - one with ring buffer in memory to store incoming data and one with db client to store data in db... preferabli sqllite but in this scenario you can 3ven use remote db like mysql server... just be sure to allocate a large ring buffe4 to avoid overflows between db dumps
Speak postscript or die!
Translate to pdf and live!

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Re: Serial read and database
« Reply #12 on: June 11, 2016, 10:45:37 pm »
Hi Blestan !
The suggestion of Avra "while not EOF do" decreases the delay between the data packets (but not suppressed) but allow to send all the data correctly.
I don't understand exactly your suggestion : have you an exemple somewhere ?
Many thanks !

Blestan

  • Sr. Member
  • ****
  • Posts: 461
Re: Serial read and database
« Reply #13 on: June 12, 2016, 09:00:26 am »
hi! just run the while not eof in a separate thread to leave the gui unblocked ... and then in a third thread every for example 1 sec send all buffered data to db ...then put this thread to sleep...for the rest of the 1 sec
Speak postscript or die!
Translate to pdf and live!

Pierre.Marthinet

  • New Member
  • *
  • Posts: 11
Re: Serial read and database
« Reply #14 on: June 12, 2016, 09:43:44 pm »
Hi ! In fact, the database is already complet when I'm sending the data on the serial. 
I think to use only one thread with the while not eof...or perhaps I don't understand your suggestion ? (I'm a new user..)

 

TinyPortal © 2005-2018