Most simple serial communication Windows USB COM lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- zanden30@hetnet.nl · 2018-03-14 00:42 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i assign(f, 'COM1:'); read(f, c); write(f, c); 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.... ---------------------------------------------------------------------- tr_escape · 2018-03-14 07:26 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 ---------------------------------------------------------------------- bigeno · 2018-03-14 07:32 Im using synaser from Synapse. simple and works good. ---------------------------------------------------------------------- Thaddy · 2018-03-14 08:08 Quote from: bigeno on March 14, 2018, 08:32:33 amIm 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. ---------------------------------------------------------------------- avra · 2018-03-14 09:06 Quote from: zanden30@hetnet.nl on March 14, 2018, 01:42:58 amI 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).http://wiki.lazarus.fr eepascal.org/Hardware_Access#Serial_Communication https://forum.lazarus.freepascal.org/index.php/topic,33423.msg266388.h tml#msg266388 http://wiki.lazarus.freepascal.org/Arduino#Minimal_Lazarus_Synaser_ser ial_example https://forum.lazarus.freepascal.org/index.php/topic,36885.msg246260.h tml#msg246260 ---------------------------------------------------------------------- bigeno · 2018-03-14 09:12 Quote from: Thaddy on March 14, 2018, 09:08:31 amIndeed. 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     RegINI := TRegistry.Create;    with RegINI do       try          RootKey := HKEY_LOCAL_MACHINE;          if KeyExists('HARDWARE') and OpenKeyReadOnly('HARDWARE') then begin             if KeyExists('DEVICEMAP') and OpenKeyReadOnly('DEVICEMAP') then begin              if KeyExists('SERIALCOMM') and OpenKeyReadOnly('SERIALCOMM') then begin                   AList := TStringList.Create;                   try                      GetValueNames(AList);                      loop := 0;                    while loop <= AList.Count-1 do begin                         if ComName.Items.IndexOf(ReadString(AList[loop])) < 0 then begin                          ComName.Items.Clear;                            for i:=0 to AList.Count-1 do                               ComName.Items.Add(ReadString(AList[i]));                            break;                         end;                         inc(loop);                      end;                   finally                    AList.Free;                   end;                end;           end;          end;       finally          RegINI.Free;       end; ---------------------------------------------------------------------- zanden30@hetnet.nl · 2018-03-14 11:30 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. ---------------------------------------------------------------------- SymbolicFrank · 2018-03-14 12:59 You can even use: TStringList.SaveToFile('COM12'); TStringList.LoadFromFile('COM12'); works as well, if the sender stays within the specs. ---------------------------------------------------------------------- MarkMLl · 2020-11-11 18:46 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 ---------------------------------------------------------------------- zanden30@hetnet.nl · 2020-11-11 22:32 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program SerialTest; //Uses Synaser {$mode objfpc}{$H+}{Test program for connection between FPC Pascal on Windows and Arduino via USB/COM1: portThe Arduino produces every 5 seconds a string of data with the time.The Arduino reacts on this type of string ¨´e0xf´¨ with a special answer.Sending and receiving works well during logn time.} uses  {$IFDEF UNIX}{$IFDEF UseCThreads}  cthreads,  {$ENDIF}{$ENDIF}  Classes, SysUtils, Synaser, Crt  { add other units after this }; var  d         : char = 'a';  ser       : TBlockSerial;  SendTime  : comp = 0 ;  function millis : comp; // time in ms like Arduino begin  millis :=  DateTimeToTimeStamp(Now).time;end; Procedure ReceiveData; begin  while ser.canread(0) do         //reads buffer empty each cycle    Write(chr(ser.RecvByte(0)));  //IntToHex for binaryend; Procedure SendData; begin  if SendTime < millis then // once in a second  begin    ser.sendstring( 'e0' + d + 'f'); //sends strings in format ´e0xf´; x = d loops    d := succ(d);                    //no buffer overflow is checked.    if  d  >= 'f' then              //amount of data sent is too little.      d := 'a';                     //otherwise use ser.canwrite : boolean;    SendTime := millis + 974;  end;end; Procedure writeStatus;begin    Write('Device: '          + ser.Device +          ' Windows Status: ' + ser.LastErrorDesc +        ' Error Code '      + Inttostr(ser.LastError));    writeln;end; procedure RS232_connect; begin  ser := TBlockSerial.Create;  try    ser.Connect('COM1:'); //ComPort <= 9  check portnumber on PC    Sleep(1000);          // /dev/ttyUSB0 \\\\.\\ did not work with W10    ser.config(38400, 8, 'N', SB1, False, False);    Sleep(1000);    writeStatus;        while (not keypressed ) and (ser.LastError = 0) do    begin      ReceiveData;      SendData;    Delay(50); //cool CPU of laptop a bit.....    end; //never ending loop to receive & send data.      finally     Writeln('Serial Port will be freed...');    writeStatus;    ser.free;    Writeln('Serial Port was freed!');    writeStatus;  end;end; begin //main  RS232_connect();  Write('Program quit! Type Enter : ');  readln;end.