Recent

Author Topic: 2 Comports in Freepascal Linux Native code ?  (Read 1422 times)

pascalbythree

  • Sr. Member
  • ****
  • Posts: 261
2 Comports in Freepascal Linux Native code ?
« on: December 23, 2022, 10:15:19 am »
Hey FPC fanatics,
I need to read 2 comports at the same time on a Raspberry 3 in the console.
PXL works fine, but does have a Read procedure in it that stops in the line.
Sdpo works fine, but i can not run it by two

i need to open:

/dev/ttySOFT0
/dev/serial0

both at 4800 Baud
So i need some native code to do this. With keep running like:

Code: Pascal  [Select][+][-]
  1. if sdpo.DataAvailable thens S:=sdpo.ReadData;

so it does not wait for a receive string.

Anybody have native code to help me out? Or another component ?

Greetz, Wouter van Wegen PascalByThree




MarkMLl

  • Hero Member
  • *****
  • Posts: 7633
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #1 on: December 23, 2022, 10:57:02 am »
I normally use the serial unit supplied as standard with FPC to do this sort of thing.

It's intentionally low-level, and in most cases you'd have at least one thread running in the background. I tarted it up a bit and made sure it worked with Solaris (i.e. with the BSD-like select() syscall as well as Linux ten years or so ago, specifically to be able to timestamp received data fairly accurately for a serial comms monitor.

Examples at https://forum.lazarus.freepascal.org/index.php/topic,58709.msg438286.html#msg438286

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascalbythree

  • Sr. Member
  • ****
  • Posts: 261
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #2 on: December 23, 2022, 12:06:44 pm »
Somebody got more simple code?  :D

alpine

  • Hero Member
  • *****
  • Posts: 1263
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #3 on: December 23, 2022, 12:30:16 pm »
Somebody got more simple code?  :D
if it can wait a bit, I can synthesize some code later
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Dzandaa

  • Sr. Member
  • ****
  • Posts: 350
  • From C# to Lazarus
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #4 on: December 23, 2022, 12:43:16 pm »
Hi,
I'm interested to know why you can not run two Sdpo Serial at the same time.

B->
 
Regards,
Dzandaa

tetrastes

  • Hero Member
  • *****
  • Posts: 526
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #5 on: December 23, 2022, 06:52:30 pm »
Anybody have native code to help me out? Or another component ?


I recommend synaser.pas from Synapse project: http://synapse.ararat.cz/doku.php, the last version https://sourceforge.net/p/synalist/code/HEAD/tree/trunk/ .

WayneSherman

  • Full Member
  • ***
  • Posts: 249

alpine

  • Hero Member
  • *****
  • Posts: 1263
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #7 on: December 23, 2022, 07:57:54 pm »
Okay,
I think the reply #1 by MarkMLI has everything you need to, incl. how to open, set params, read with timeout (which can be non-blocking if you give a zero timeout) and then close a serial port. If you go through his examples you probably will figure how to make it for two ports at once.

Here is another simple class you can use for the purpose:
https://gist.github.com/alpinistbg/61d103896400f35ee625ad23b22b2120

You can use it like:
Code: Pascal  [Select][+][-]
  1. var
  2.   S: RawByteString;
  3. begin
  4.   Chan1 := TMyChannel.Create;
  5.   Chan1.Port := '/dev/ttySOFT0';
  6.   Chan1.Speed := 4800; // The other params are hardcoded at line 87 in mychannel.pas
  7.  
  8.   Chan2 := TMyChannel.Create;
  9.   Chan1.Port := '/dev/serial0';
  10.   Chan1.Speed := 4800; // The other params are hardcoded at line 87 in mychannel.pas
  11.  
  12.   Chan1.Active := True;  // here exception may be trown, so you can enclose it in try...finally block
  13.   Chan2.Active := True;  // here exception may be trown, so you can enclose it in try...finally block
  14.  
  15.   while not some_ending_condition_check do
  16.   begin
  17.     if Chan1.Get(S, 16, 10) > 0 then // Get accumulated bytes for 10ms up to 16 from chan1
  18.       {process received chars from chan1 in S};
  19.  
  20.     if Chan2.Get(S, 16, 10) > 0 then // Get accumulated bytes for 10ms up to 16 from chan2
  21.       {process received chars from chan2 in S};
  22.  
  23.     {other processing}
  24.   end;
  25.  
  26.   Chan1.Active := False;
  27.   Chan2.Active := False;
  28.   Chan1.Free;
  29.   Chan2.Free;
  30.  
  31.   ...
  32.  
  33.  
  34.  
Please note that:
  • Since the serial ports are internally buffered you can check them at some regular intervals without risking data loss
  • You must include some timeout (3-rd parameter at lines 17,20) otherwise the loop will be so tight it will utilize all of the CPU time
  • It is possible to call Get with zero timeout (e.g. completely non-blocking) when your main loop performs some additional tasks, but you must ensure reasonable delay between calls for the reason above
  • You must process the received bytes one-by-one since there is no guarantee you'll get any sequence in one call (whole line, CR+LF, or whatever)

Hope it helps.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

MarkMLl

  • Hero Member
  • *****
  • Posts: 7633
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #8 on: December 23, 2022, 09:27:57 pm »
I think the reply #1 by MarkMLI has everything you need to, incl. how to open, set params, read with timeout (which can be non-blocking if you give a zero timeout) and then close a serial port. If you go through his examples you probably will figure how to make it for two ports at once.

Most of the complexity in those examples was to make the output look right. After all, they were originally prepared for a Mac user so "pretty" wasn't exactly an optional extra :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

pascalbythree

  • Sr. Member
  • ****
  • Posts: 261
Re: 2 Comports in Freepascal Linux Native code ?
« Reply #9 on: December 24, 2022, 03:51:20 pm »
Thank you, it seems to work!

but still with weird characters

For next upcomming weeks i am going to replace my level shifter board.

Incase it still does not work then you will hear from me again.

Great thanks W v W

Maybe because my scope it running on it or not enough GND wires or to long wires in my enclosue.

 

TinyPortal © 2005-2018