Recent

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

zanden30@hetnet.nl

  • New Member
  • *
  • Posts: 12
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.

Added: on 11-11-2020
See very simple working solution beneath....
« Last Edit: November 11, 2020, 11:39:01 pm by zanden30@hetnet.nl »

tr_escape

  • Sr. Member
  • ****
  • Posts: 432
  • 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

  • Sr. Member
  • ****
  • Posts: 266
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: 14214
  • Probably until I exterminate Putin.
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.
Specialize a type, not a var.

avra

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

bigeno

  • Sr. Member
  • ****
  • Posts: 266
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

  • New Member
  • *
  • Posts: 12
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.

It is al too complicated.

See solution 11-11-2020. That's easy and works excellent.
« Last Edit: November 11, 2020, 11:40:31 pm by zanden30@hetnet.nl »

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
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.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Most simple serial communication Windows USB COM
« Reply #8 on: November 11, 2020, 07:46:58 pm »
Able? Yes sure. But I'm afraid that I've got more on my plate in that area than I can cope with and I'm /particularly/ not messing around with Windows, so for the moment at least I'd direct you at https://github.com/MarkMLl/ping-arduino-loader which is how I do it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

zanden30@hetnet.nl

  • New Member
  • *
  • Posts: 12
Re: Most simple serial communication Windows USB COM now working
« Reply #9 on: November 11, 2020, 11:32:00 pm »
Thanks.

I have it running now at 38400 baud. With Synaser.
Problem seemed to be port COM14:.
\\\\.\\COM14 or \\.\COM14 or   \\.\COM14: did not work either.

COM1: works mostly finally. But very sometimes it does not work. I'm searching under which circumstances it is not working.....

I only use create, ser.connect, ser.config, ser.sendstring, ser.canread, ser.RecvByte and ser.free.  With a string XOR of CRC check I think it is reliable enough for me. Missing strings is not a problem; they will be resent within 200 ms.

Attached the working code for Newbees....

Code: Pascal  [Select][+][-]
  1. program SerialTest; //Uses Synaser
  2.  
  3. {$mode objfpc}{$H+}
  4. {
  5. Test program for connection between FPC Pascal on Windows and Arduino via USB/COM1: port
  6. The Arduino produces every 5 seconds a string of data with the time.
  7. The Arduino reacts on this type of string ¨´e0xf´¨ with a special answer.
  8. Sending and receiving works well during logn time.
  9. }
  10.  
  11. uses
  12.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  13.   cthreads,
  14.   {$ENDIF}{$ENDIF}
  15.   Classes, SysUtils, Synaser, Crt
  16.   { add other units after this };
  17.  
  18. var
  19.   d         : char = 'a';
  20.   ser       : TBlockSerial;
  21.   SendTime  : comp = 0 ;
  22.  
  23.  
  24. function millis : comp; // time in ms like Arduino
  25.  
  26. begin
  27.   millis :=  DateTimeToTimeStamp(Now).time;
  28. end;
  29.  
  30. Procedure ReceiveData;
  31.  
  32. begin
  33.   while ser.canread(0) do         //reads buffer empty each cycle
  34.     Write(chr(ser.RecvByte(0)));  //IntToHex for binary
  35. end;
  36.  
  37. Procedure SendData;
  38.  
  39. begin
  40.   if SendTime < millis then // once in a second
  41.   begin
  42.     ser.sendstring( 'e0' + d + 'f'); //sends strings in format ´e0xf´; x = d loops
  43.     d := succ(d);                    //no buffer overflow is checked.
  44.     if  d  >= 'f' then              //amount of data sent is too little.
  45.       d := 'a';                     //otherwise use ser.canwrite : boolean;
  46.     SendTime := millis + 974;
  47.   end;
  48. end;
  49.  
  50. Procedure writeStatus;
  51. begin
  52.     Write('Device: '          + ser.Device +
  53.           ' Windows Status: ' + ser.LastErrorDesc +
  54.           ' Error Code '      + Inttostr(ser.LastError));
  55.     writeln;
  56. end;
  57.  
  58. procedure RS232_connect;
  59.  
  60. begin
  61.   ser := TBlockSerial.Create;
  62.   try
  63.     ser.Connect('COM1:'); //ComPort <= 9  check portnumber on PC
  64.     Sleep(1000);          // /dev/ttyUSB0 \\\\.\\ did not work with W10
  65.     ser.config(38400, 8, 'N', SB1, False, False);
  66.     Sleep(1000);
  67.     writeStatus;
  68.    
  69.     while (not keypressed ) and (ser.LastError = 0) do
  70.     begin
  71.       ReceiveData;
  72.       SendData;
  73.       Delay(50); //cool CPU of laptop a bit.....
  74.     end; //never ending loop to receive & send data.
  75.    
  76.   finally
  77.  
  78.     Writeln('Serial Port will be freed...');
  79.     writeStatus;
  80.     ser.free;
  81.     Writeln('Serial Port was freed!');
  82.     writeStatus;
  83.   end;
  84. end;
  85.  
  86. begin //main
  87.   RS232_connect();
  88.   Write('Program quit! Type Enter : ');
  89.   readln;
  90. end.
  91.  
« Last Edit: November 11, 2020, 11:42:28 pm by zanden30@hetnet.nl »

 

TinyPortal © 2005-2018