* * *

Author Topic: Most simple serial communication Windows USB COM  (Read 2352 times)

zanden30@hetnet.nl

  • Newbie
  • Posts: 4
Most simple serial communication Windows USB COM
« on: March 14, 2018, 01:42:58 am »
I want to send and receive some bytes via a serial port USB (COMx) from a W10 PC to a Paying terminal with USB serial COM connection (it behaves like the serial communication with an Arduino board).

In Windows I can set the baudrate and parity/stop bit

What is the most easy way with FPC to realise this.
I would just like to write something like this in Pascal:

Code: Pascal  [Select]
  1. assign(f, 'COM1:');
  2.  
  3. read(f, c);
  4.  
  5. write(f, c);
  6.  
It is not that fast, so  even buffering is strictly not needed. allthough it would be nice. A combination with a .ddl perhaps?

Who can give me some help? I only found big libraries, Lazarus OO etc. I just want something simple.

tr_escape

  • Sr. Member
  • ****
  • Posts: 332
  • sector name toys | respect to spectre
    • Github:
Re: Most simple serial communication Windows USB COM
« Reply #1 on: March 14, 2018, 08:26:03 am »
Hello,

You can use TLazSerial package for windows and linux OS.

https://github.com/JurassicPork/TLazSerial

Direct download:

http://packages.lazarus-ide.org/LazSerial.zip


But if you look for very simple example is in this forum:

http://www.swissdelphicenter.ch/en/showcode.php?id=841


Extra info:

http://delphi-kb.blogspot.com.tr/2009/03/how-to-communicate-with-com-port.html

« Last Edit: March 14, 2018, 08:32:04 am by tr_escape »

bigeno

  • Full Member
  • ***
  • Posts: 211
Re: Most simple serial communication Windows USB COM
« Reply #2 on: March 14, 2018, 08:32:33 am »
Im using synaser from Synapse. simple and works good.

Thaddy

  • Hero Member
  • *****
  • Posts: 6728
Re: Most simple serial communication Windows USB COM
« Reply #3 on: March 14, 2018, 09:08:31 am »
Im using synaser from Synapse. simple and works good.
Indeed. Synaser is stable and very easy ti use.

Note that USB COM ports usually map to a higher number than COM1, so you need to check that. On my laptops it usually is COM12 under Windows 10.
Note if you know the comport number (or enumerate them) you can indeed use similar code as your example, but be aware that a slightly more involved library like Synapse/Synaser gives you much more control.
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

avra

  • Hero Member
  • *****
  • Posts: 1345
    • Additional info
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

bigeno

  • Full Member
  • ***
  • Posts: 211
Re: Most simple serial communication Windows USB COM
« Reply #5 on: March 14, 2018, 10:12:05 am »
Indeed. Synaser is stable and very easy ti use.

Note that USB COM ports usually map to a higher number than COM1, so you need to check that. On my laptops it usually is COM12 under Windows 10.
Note if you know the comport number (or enumerate them) you can indeed use similar code as your example, but be aware that a slightly more involved library like Synapse/Synaser gives you much more control.
btw, listing used com ports, tested on win7,10, from my app:
Code: Pascal  [Select]
  1.     RegINI := TRegistry.Create;
  2.     with RegINI do
  3.        try
  4.           RootKey := HKEY_LOCAL_MACHINE;
  5.           if KeyExists('HARDWARE') and OpenKeyReadOnly('HARDWARE') then begin
  6.              if KeyExists('DEVICEMAP') and OpenKeyReadOnly('DEVICEMAP') then begin
  7.                 if KeyExists('SERIALCOMM') and OpenKeyReadOnly('SERIALCOMM') then begin
  8.                    AList := TStringList.Create;
  9.                    try
  10.                       GetValueNames(AList);
  11.                       loop := 0;
  12.                       while loop <= AList.Count-1 do begin
  13.                          if ComName.Items.IndexOf(ReadString(AList[loop])) < 0 then begin
  14.                             ComName.Items.Clear;
  15.                             for i:=0 to AList.Count-1 do
  16.                                ComName.Items.Add(ReadString(AList[i]));
  17.                             break;
  18.                          end;
  19.                          inc(loop);
  20.                       end;
  21.                    finally
  22.                       AList.Free;
  23.                    end;
  24.                 end;
  25.              end;
  26.           end;
  27.        finally
  28.           RegINI.Free;
  29.        end;

zanden30@hetnet.nl

  • Newbie
  • Posts: 4
Re: Most simple serial communication Windows USB COM
« Reply #6 on: March 14, 2018, 12:30:27 pm »
Thanks for the suggestions and overview. I will try one.

I think a good instruction/overview with adequate keywords on this topic is missing in de FPC documentation/Wiki. With pro's en con's of the different solutions.

SymbolicFrank

  • Sr. Member
  • ****
  • Posts: 393
Re: Most simple serial communication Windows USB COM
« Reply #7 on: March 14, 2018, 01:59:42 pm »
You can even use: TStringList.SaveToFile('COM12');

TStringList.LoadFromFile('COM12'); works as well, if the sender stays within the specs.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus