Serial Port Component For Lazarus Come

Posted By admin On 04.01.21
Serial Port Component For Lazarus Come Average ratng: 9,3/10 1688 votes

Newer versions of FPC/Lazarus may support newer MySQL client libraries. Make sure you are using the correct connection component for your client library version. So if you have the client libraries installed for MySQL 4.1 then you have to use TMySQL41Connection component, even if the server is running version 4.0 or 5.0. The reason for this is. After a long time, i decided to go ahead to develop a program to read the data from different sensors installed in my arduino board, based on the use of Pascal on Lazarus open source. I located also this component to establish the serial communication between arduino and lazarus.

Deutsch (de)English (en)español (es)français (fr)magyar (hu)日本語 (ja)한국어 (ko)polski (pl)português (pt)русский (ru)slovenčina (sk)中文(中国大陆)‎ (zh_CN)

Serial Port Component For Lazarus Comedy

  • 3Parallel Communication
  • 4Serial Communication
  • 5USB

Overview

This page describes various ways of accessing hardware devices on Lazarus. These devices include, but are not limited to: ISA, PCI, USB, parallel port, serial port.

Uniform multi-platform access to hardware devices is not implemented by the Free Pascal Runtime Library (RTL) or by the LCL - the underlying operating systems are often different enough to make that very difficult. Therefore, this article will basically cover hardware access methods on different platforms. The code can be compiled on different environments using conditional compiles, like this:

Parallel and Serial Comparison

ISA cards, PCI cards and the Parallel Port communicate with the computer using a parallel protocol. The Serial Port and USB devices work with a serial protocol. Because the processor and thus programming languages all work on a parallel approach to data, access to these kinds of protocols is easier to be implemented on the software side. When an Integer variable is accessed for example, its value can be accessed with a single command. With a serial protocol however, only one bit at a time can be accessed and the pieces need to be 'glued' together to understand the data.

Serial communication is difficult to be implemented directly, but it can be pretty easy if pre-made component are used. It is also harder on the hardware side, so many devices use specialised integrated circuits or microcontrolers to implement it.

Now a brief comparison of hardware access protocols will be given:

SpeedHardware implementation difficulty
Serial PortVery slow (< E5 bit/s)Medium
Parallel PortSlow (~ E6 bit/s)Easy
ISA CardMedium (~ E7 bit/s)Medium
USBMedium (~ E7 bit/s)Hard
PCI CardVery Fast (> E9 bit/s)Very Hard

Parallel Communication

Using inpout32.dll for Windows

Windows has different ways to access hardware devices on the 9x and NT series. On the 9x series (95, 98, Me) programs can access the hardware directly, just like they did on DOS. The NT series (Windows NT and XP), however, do not allow this approach. On this architecture, all communication with hardware ports must be through a device driver. This is a security mechanism, but developing a driver for small projects can cost too much in terms of time and money.

Fortunately there is a library that solves this problem. If Windows NT is detected, it decompresses the HWInterface.sys kernel device driver and installs it. If Windows 9x is detected, it simply uses assembler opcodes to access the hardware.

The library has only two functions, Inp32 and Out32, and their use is quite intuitive.

The library will be loaded dynamically, so define both functions first:

  • 'Address' represents the address of the port to be accessed
  • 'Out32' sends data to the port specified by 'Address'
  • 'Inp32' returns a byte from the port specified by 'Address'

Now the library can be loaded. This may be implemented in the 'OnCreate' method of the program's main form:

If the library is loaded on 'OnCreate', it must be unloaded in 'OnDestroy':

Here is a simple example of how to use the 'Inp32' function:

This code was tested with a custom ISA card on port $0220, using Lazarus 0.9.10 on Windows XP. Of course 'Windows' must be in the uses clause in order for this code to run.

Note: For deployment 'inpout32.dll' must be in the same directory of the application. Also the library has to be registered in 'system' by the 'administrator' user on Windows NT/XP/2000 or elevated privileges on Windows Vista/7. This can be done by installation of a program such as InnoSetup:


This is the homepage for the library: www.logix4u.net/inpout32.htm (see discussion).

Using assembler on Windows 9x

On Windows 9x assembler code can be used. Suppose one wants to write $CC to the $320 port. The code to do that is:

Troubleshooting on Windows

One possible source of trouble using parallel hardware that does not support Plug And Play on Windows is that Windows may assign the port utilized by the hardware to another device. Instructions on how to tell Windows not to assign the address of a device to Plug And Play devices can be found at http://support.microsoft.com/kb/135168

Using 'ioperm' to access ports on Linux

The best way to access the hardware on Linux is through device drivers, but, due to the complexity of the task of creating a driver, sometimes a quick method is very useful.

In order to use the 'ports' unit under Linux the program must be run as root, and IOPerm must be called to set appropriate permissions on the port access. Documentation about the 'ports' unit can be found here: http://www.freepascal.org/docs-html/rtl/ports/index.html

Download your device driver CANOPUS: it's FREE! Download CANOPUS IMAGINATE 2.0 driver for free. Canopus imaginate 2.0 employs Microsoft Agent technology, which supports speech recognition and text-to-speech functions in the guise of animated characters. In this case the character is Merlin, a cute and friendly-looking wizard. Canopus imaginate 2.0 free download. Download canopus imaginate 2.2 for free. Photo & Graphics tools downloads - Imaginate by Canopus and many more programs are available for instant and free download.

The first thing to do is link to (g)libc and call IOPerm. A unit that links to the entire (g)libc exists on free pascal, but this unit gives problems when used directly by an application and linking statically to the entire (g)libc library is not good practise as it changes often between versions in an incompatible manner. Functions like ioperm, however, are unlikely to change.

  • 'from' represents the first port to be accessed.
  • 'num' is the number of ports after the first to be accessed, so ioperm($220, 8, 1) will give access for the program for all ports between and including $220 and $227.

After linking to IOPerm, port[<Address>] will access the ports.

This code was tested with a custom ISA card on port $0220, using Lazarus 0.9.10 on Mandriva Linux 2005 and Damn Small Linux 1.5

General UNIX hardware access


Note: FPC provides an abstraction for ioperm called 'fpioperm' in unit x86, and also defines fpIOPL and out-/inport functions. These functions are currently implemented for Linux/x86, Linux/x86_64 and FreeBSD/x86, FreeBSD/x86_64.

It is not recommended to link to libc unless absolutely necessary due to possible deployment and portability functions. Also manual linking to libc (by declaring ad hoc libc imports for functions that are available elsewhere) like done above is not recommended (e.g. the above libc import line will unnecessarily fail if the standard C lib is not called libc, like e.g. libroot on BeOS, or on platforms with a non standard C symbol mangling).

Note 2: Using unit libc is not recommended under any circumstances other than Kylix compatibility. See libc unit

Port

Status and control

Besides data lines, the parallel port also has status and control lines which are accessed using the status and control registers. While the base address accesses the data lines and reads or writes data bytes from/to them, the status register is accessed on the address offset by +1 and the control register is accessed on the offset +2. For example, LPT1 (first parallel port on a PC) has the base address $378, so its status register is at $379 and control register at $380. To get individual status line states, read a byte from its address and its bits represent those lines. Setting control lines is similarly done by writing a byte with accordingly set bits to the control register.

Newer bidirectional parallel port versions have more registers on higher offsets. More details about them, together with information which bits map to which lines can be found here.

Most directly accessed hardware devices other than PC parallel ports are controlled in a similar way. Depending on the device in question, it is necessary to find out what registers are available (above mentioned control and status, but also address and other registers) and which bits represent which hardware functions.

Serial Communication

Device Setup

Before starting, make sure to authorize your device on Linux. An option is to create a persistent UDEV rule for each device. For example, the following approach apply mode '0064' for the device specified by 'serial', 'idVendor' and 'idProduct' attributes:


Synaser

It is very easy to build a serial communication software using the Synaser library. The example when used together with the Synaser documentation should be trivial to understand. The most important part is TBlockSerial.Config to configure the speed (in bits per second), data bits, parity bits, stop bits and handshake protocol, if any. The following code was tested with a serial mouse connected to COM1.

The following code-example is an alternative version of the example above. The example above has a critical bug in its main concept, to be exact, it is the part with 'while true do..'. On the test system (Asus A6T laptop with Digitus USB to RS232 Adapter, Ubuntu 8.04.1), this part caused the following error: the application ran only one time successfully per session, when the application was started again, the application was unable to connect to the serial port. Thus, a reboot was necessary everytime the user tried to relaunch the application.

The reason is not difficult to understand: The application is in the while true do - loop, which is, to be more precise, an endless loop. There is no abort-condition, so the only way to close the application is to close the terminal or to press CTRL-C. But if the application is aborted this way, the important part with 'ser.free', which frees the serial port, will never be called. This problem is described in the following thread in the German Lazarus-Forum http://www.lazarusforum.de/viewtopic.php?f=10&t=2082

Serial Port Component For Lazarus Come

There is a bit of code around the main application to make it clear to the user not to press CTRL-C. /dev/ttyUSB0 is used for the com-port due to the USB to Serial Adapter (from Digitus) on the test-system. With a built-in serial port, use the 'Com0' - declaration like in the code - example above.

Also, the External Links section has UNIX and Windows serial port tutorials.

Note the function of the TBlockSerial.LinuxLock parameter under linux. When set to default of True, a connect will try to create a lock file (eg. 'LCK.ttyUSB0') under /var/lock and fail if a lock already exists for the requested port. The lock file will be left over if Free was not called. Setting LinuxLock to False will make Synaser ignore port locking.

There are alternatives to Synaser; see below.

Serial Port Component For Lazarus Come Follow Me

Visual Synapse

Visual Synapse has lots of component wrappers for many parts of Synapse serial and networking library. TvsComPort is a visual component wrapper around Synaser.

5dpo

There is also a visual component 5dpo based on Synaser.

TLazSerial

Based on 5dpo (and therefore Synapse): http://forum.lazarus.freepascal.org/index.php/topic,20481.0.html

FPC built in Serial unit

Another serial unit is part of FreePascal since version 2.2.2: just put 'Serial' in the Uses list. However there does not seem to be any documentation other than the Serial.pp source file and some discussions.

An example using FPC 2.6.2 on WIN7.

TDataPort

DataPort is a thread-safe abstract port for data exchange. It is used for communication over networks (TCP/UDP/HTTP), serial ports (UART, COM-port, FTDI), device files (ioctl supported) and conventional file named pipes. For serial communication it has TDataPortSerial and TDataPortFtdi descendants. Small example can be found in demo and here.

PXL

PXL (Platform eXtended Library) for low level native access to serial ports, GPIO, I²C, SPI, PWM, V4L2, displays and sensors.

Serial port names on Windows

COM ports are named with a number on Windows 9x-based OSes (95, 98, ME), e.g. COM1, COM30.

On Windows NT-based systems (NT, 2000, XP, Vista, Windows 7, Windows 8), COM ports are numbered too, but only for compatibility with DOS/Win9x.

Use this code to get the real name:

USB

libusb

A cross platform possibility for Windows, Linux, BSDs and macOS is libusb.

Headers are listed in http://www.freepascal.org/contrib/db.php3?category=Miscellaneous:

  • Serial Communication Links:
  1. On UNIX: [1]
  2. On Windows: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp
  3. Synaser component: http://synapse.ararat.cz/
  4. Comport Delphi package: http://sourceforge.net/projects/comport/
  • Digital Oscilloscope - A example of hardware access with full source included.
Retrieved from 'https://wiki.freepascal.org/index.php?title=Hardware_Access&oldid=129461'
HOME - - - - - Lazarus Tutorials TOC - - - - - - Other material for programmers
Save this on Delicious

Getting started with Synapse in Lazarus

Free programming environment. Free TCP/IP units.
And all multi-platform.

Getting started with Synapse in Lazarus.

14 Feb 17, I went back to Synapse, having been away for a while. (I wasn't using a different package, I just hadn't been doing any TCP/IP programming for a while.)

I was using a freshly installed Lazarus version 1.6.2 (2016-11-12) on a 64-bit Windows 7 computer. (I have not had good luck with the 64 bit Lazarus in the past, and tend to use the 32-bit compiler. For a start, I want things I write to work on a variety of machines. I don't want to maintain two versions.)

It had a tab on the components bar 'SynEdit' which is, I believe, nothing to do with the Synapse TCP/IP package. Ditto a tab 'IPro'. Both were present in a freshly installed Lazarus.

I don't think the computer concerned had ever had Synapse on it. Focus t25 workout utorrent.

((Sidebar:)) Oddly enough, although I don't think this machine ever had Synapse on it, something I wrote a while ago (which uses Synapse) compiled quite happily on this 'Synapse-less' machine. I believe this was because of the Synapse .pas files I had in the folder with the sourcecode for my project. There were 8 files in the folder which I believe came from Synapse. I say 'came from'.. I think I just used the ordinary Windows file handling utility to copy those files into the folder from.. somewhere! (Another machine I'd installed Synapse on? (P.S.: No.. it turns out I'd downloaded the package three years ago, in 2014. Apparently that time I just copied files from it. Didn't 'install'.)

Oh. Duh. RTFM'd (http://wiki.freepascal.org/Synapse#Installation) after I'd worried about the above for a while, and found..

Oh well. Having used the first solution in the past, and found it a pain to figure out WHICH files were needed, I thought that this time I'd try the alternative. (Figuring out which files are needed is a funny thing.. An error message will say you need file 'x'. So you copy that to the project folder, and try to compile again. But file 'x' needs files 'y' and 'z'. So you copy them. And..

But the first DOES work. (You just have to deal with a number of 'Could not find such-and-such file' complaints.. once per project.)

So I set off to conquer the second approach. I got it working.. sort of. You now tell it where to look for the 'missing' files (the .pas files supplied by Synapse) in the Project/ Project Options page. there must be a way to tell your Lazarus that you want that particular folder ((MyDocs)LazarusThirdPtrySynapsesourcelib, if you set things up as I did) in YOUR default project options. I haven't found it yet. But it is worth using the 'set Lazarus to look in the extra place' trick! The only downside is that an archive of the project won't include ALL of the files used to compile it. If the archive is moved to another machine, besides setting up Lazarus, the Synapse .pas files that project uses will also have to be made available. (I don't think this is a deal breaker.)

((end sidebar))

Much against my general wish for the quiet life, for you, dear reader, I embarked upon a Lazarus update prior to starting the Synapse tutorial I sat down to write. See my account of how the update went, if the information is of interest.

Install Synapse

Happy discovery!.. Synapse not only gives you access to TCP/IP, but I think it has some stuff for using the computer's serial port. Not sure if that includes simulated serial, as provided by some USB devices. (If Synapse doesn't offer this, a package with serial support is available from the same source. (See the download page.)

Just before we start, with the hope that I can put you in the right frame of mind: Here's a useful and informative note from the official Synapse page on installing Synapse..

Went to the Synapse homepage.

Slightly worrying.. the most recent (at 14 Feb 17) latest full release was from 2012. And there was talk about the program needing a Win 32 operating system. The latter not, I hope, an issue for me, because I use the 32-bit Lazarus, even on my 64-bit machine. But maybe it will matter to others?

Clicked 'Download'. It went VERY quickly.. under three seconds on my rather slow connection.. but I had a .zip file of 899kb. The download page said it included 'demos and documentation'.. and simple investigation, again with the normal Windows file management utility, made it seem that this was true.

Take a moment to read the short 'README.txt' inside the distribution .zip.

Ha! Hoist by my own petard! Synapse 'installs' the way my own software installs. No messing with the registry. No 'click the magic button and it will all be done for you (how we want to do it.)'

So you have options. Here's how I 'installed' the program on my system..

Start by setting up a temporary folder for the contents of the Synapse .zip file, and unzip everything into it.

I like to keep third party things where I can see them for what they are. I have, in 'MyDocs', a 'Lazarus' folder which is where I keep the sourcecode, etc, of my projects. (In sub-folders, one per project)

That seemed a good place to put the Synapse stuff. (I'd already learned from the README.txt file that there's a way to let Lazarus know where to look.)

So, I created (MyDocs)LazarusThirdPrtySynapse

(I've learned that with software which is also happy on Macs and Linux, it is best to avoid spaces in names, and to remember that THisNAme and thisname are sometimes seen as DIFFERENT. (Which of course they are NOT in Windows.) So I try to be consistent about the case of letters in a name.)

It seemed best to put ALL of the Synapse files in one place. A shortcut to
..LazarusThirdPtrySynapsedocshelpindex.htm
can go where I want it, for every day access, in due course.

So.. 'Synapse installed'.. but my Lazarus IDE doesn't yet know it!

Lazarus Examples

Showing Lazarus where to find the Synase material

Fire up Lazarus.

I'm going to show you how I use third party materials.. I only make them available on a project- by- project basis. Install them globally, and it is too easy to begin to think of something as part of the basic Lazarus. My view. There probably is a way to make something available 'for all time'.. but you'll have to look elsewhere for that.

To say to Lazarus, 'for this project, you should use..' you first go into a specific project. Either open an old one you want to extend, or start a new one.

Use 'Project/ Project Options/ Compiler Options/ Paths/.'

Once you've gone that far, there will be a panel to the right, with multiple edit boxes.

If you 'installed' Synapse on your system as described earlier.. i.e. unzipped all they offer, someplace sensible, but haven't made those files GENERALLY available to your Lazarus work, then you continue as follows. (If you haven't, go back and do that!)

Click the ellipsis (..) to the right of the edit box for 'Other Unit Files (-Fu):', and the 'Path Editor' should open. (That name is in title bar.)

Click the folder icon.

Lazarus For Windows

Lazarus

Drill to the relevant folder.. in this case ..Synapsesourcelib. Click Add. Click Test. Click OK.

I have to admit, my Lazarus was a bit flaky while I was trying to do this.. especially if I clicked the 'Test' button.. it didn't crash when I hit 'Test', but it did shortly after, and then during the next re-launch, and THEN it settled down.

Sigh. But I still like it!! (I was using ver 1.6.2 on a Win 7 machine, 23 Oct 17)

Onward!..

Once Lazarus can find the Synapse .pas files it needs, try running some of the examples which come with Synapse.. or go straight to some of my tutorials.


Site Map What's NewSearchBEWARE: There is stuff at my other two sites that the search above won't reveal. Go to either site (see links below) and use that site's FreeFind search button.

BEWARE: The search above only visits a selection of my stuff. Go to either of my other sites (see links below) and use that site's FreeFind search button, if you haven't found something you 'know is there'.

In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful...

My other sites..
Sheepdog Software homepage.
My Arunet homepage.

.. and some links to specific pages within them you might want..
You can't 'play' all day.. learn to use the Libre Office/ Open Office database. Free. Multi-platform.
The Arduino- LOTS of fun, for not much money. And beginner (intelligent beginner) friendly. And good pursuit for kids. Combine programming and electronics!
Designing printed circuit boards the KiCad way. Free. Multi-platform. Long established. PCB-fab houses take native KiCad files.
And lastly.. Making maps.. how we did it before GPS Indulge me? This discusses a worthwhile, fun (if presented intelligently) activity for kids, which can be undertaken on many levels.. a simple 20 minutes, or weeks of engaging activity. (Also known to divert susceptible adults.)

Ad from page's editor: Yes. I do enjoy compiling these things for you.. hope they are helpful. However. this doesn't pay my bills!!! If you find this stuff useful, please at least help me with Facebook links, etc (buttons at top of page). If you run an MS-DOS or Windows PC, please visit my freeware and shareware page, try something? Links on your page to this page would also be appreciated, of course!
Click here to visit editor's freeware, shareware page.

Serial Port Component For Lazarus Comes


Lazarus Component Library

Here is the way to contact the author of these Lazarus/Delphi tutorials, Tom Boyd.


Serial port component for lazarus comedy

Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. (Mostly passes. A few problems caused by Google+ and Delicious code.)

If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.

.... P a g e . . . E n d s ...

nameauthorversiondatelinkremarks
libusb.pp2006-06-29http://www.sciencetronics.com/download/fpc_libusb.tgz
libusb.pasJohann Glaser
fpcusbJoe Jared0.11-14download link broken
libusb.ppMarko Medic1.0