Recent

Author Topic: TLazSerial my best option?  (Read 8084 times)

jdredd

  • New Member
  • *
  • Posts: 23
TLazSerial my best option?
« on: July 20, 2016, 05:45:06 pm »
Pretty new to using TLazSerial ... Using it with Windows and an RPi3 ...

I see TLazSerial, TComport, Synapse, 5dpo, ect .... which looks like most are based off just one original source?

Anyways, I know its opinion based, but if possible, before I get myself to deep rewriting this program, what is
the best Serial component to go with for Windows and Linux compiling?






Arctic_Eddie

  • Jr. Member
  • **
  • Posts: 91
Re: TLazSerial my best option?
« Reply #1 on: July 20, 2016, 11:12:05 pm »
TLazSerial works quite well. However, if you go one layer deeper using TLazSerial.SynSer, you will find numerous functions and procedures available.

jdredd

  • New Member
  • *
  • Posts: 23
Re: TLazSerial my best option?
« Reply #2 on: July 20, 2016, 11:16:58 pm »
TLazSerial works quite well. However, if you go one layer deeper using TLazSerial.SynSer, you will find numerous functions and procedures available.

Yah ive been using the .SynSer part of it for a few things. Just seems odd to me is all to go that route.






JonnyMac

  • Newbie
  • Posts: 6
Re: TLazSerial my best option?
« Reply #3 on: November 19, 2017, 06:58:36 pm »
TLazSerial works quite well. However, if you go one layer deeper using TLazSerial.SynSer, you will find numerous functions and procedures available.

Can you -- or anyone else -- expand on this comment? FTR, I'm brand new to Lazurus, though I did program in Turbo Pascal ages ago, and even worked with Delphi when it first came out. For the past 20 years, though, I've been an embedded guy, with the last 10 mostly using the Parallax Propeller microcontroller. I do a bunch of serial work with the Propeller, but I am mystified by serial use in Lazarus. In Lazarus, everything seems string based, where I am used to doing things a byte at a time.

For example, this is a bit of code I use (successfully) for receiving XBee packets into the Propeller.

pub get_rx_frame(ms) : len | c, pb

'' Check for and return rx frame if available

  if (ms =< 0)
    c := serial.rxcheck
  else
    c := serial.rxtime(ms)

  if (c == $7E)                                                 ' frame header?
    bytefill(@rxbuf, 0, BUF_SIZE)                               ' clear input buffer
    rxbuf[len++] := c                                           ' put header in buffer

    c := serial.rxtime(2)                                       ' msb of payload (always 0) 
    if (c < 0)               
      return 0               
    else   
      rxbuf[len++] := c

    pb := serial.rxtime(2)                                      ' lsb of payload bytes   
    if (pb < 0)               
      return 0               
    else   
      rxbuf[len++] := pb
       
    repeat (pb+1)                                               ' get payload bytes + checksum
      c := serial.rxtime(2)
      if (c < 0) 
        return 0 
      else
        rxbuf[len++] := c


Parallax uses Delphi to create their internal IDE, so you can see that this language (Spin) is influenced by Pascal (as well as C and Python).  I'd like to do with Lazarus what I'm doing here: if there's anything in the RX buffer, pull a byte and deal with it.

I'm sure the tools are there and I'm overlooking them in my ignorance. I live in work in Hollywood (SAG-AFTRA member), so I don't have a problem being made look foolish -- it's a small price to pay to begin to move forward.


« Last Edit: November 19, 2017, 07:01:00 pm by JonnyMac »

boz

  • New Member
  • *
  • Posts: 10
    • Roving Dynamics Ltd
Re: TLazSerial my best option?
« Reply #4 on: November 19, 2017, 07:27:13 pm »
Hi

I am using Lazarus and Lazserial for embedded. a string is just an array of bytes so there are no issues (you dont need CR/LF for it to work if thats what your thinking)

Here is the OnRxData method I use for my last project which just waits for the whole message to come in, parses it and replies. should give you the jist of whats happening

Code: Pascal  [Select][+][-]
  1. procedure THostForm.serialRxData(Sender: TObject);
  2. var
  3.   s,RxBuffer,TxBuffer:string;
  4.   i:integer;
  5. begin
  6.   while Serial.DataAvailable do
  7.   begin
  8.     s:=serial.ReadData; // read in serial
  9.     for i:=1 to length(s) do // append to whats already received
  10.       RxBuffer=RxBuffer+s[i];
  11.     sleep(2); // give enough time for next char at 9600 baud but break when no serial data is received for 2-3 chars (@9600 baud)
  12.   end;
  13.   if RxBuffer[2] = #$05 then // process message and send ack
  14.   begin
  15.     TxBuffer:= #$01 + #$02 + copy(RxBuffer,2,4) + #$03; // modify message and send it back
  16.     serial.WriteBuffer(TxBuffer,length(TxBuffer));
  17.   end;
  18. end;

Hope it helps
Boz
Roving Dynamics Ltd
www.rodyne.com

JonnyMac

  • Newbie
  • Posts: 6
Re: TLazSerial my best option?
« Reply #5 on: November 19, 2017, 07:50:04 pm »
Thanks, Boz, here's where I ended up.

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.serialRxData(Sender: TObject);
  2. var
  3.   c : byte;
  4. begin
  5.   c := serial.SynSer.RecvByte(2);
  6.   mmoCapture.Text := mmoCapture.Text + chr(c);
  7. end;

For the moment, I have a Propeller board sending strings via USB. I simply wanted to prove that I could capture them, one byte at a time, and put them somewhere else. It works.

Thanks for the boost, and cheers from Hollywood.

Jon
« Last Edit: November 19, 2017, 08:33:21 pm by JonnyMac »

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: TLazSerial my best option?
« Reply #6 on: November 19, 2017, 08:40:05 pm »
what is the best Serial component to go with for Windows and Linux compiling?
Depends on your needs. Do you expect to work with visual event based component? If yes, then can you tolerate small not responding periods when main thread handles communication instead of handling gui? If not, then are you familiar with creating communication thread and syncing with main gui thread?

I am biased to Synaser, because it's a perfect companion for communication thread, and having separate communication thread makes your application look responsive all the time. I also used LazSerial and TDataPort but they did not work with MOXA virtual serial port drivers (they do work well with real serial ports), so I mostly switched to Synaser.

Here are listed all your options, and some have examples:
http://wiki.lazarus.freepascal.org/Hardware_Access#Serial_Communication

Very simple example for Synaser used from gui without separate comm thread:
http://wiki.lazarus.freepascal.org/Arduino
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

JonnyMac

  • Newbie
  • Posts: 6
Re: TLazSerial my best option?
« Reply #7 on: November 20, 2017, 03:23:37 am »
The Arduino demos are usually from the PC to the Arduino (as in the case above); I want to go the other way. In my project, I am receiving a IR protocol from a specialty gaming device that I want to convert and send up to the PC. I'd like to start with Windows and then move to the RPi.

I made some headway today, and then learned that TLazSerial won't work on the RPi. Darn.

It's funny... there are serial ports of one sort or another on virtually every PC but this isn't a component that is standardized in Lazarus.

I will get it -- it's just frustrating being on the steep slope of the initial learning curve. I liked your comment vis-a-vis a communication thread, especially since it comes from practical experience. Can you tell me more?

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: TLazSerial my best option?
« Reply #8 on: November 20, 2017, 10:26:10 am »
The Arduino demos are usually from the PC to the Arduino (as in the case above); I want to go the other way. In my project, I am receiving a IR protocol from a specialty gaming device that I want to convert and send up to the PC. I'd like to start with Windows and then move to the RPi.
The simplest FPC command line synaser demo receives serial mouse stream of bytes in a forever loop. Just look at the link. However, from this to a gui is a long way. You need to learn a lot. I will discuss your options at the end of this message.
http://wiki.lazarus.freepascal.org/Hardware_Access#Synaser

Quote
I made some headway today, and then learned that TLazSerial won't work on the RPi.
Unfortunatelly. The link bellow confirms this. Look at other options. I have tested TDataPort and liked it a lot (but didn't test it on RPi). However, TDataPort has problems with MOXA virtual serial ports which I have to use to access some serial devices over network, so I stick to Synaser. It might be my personal preference, but for writing communication threads Synaser and Synapse have never let me down. They work rock solid on 24/7 applications that are running for months without restart. Besides, once you learn Synaser you will see that exactly same code logic can be applied to your Synapse TCP/UDP clients and servers. A win-win case.
https://forum.lazarus.freepascal.org/index.php/topic,30577.msg194646.html

Quote
It's funny... there are serial ports of one sort or another on virtually every PC but this isn't a component that is standardized in Lazarus.
Delphi also doesn't have a standard component for serial communication. FPC does have a unit to handle serial communication cross platform, but it has not been wrapped into a component for Lazarus. Anyway, I like Synaser so I didn't care. It's rock solid and cross platform, too. Synaser has a visual component wrapper for both serial and tcp/udp communication named Visual Synapse, but I haven't checked it lately (since for any complex task with gui applications threads are a way to go) and I have never tried Visual Synapse on RPi.
https://sourceforge.net/p/visualsynapse/wiki/Home/

Quote
I will get it -- it's just frustrating being on the steep slope of the initial learning curve.
You're not alone, but you have to read a lot first and pay attention to avoid for example problems mentioned in these threads:
https://forum.lazarus.freepascal.org/index.php/topic,36885.0.html
https://forum.lazarus.freepascal.org/index.php/topic,36523.0.html

Quote
I liked your comment vis-a-vis a communication thread, especially since it comes from practical experience. Can you tell me more?
Well, I do not recommend you to start with communication thread first. Better start with something smaller, and once you are confident move forward. Otherwise it might be too much and you might give up. With serial component you have events. That is easy to work with, but if you have more then few bytes from time to time, you will soon have an application that will cause mouse moves to become erratic and which will have constant small periods of not responding to windows messages. That's because main thread which handles windows messages is blocked by your communication for small periods of time (but big enough to be irritating for your users). Since event based logic is hard to convert to communication thread later if you use another serial component, I would recommend to use the same serial lib for your event based communication that you will later use for threading. Your life will be much easier. Anyway, what are your options with Synaser? Simple Synaser example that sends a message on button click is in the wiki. However, you need to respond to a receiving message. You can do it in 2 ways with Synaser. Both need manual polling that will check if there is something in serial communication receive buffer. First is with a timer, and that's what I recommend for a start since it's quite easy. If your messages are just few bytes from time to time then you might even get away with it and find it enough for your needs. Second is with separate communication thread. It is not easy, you need to study a lot and learn about thread synchronization, atomic access to variables, train not to access gui from communication thread to avoid SIGSEGV, exceptions and leaks (well, there is a async way but not for beginners), and few more things.

You can find Synaser documentation here:
http://synapse.ararat.cz/doc/help/
http://synapse.ararat.cz/doku.php/public:howto
Please study it first if your choice will be Synaser. Have in mind that most TCP examples can be aplied to serial too. For download you should use SVN version because stable version has not been updated for a very long time. Both versions are very stable so there is nothing to fear about trunk version. Synaser has pretty liberal BSD license, and it's safe to use for commercial applications.

Short explanation why gui application might become non-responsive: http://forum.lazarus.freepascal.org/index.php/topic,36656.msg244520.html. Application.Processmessages and Timers can help in some cases, but they are not all mighty. Communication thread is the way to go to have your application responsive even when communication messages are being sent/received.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: TLazSerial my best option?
« Reply #9 on: November 20, 2017, 10:31:51 am »
Yes. Very good write up.
More specific the last remark: for reliable communication use a thread.

And use synapse: works perfectly OK on a Raspberry Pi and is very, very stable: hence the code needs less updates, hence you may be fooled it is not maintained: it is maintained, but because it is stable it needs less updates.

Note that means writing code by hand: TLazSerial components aren't very good (my opinion) and it takes just a few lines of code.
Specialize a type, not a var.

JonnyMac

  • Newbie
  • Posts: 6
Re: TLazSerial my best option?
« Reply #10 on: November 21, 2017, 05:12:09 am »
Thanks for the feedback, gentlemen. I realize I'm biting into two apples at once (learning Lazarus and serial coms on a PC-type platform) but it's my nature. My colleague and I (we are www.efx-tek.com) are going to sit through a day of online classes to learn Lazarus this week; that will probably be helpful. That said, I tend to recommend people have an actual project in mind when learning to program the Propeller (which I write about in Nuts & Volts magazine). I'm trying to swallow my own medicine!

Again, thanks for the thoughtful commentary.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: TLazSerial my best option?
« Reply #11 on: November 21, 2017, 09:03:53 am »
I tend to recommend people have an actual project in mind when learning to program the Propeller (which I write about in Nuts & Volts magazine)
I am glad to see here a writer for such a nice magazine, and I hope that your experience will be positive and that you will have a chance to recommend Lazarus. Lazarus is a perfect fit, being able to produce cross platform gui and console executables and services without dependencies to frameworks having hundreds of megabytes, and being able to communicate with custom devices through serial, parallel, tcp/udp, i2c, spi, 1wire, gpio and other means. Lazarus is not limited to just RPi, although RPi has best support since many people have it. Look at so many ways to access the hardware:
http://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi

You don't even need an OS for RPi:
https://ultibo.org
https://ultibo.org/wiki/Main_Page
https://ultibo.org/wiki/Supported_Hardware

You should have in mind that writing communication threads (and multithreaded applications in general) with Lazarus does not differ much compared to Java or C#. The learning curve is the same, and so are the problems developers face. Once you are familiar with multithreading in one language, another one won't be a problem at all since the most important knowledge you already have.

It might be also significant to mention that Synaser/Synapse works with Delphi too, so if you want you can write applications that compile with both Delphi and Lazarus.

Have fun!
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

JonnyMac

  • Newbie
  • Posts: 6
Re: TLazSerial my best option?
« Reply #12 on: November 22, 2017, 05:05:57 am »
Quote
I am glad to see here a writer for such a nice magazine, and I hope that your experience will be positive and that you will have a chance to recommend Lazarus.

I absolutely will -- but only I have mastered things. Anything that I present in my column has to be explained (by me!) and I have to provide working code. I think this is a bit of my frustration: there are lots of snippets but -- that I've seen, anyway -- not a great starter program for basic serial coms using Lazarus; every thing I've seen is partial code and makes a lot of assumptions. Still, I recognize that in my ignorance of Lazarus I could be looking right past what I need.

I've seen Ultibo and that looks very interesting, especially since it's based in Lazarus (another reason for me to really master this).

I don't know if it's any good but I purchased the Lazarus programming course from Udemy. My business partner and I are going to have adult drinks and take the course together this weekend. I'm hoping that once we've done that I can port a little gaming accessory that runs on the Propeller to the RPi. The device listens for special messages on an XBee network and keeps track of team scores. The Propeller is capable of simple VGA output, but it's really not what I want. It would be nice to have a program I can run on my PC, or put into a box with a small screen using the RPi.
« Last Edit: November 22, 2017, 02:52:49 pm by JonnyMac »

JonnyMac

  • Newbie
  • Posts: 6
Re: TLazSerial my best option?
« Reply #13 on: January 20, 2018, 10:10:27 pm »
Life has been busy but I did finally manage to get a very small serial project working using the SynaSer library that was recommended by so many.

Goals:
  • Scan/Re-scan for available serial ports
  • Select and connect to port
  • Send a command byte
  • Asynchronously process a response byte

I used a Parallax Propeller Activity Board because it has two LEDs attached.

Yes, it's dirt simple, but I hope it will help someone who struggled like I did. I've attached the Lazarus project and my Propeller code.

Fair warning: I'm new to Lazarus so I may not have done everything is the best way. That said, it works.

Jon


avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: TLazSerial my best option?
« Reply #14 on: January 21, 2018, 03:50:33 pm »
I did finally manage to get a very small serial project working using the SynaSer library
I am glad that this story got a happy ending  ;)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018