Serial to TCP gateway in FPC?

classic Classic list List threaded Threaded
42 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Serial to TCP gateway in FPC?

Bo Berglund
I need to implement a serial to TCP gateway in order to communicate
between a Windows based control software and a system controller box.
The software and the controller only talk serial to each other and the
protocol is proprietary and binary.
Now I have a situation where the controller is on a remote location
many thousands of km away from here.
I have managed to get some help on the remote site to hook up the
controller serial port to a serial to USB cable plugged into a
Raspberry Pi, which I can reach via VPN from here. So I installed
serial2net on the RPi, making it publish the serial port as a TCP port
(#2091). This part works OK, so whatever is sent on the TCP socket
connection gets transferred to the RS232 serial line and vice versa.

So far so good, but now I also need some kind of serial port to TCP
converter on the Windows 7 PC I use in my end, which is handling the
VPN connection.

So I wonder if there are any FPC examples around for conversion serial
to TCP (where the TCP part is a client rather than a server), which
can compile on Windows. I cannot use any RPi in this end because I
cannot get the RPi to connect by VPN to the remote location...

Ready made example or suggestions on how to go about writing a
converter in FPC are welcome!


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Snorkl e
Couldn't you read the serial port data and then stream it with Synapse to the remote location?
You can use Synapse to read the serial port data and sent via TCP/IP.



On Jun 18, 2017 2:05 AM, "Bo Berglund" <[hidden email]> wrote:
I need to implement a serial to TCP gateway in order to communicate
between a Windows based control software and a system controller box.
The software and the controller only talk serial to each other and the
protocol is proprietary and binary.
Now I have a situation where the controller is on a remote location
many thousands of km away from here.
I have managed to get some help on the remote site to hook up the
controller serial port to a serial to USB cable plugged into a
Raspberry Pi, which I can reach via VPN from here. So I installed
serial2net on the RPi, making it publish the serial port as a TCP port
(#2091). This part works OK, so whatever is sent on the TCP socket
connection gets transferred to the RS232 serial line and vice versa.

So far so good, but now I also need some kind of serial port to TCP
converter on the Windows 7 PC I use in my end, which is handling the
VPN connection.

So I wonder if there are any FPC examples around for conversion serial
to TCP (where the TCP part is a client rather than a server), which
can compile on Windows. I cannot use any RPi in this end because I
cannot get the RPi to connect by VPN to the remote location...

Ready made example or suggestions on how to go about writing a
converter in FPC are welcome!


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
On Sun, 18 Jun 2017 09:46:06 -0500, Snorkl e
<[hidden email]> wrote:

>Couldn't you read the serial port data and then stream it with Synapse to
>the remote location?
>You can use Synapse to read the serial port data and sent via TCP/IP.
>

Probably, but I was hoping for some ready-made example to get going a
bit faster...
I did another piece of software a year or so ago only involving
TCP/IP, also for remote control, but then I used an Indy TCP client
component.
My problem is getting my head around serial port handlers, which I
have so-so experience of in the past. No RS232 work with FPC/Lazarus
earlier and in Delphi I used AsyncPro components, but these are so
complex that one is lost amongst all properties to set...

I would like a dead simple serial component where I could just specify
the comm parameters (baud, start/stop bits, handshaking and port) then
just open the connection and wait for receive *events*.

Yes, it is easier for me to have components with event generation on
data reception rather than using blocking components, which I must
make threads for and then figure out how to channel the data from the
thread into the main application...


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Koenraad Lelong-4
Op 18-06-17 om 18:16 schreef Bo Berglund:

Hi,

Do you know the SDPO-package ? That has a serial component (SDPOSerial).
This has a OnRxData event.

Just a thought. I used it in the past without problems.

HTH

Koenraad.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Snorkl e
In reply to this post by Bo Berglund
Synapse and fpc works great with serial port, I have a daemon I wrote 4 years ago that reads serial data from a phone PBX runs 24X7 and never an issue.

On Jun 18, 2017 11:17 AM, "Bo Berglund" <[hidden email]> wrote:
On Sun, 18 Jun 2017 09:46:06 -0500, Snorkl e
<[hidden email]> wrote:

>Couldn't you read the serial port data and then stream it with Synapse to
>the remote location?
>You can use Synapse to read the serial port data and sent via TCP/IP.
>

Probably, but I was hoping for some ready-made example to get going a
bit faster...
I did another piece of software a year or so ago only involving
TCP/IP, also for remote control, but then I used an Indy TCP client
component.
My problem is getting my head around serial port handlers, which I
have so-so experience of in the past. No RS232 work with FPC/Lazarus
earlier and in Delphi I used AsyncPro components, but these are so
complex that one is lost amongst all properties to set...

I would like a dead simple serial component where I could just specify
the comm parameters (baud, start/stop bits, handshaking and port) then
just open the connection and wait for receive *events*.

Yes, it is easier for me to have components with event generation on
data reception rather than using blocking components, which I must
make threads for and then figure out how to channel the data from the
thread into the main application...


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Paul Breneman
In reply to this post by Bo Berglund
On 06/18/2017 03:04 AM, Bo Berglund wrote:

> I need to implement a serial to TCP gateway in order to communicate
> between a Windows based control software and a system controller box.
> The software and the controller only talk serial to each other and the
> protocol is proprietary and binary.
> Now I have a situation where the controller is on a remote location
> many thousands of km away from here.
> I have managed to get some help on the remote site to hook up the
> controller serial port to a serial to USB cable plugged into a
> Raspberry Pi, which I can reach via VPN from here. So I installed
> serial2net on the RPi, making it publish the serial port as a TCP port
> (#2091). This part works OK, so whatever is sent on the TCP socket
> connection gets transferred to the RS232 serial line and vice versa.
>
> So far so good, but now I also need some kind of serial port to TCP
> converter on the Windows 7 PC I use in my end, which is handling the
> VPN connection.
>
> So I wonder if there are any FPC examples around for conversion serial
> to TCP (where the TCP part is a client rather than a server), which
> can compile on Windows. I cannot use any RPi in this end because I
> cannot get the RPi to connect by VPN to the remote location...
>
> Ready made example or suggestions on how to go about writing a
> converter in FPC are welcome!

Why do you need FPC code?  Here are free drivers that work in Windows:
   http://com0com.sourceforge.net/
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

AndreasGMail
In reply to this post by Snorkl e
I know this is off topic, but does Synapse work on Android? I am
planning on using FpcUpDeluxe to write an android app that has a TCP
client, but am still looking for the TCP component.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
In reply to this post by Paul Breneman
On Sun, 18 Jun 2017 19:16:05 -0400, Paul Breneman
<[hidden email]> wrote:

>Why do you need FPC code?  Here are free drivers that work in Windows:
>   http://com0com.sourceforge.net/

Because I am programming in Pascal (Delphi and FPC) and I had failed
to find any working ready-made solutions.

It looks like I have already tested com0com a year ago or so:

When I look in my PC Win7 registry I find that two ports that are
enumerated but non-working seems to have been set up by com0com and by
what I see in the registry com0com seems to be a service.
However the indicated home dir for com0com does not exist and still
there are all these entries in the registry. There is no registered
service named com0com either...
Looks like leftovers from an incomplete uninstall.

Anyway, I would like to test this again but the question is if it
really works on Windows 7 and up (X64)?
The interesting part would be com0tcp in my case.


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Michael Schnell
In reply to this post by Bo Berglund


On 18.06.2017 18:16, Bo Berglund wrote:
> No RS232 work with FPC/Lazarus
> earlier and in Delphi I used AsyncPro components, but these are so
> complex that one is lost amongst all properties to set...
AsyncPro is free and easy to use but not perfectly bug-free.

Unfortunately no fpc/Lazarus port yet.

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
In reply to this post by Paul Breneman
On Sun, 18 Jun 2017 19:16:05 -0400, Paul Breneman
<[hidden email]> wrote:

>Why do you need FPC code?  Here are free drivers that work in Windows:
>   http://com0com.sourceforge.net/

I tried again and failed again.
I installed com0com and had an error in my face during installation:
SetupUninstallOEMInf(oem11.inf)
ERROR: 0xE000023D -

But it was possible to continue anyway, so I did that.

Afterwards I used the setup utility to make the com pair COM91-COM92.

Then I started com2tcp to connect to the remote port and make it
possible for my local application to use the virtual com port COM91 to
connect to the remote system. But:

com2tcp.exe --baud 38400 --data 8 --parity n --stop 1 --ignore-dsr
COM92 10.0.0.26 2091
OpenC0C(): CreateFile("COM92") ERROR No such file or directory (2)

And my application, which enumerates available serial ports on
startup, does not even show the COM91, COM92 virtual port pair...

So the local application cannot connect to one side of the virtual com
pair and com2tcp cannot connect to the other side...

I would really like to use an available program rather than spening
time to develop something myself, but it seems like com0com does not
cut it...


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Paul Breneman
On 06/19/2017 08:43 AM, Bo Berglund wrote:
> On Sun, 18 Jun 2017 19:16:05 -0400, Paul Breneman
> <[hidden email]> wrote:
>
>> Why do you need FPC code?  Here are free drivers that work in Windows:
>>    http://com0com.sourceforge.net/
>
...
> I would really like to use an available program rather than spening
> time to develop something myself, but it seems like com0com does not
> cut it...

I've used com0com *and* hub4com successfully for several things in the
past.  See #3 here:
   http://www.ctrlterm.com/related.htm

I'll try to take a look at com2tcp in the next few days and compare that
to the Digi One SP (where Win2K drivers are *still* available) which
I've used recently:
https://www.digi.com/products/serial-servers/serial-device-servers/digionesp

BTW, Digi used Delphi & AsyncPro years ago for some test software...

Regards,
Paul
www.ControlPascal.com
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
On Mon, 19 Jun 2017 09:25:59 -0400, Paul Breneman
<[hidden email]> wrote:

>I've used com0com *and* hub4com successfully for several things in the
>past.  See #3 here:
>   http://www.ctrlterm.com/related.htm
>

In the README file with com0com they write:

"INSTALLING
==========

NOTE (x64-based Windows Vista/Windows Server 2008/Windows 7/8):
The com0com.sys is a test-signed kernel-mode driver that will not load
by default. To enable test signing, enter command:

    bcdedit.exe -set TESTSIGNING ON

  and reboot the computer.

NOTE:
  Enabling test signing will impair computer security."

This does not sound good...

During installation I saw several warnings from Windows about unknown
signing of a driver, and Windows recommended not to install...


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Mark Morgan Lloyd-5
In reply to this post by Michael Schnell
On 20/06/17 06:45, Michael Schnell wrote:
> On 18.06.2017 18:16, Bo Berglund wrote:> No RS232 work with FPC/Lazarus>
> earlier and in Delphi I used AsyncPro components, but these are so>
> complex that one is lost amongst all properties to set...AsyncPro is
> free and easy to use but not perfectly bug-free.
> Unfortunately no fpc/Lazarus port yet.

I tend to use the lower-level serial.pp unit. The patches I contributed
a couple of years ago specifically added a couple of read-with-timeout
functions.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Michael Schnell
On 20.06.2017 11:18, Mark Morgan Lloyd wrote:
> I tend to use the lower-level serial.pp unit. The patches I
> contributed a couple of years ago specifically added a couple of
> read-with-timeout functions

IMHO, freeing the user from the necessity to poll or do a thread to wait
for serial events is a very beneficial strategy.

But Koenraad Lelong said in the other mail that "SDPOSerial" has a
OnRxData event. Hopefully by "event" a main thread event is meant.

-Michael
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Paulo Costa-2
On 20-Jun-17 10:45, Michael Schnell wrote:

> On 20.06.2017 11:18, Mark Morgan Lloyd wrote:
>> I tend to use the lower-level serial.pp unit. The patches I
>> contributed a couple of years ago specifically added a couple of
>> read-with-timeout functions
>
> IMHO, freeing the user from the necessity to poll or do a thread to wait
> for serial events is a very beneficial strategy.
>
> But Koenraad Lelong said in the other mail that "SDPOSerial" has a
> OnRxData event. Hopefully by "event" a main thread event is meant.

Yes. That is the case.

Paulo Costa
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
In reply to this post by Mark Morgan Lloyd-5
On Tue, 20 Jun 2017 09:18:50 +0000, Mark Morgan Lloyd
<[hidden email]> wrote:

>I tend to use the lower-level serial.pp unit. The patches I contributed
>a couple of years ago specifically added a couple of read-with-timeout
>functions.
>
Mark, thanks for your input!

I had a look at the serial unit and I found that there are two
overloaded functions SerReadTimeout():

function SerReadTimeout(Handle: TSerialHandle; var Buffer; mSec:
LongInt): LongInt;

function SerReadTimeout(Handle: TSerialHandle; var Buffer: array of
byte; count, mSec: LongInt): LongInt;

In the second of them there is a count argument, presumably indicating
the number of bytes to read from the port. The first is missing this
but sets the value to 1 in the call to ReadFile.

Does this mean that it will only read a single byte and return it
whereas the second version reads the indicated number of bytes and
then returns?
I assume that for both there is a return forced when the timeout
expires.
In both cases the number of actually read bytes are returned in the
result.

Questions:
----------
Do I have to create a thread in order to constantly monitor the state
of the reception?
And I could not find a way to set the comm buffer size, what happens
if I do not check incoming data for a while when there is an inbound
stream. Is there an overflow somewhere?

Anyway it looks promising so far, now I just have to find the proper
TCP client component to use as the far end of the link.
I have used Indy components in the past but I would like something a
bit simpler to use...


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
In reply to this post by Mark Morgan Lloyd-5
On Tue, 20 Jun 2017 09:18:50 +0000, Mark Morgan Lloyd
<[hidden email]> wrote:

>
>I tend to use the lower-level serial.pp unit. The patches I contributed
>a couple of years ago specifically added a couple of read-with-timeout
>functions.

I'm back with a question:
I used the serial example in the hardware access wiki:
http://wiki.freepascal.org/Hardware_Access#FPC_built_in_Serial_unit

as a template and I have a button on my form to open/close the serial
port, but it does not work properly. Below is my button code to open
and close the port depending on the state it is in, which is set by
the global boolean var FComOpen.
The port number is taken from a spinedit speComPort and the baud rate
is from a preloaded combobox cbxBaud:

procedure TForm1.btnConnectComClick(Sender: TObject);
begin
  if FComOpen then
  begin
    //Close com port
    SerSync(FComH); { flush out any remaining before closure }
    SerFlushOutput(FComH); { discard any remaining output }
    SerClose(FComH);
    FComOpen := false;
    TButton(Sender).Caption:= 'Open Serial';
    ledSerial.Brush.Color:= clRed;
  end
  else
  begin
    //Open com port
    FComPortName := 'COM' + IntToStr(speComPort.Value) + ':';
    FComFlags := [ ];
    FBaud := StrToInt(cbxBaud.Text);
    FComH := SerOpen(FComPortName);
    if FComH <> 0 then
    begin
      FComOpen := true;
      SerSetParams(FComH,FBaud,8,NoneParity,1,FComFlags);
      TButton(Sender).Caption:= 'Close Serial';
      ledSerial.Brush.Color:= clLime;
    end;
  end;
end;

The problem I have is that it seems only to work for COM1 or COM3, as
soon as I use a higher numbered port like COM33 then it fails.
What have I missed here?
Is serial only able to work with the low numbered ports (single
digit)?


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

José Mejuto
El 21/06/2017 a las 1:37, Bo Berglund escribió:

> The problem I have is that it seems only to work for COM1 or COM3, as
> soon as I use a higher numbered port like COM33 then it fails.
> What have I missed here?
> Is serial only able to work with the low numbered ports (single
> digit)?

Hello,

COM ports in windows can only be 1-9, to open high numbered COM ports
you must use the extended name syntax (without quotes) "\\.\COM99" you
can also use "\\.\COM1" for COM1-9.


--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Bo Berglund
On Wed, 21 Jun 2017 01:57:36 +0200, José Mejuto
<[hidden email]> wrote:

>> The problem I have is that it seems only to work for COM1 or COM3, as
>> soon as I use a higher numbered port like COM33 then it fails.
>> What have I missed here?
>> Is serial only able to work with the low numbered ports (single
>> digit)?
>
>COM ports in windows can only be 1-9, to open high numbered COM ports
>you must use the extended name syntax (without quotes) "\\.\COM99" you
>can also use "\\.\COM1" for COM1-9.

It turns out that I also must not use the trailing colon in the name
which is present in the wiki example...
But then I get a non-zero handle. I have yet to verify that data can
flow.

Is there a way to enumerate the available com ports so that they can
be listed in a combobox for selection? This would have the advantage
that it would work on both Windows and Linux and it limits the
possible errors by the user. Of course the enumerator must know about
the differences in syntax between Windows and Linux!

What about Linux (Raspberry Pi)? Is the com port name simply ttyUSB1
or do I have to use the full /dev/ttyUSB1?


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Serial to TCP gateway in FPC?

Mark Morgan Lloyd-5
On 21/06/17 05:30, Bo Berglund wrote:
> On Wed, 21 Jun 2017 01:57:36 +0200, José Mejuto<[hidden email]> wrote:
>>> The problem I have is that it seems only to work for COM1 or COM3, as>> soon as I use a higher numbered port like COM33 then it fails.>> What have I missed here?>> Is serial only able to work with the low numbered ports (single>> digit)?>>COM ports in windows can only be 1-9, to open high numbered COM ports >you must use the extended name syntax (without quotes) "\\.\COM99" you >can also use "\\.\COM1" for COM1-9.
> It turns out that I also must not use the trailing colon in the namewhich is present in the wiki example...But then I get a non-zero handle. I have yet to verify that data canflow.

> Is there a way to enumerate the available com ports so that they canbe listed in a combobox for selection? This would have the advantagethat it would work on both Windows and Linux and it limits thepossible errors by the user. Of course the enumerator must know aboutthe differences in syntax between Windows and Linux!

Not reliably in unix. You have to know the various potential name
patterns, i.e. /dev/ttyS*, /dev/ttyAMA* and so on, and then enumerate
what's currently visible in the filesystem. It might be possible to get
"hints" as to the device type from the /sys filesystem, but I'm not sure
it's safe to rely on this.

> What about Linux (Raspberry Pi)? Is the com port name simply ttyUSB1or do I have to use the full /dev/ttyUSB1?

You *definitely* have to use the name as given. You're opening a file in
the filesystem tree, not an internally-reserved name.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
123