Recent

Author Topic: TCP to RS232 converter with Lazarus?  (Read 11231 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 484
TCP to RS232 converter with Lazarus?
« on: March 03, 2016, 02:15:13 pm »
I would like to create a small server program, which is capable of communicating with a data collection system using RS232 on one side and a network client on the other side. All data should be passed as-is between the endpoints.
So I figured I could use a TIdTcpServer component for the network side (Indy10) to which the remote unit could connect.
Then I also need some serial component to talk to the data collection unit with, and here I don't really know what would be a proper choice.
In any case this "server" application would sit idle listening for a TCP connection on say port 10001.
When a connection is made then it would open the serial port towards the data collection unit and start waiting for data in both directions. In fact the serial port might be open all the time the program runs because this is a fixed installation application...

Anything coming in on the TCP port should be sent to the serial port and anything coming in from serial should be sent back to the TCP client.
There is to be no handling of any protocol whatsoever, ALL bytes must be transferred exactly as received.

The target is Linux, either on PC hardware or on a Raspberry Pi unit.

On a Windows PC target I would have used Delphi where I have Turbo AsyncPro available for the serial port. But AsyncPro is not available for FreePascal so I am at a blank here.  :(

Is there existing code already available somewhere that can accomplish this?
Otherwise what are your suggestions?
I have read this example of port forwarding TCP <=> TCP and it is instructive, but it does not deal with the serial port at all and this seems to be the hard part....
« Last Edit: March 03, 2016, 02:18:04 pm by BosseB »
--
Bo Berglund
Sweden

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: TCP to RS232 converter with Lazarus?
« Reply #1 on: March 03, 2016, 02:25:05 pm »
I think Synaser should work on WinCE.

What do you plan to do with slow or dropped connections? What about multiple connections to a single serial port?

mig-31

  • Sr. Member
  • ****
  • Posts: 308
Re: TCP to RS232 converter with Lazarus?
« Reply #2 on: March 03, 2016, 04:11:20 pm »
Hi,

you can use SynaSer unit from Synapse library to communicate through RS232 with data collection system works on Linux x86,x86_64 and ARM.

For TCP server you can use also Synapse or Lnet library. Both work under Linux Linux x86,x86_64 and ARM.
Lazarus 4.0 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: TCP to RS232 converter with Lazarus?
« Reply #3 on: March 03, 2016, 06:01:21 pm »
Quote
What do you plan to do with slow or dropped connections?
What about multiple connections to a single serial port?
The unit is meant to be connected to by a single client. I.e. I will have to set the maxconnected client count to 1 on teh IdTcpServer object (I don't remember right now what teh exat name of that property is).

There is a protocol between the endpoints so if there is data loss it will be detected, but the converter I am trying to build will have to be a dumb proxy and transfer the bytes as-is.

Quote
I think Synaser should work on WinCE.
Concerning SynaSer I have found it by googling but it seems like it is part of a bigger library of objects named Synapse.
Is there any code examples for using SynaSer on Linux?
I found only reference to examples which used TCP/IP...

I have also seen mention of a unit named "serial" but it does not resolve in my Lazarus 1.6 with FPC 3.0.0
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: TCP to RS232 converter with Lazarus?
« Reply #4 on: March 03, 2016, 08:02:24 pm »
I have seen in the Lazarus wiki on HW access that there is a serial component included with FPC.
So I will investigate this.
SynaSer seems to need a download plus installation in order to work and that process is poorly described.

I downloaded the zip and expanded into my directory for such items like fpc, lazarus and indy. Then I opened the package file and compiled it. But it could not be installed, but Lazarus offered to add it to the project.
This produced an entry on the uses clause but Lazarus cannot find the files...
So for the moment I will investigate what is there by default instead.
That means Serial from fpc and my installation of Indy10 (which I always do).
--
Bo Berglund
Sweden

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TCP to RS232 converter with Lazarus?
« Reply #5 on: March 03, 2016, 08:18:12 pm »
Have you considered using USB FTDI? The FTDI chip allows you to hand things of to a microcontroller to do all the heavy lifting. There is a good component library 'dataport' that has a TDataPortFTDI component ready to go.



BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: TCP to RS232 converter with Lazarus?
« Reply #6 on: March 03, 2016, 10:29:52 pm »
I have used the FTDI serial to USB component on smaller controller boards I designed.
But that was for special cases using a Microchip PIC controller.

This current use is actually targeting a Raspberry Pi2 board, which runs a dialect of Debian Jessie and I do not intend to design a board of my own for it. In fact this converter is intended to be used as a simulator for debugging other code during development.
I thought that a program with a TCP server plus a serial port would be fairly simple to do. In Delphi it is on Windows, but I can't use Windows in this case.
--
Bo Berglund
Sweden

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12599
  • FPC developer.
Re: TCP to RS232 converter with Lazarus?
« Reply #7 on: March 03, 2016, 10:37:12 pm »
At work I communicate with my Microchip controllers using rs232 (sometimes with RS422<->RS232 converters to improve distance) using TComport.

I mostly do that using Delphi, but I the code readily compiled and worked with Lazarus too.

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TCP to RS232 converter with Lazarus?
« Reply #8 on: March 03, 2016, 10:58:37 pm »
Also included in the 'Dataport' components pack is the TDataportHTTP, TDataPortSerial,...TCP,...UDP, ...PIPES, and TDataPortFILE components. It is based on the Synapse library. The TDataportSerial deals directly with the synaser.pas. I really like it's 'push' and 'peek' methods for dealing with serial data. Unfortunately, I cannot find any documentation on these components. Even still, they are pretty self explanatory. Definitely worth considering, from what I can tell.


BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: TCP to RS232 converter with Lazarus?
« Reply #9 on: March 03, 2016, 11:22:45 pm »
I guess you mean this download?
I just found it while googling for serial ports, but I am not comfortable with installing it in Lazarus.
Could you give some advice? It really seem promising when reading about it.

So far I had already downloaded the synapse40.zip file and unpacked it into a location I use for downloaded files for development, in this case ~/dev/synapse40. I next opened the contained package file in Lazarus and compiled it, but it could not be installed (warning from Lazarus).
So I let it be hoping the compile would have told Lazarus where it is. But it could not be found by my application...
So I have to somehow tell Lazarus where the synapse package laz_synapse.lpk is located.
In the install instructions it says:
Quote
add \Source\Lib directory to library or search path.
But when I open the Lazarus Tools/Options dialogue there is no library or search path box to enter this into. :(
In Delphi the corresponding place has an entry for these items.

And then when I read the info on dataport it states that it requires the laz_synapse package to be installed....
So how exactly is this done?
So far I have only used what comes with Lazarus itself except for indy10, which I installed from the same dialog where I opened the laz-synapse package. For Indy there were no warnings and everything just worked.
A bit confused about laz_synapse package...

Uisng FPC 3.0.0 and Lazarus 1.6 on a Linux Mint 17.3 Mate virtual machine.
--
Bo Berglund
Sweden

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TCP to RS232 converter with Lazarus?
« Reply #10 on: March 03, 2016, 11:31:02 pm »
You should install the laz_synapse.lpk. Then the  dataportlazarus.lpk from dataport download. Installing components is easy. You open an .lpk file, compile it, the recompile the full ide. Read the details on the wiki for an in depth how to.

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: TCP to RS232 converter with Lazarus?
« Reply #11 on: March 03, 2016, 11:45:18 pm »
Well,
I did this and when I opened the dataportlazarus.lpk file in Packages submenu and hit Compile I got this error message:
Code: Pascal  [Select][+][-]
  1. Compile package DataPortLasarus 1.0.1: Exit code 256, Errors: 1, Hints: 1
  2. DataPort.pas(61,20) Hint: Parameter "InitStr" not used
  3. D2XXUnit.pas(5,6) Fatal: Cannot find Windows used by D2XXUnit.
  4.  
It seems like this unit has not been provided with the needed ifdef sets to make it able to differentiate between Windows and Unix. :(
--
Bo Berglund
Sweden

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TCP to RS232 converter with Lazarus?
« Reply #12 on: March 03, 2016, 11:55:09 pm »
Well,
I did this and when I opened the dataportlazarus.lpk file in Packages submenu and hit Compile I got this error message:
Code: Pascal  [Select][+][-]
  1. Compile package DataPortLasarus 1.0.1: Exit code 256, Errors: 1, Hints: 1
  2. DataPort.pas(61,20) Hint: Parameter "InitStr" not used
  3. D2XXUnit.pas(5,6) Fatal: Cannot find Windows used by D2XXUnit.
  4.  
It seems like this unit has not been provided with the needed ifdef sets to make it able to differentiate between Windows and Unix. :(

You need to open the .lpk's from the menu package|open package file. Find the .lpk file. This opens the package manager. Hit the compile button. That's all you do for the laz_synapse package. Close the package manager.

Do the same steps for the Dataport.lpk. Except this time, after you compile it, you then press the 'use' button which has a popup menu. Choose 'install'. It will prompt you if you want to 'rebuild Lazarus'. Hit ok. This will recompile the ide and include the new components on the component palette.

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: TCP to RS232 converter with Lazarus?
« Reply #13 on: March 04, 2016, 12:09:06 am »
I did exactly that...
Compile of laz_synapse done.
Open the dataportlazarus.lpk file in the same package/open package file dialog and hit Compile.
This is when the error appears.
All done in a LinuxMint 17.3 Mate environment where Windows should not be in the uses clause...

This is the start of the file D2XXUnit.pas file:
Code: Pascal  [Select][+][-]
  1. unit D2XXUnit;
  2.  
  3. interface
  4.  
  5. Uses Windows, SysUtils, Classes;
  6.  
  7. Type FT_Result = Integer;
  8.  
In my view there should be a construct like what is seen further down in the same file already in the uses section (line 286 ff):
Code: Pascal  [Select][+][-]
  1. // DLL Name
  2. {$ifdef WINDOWS}
  3.     FT_DLL_Name = 'FTD2XX.DLL';
  4. {$else}
  5.     FT_DLL_Name = 'FTD2XX';
  6. {$endif}
  7.  
But I don't know what to put in place of the non-escaped uses clause and I don't feel comfortable modifying code that is supposed to work unchanged.
--
Bo Berglund
Sweden

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: TCP to RS232 converter with Lazarus?
« Reply #14 on: March 04, 2016, 12:25:15 am »
I have never tried it with Linux. It is based on a library that was ported from Delphi so might need work for getting it set up for cross-platform.

If you are serious about learning to use serial functionality you might do best by dealing directly with the synaser.pas. It is well documented and might be the most productive route vs a wrapper library/components that are not very well documented, like dataport is. Just bypass the components and learn it hands on and use the convenience of components after you get a good grip on the synaser.pas itself.


 

TinyPortal © 2005-2018