2 Comports in Freepascal Linux Native code ? lazarus / 32-Linux ---------------------------------------------------------------------- pascalbythree · 2022-12-23 09:15 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2022-12-23 09:57 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 ---------------------------------------------------------------------- pascalbythree · 2022-12-23 11:06 Somebody got more simple code? ---------------------------------------------------------------------- alpine · 2022-12-23 11:30 Quote from: pascalbythree on December 23, 2022, 12:06:44 pmSomebody got more simple code?  if it can wait a bit, I can synthesize some code later ---------------------------------------------------------------------- Dzandaa · 2022-12-23 11:43 Hi, I'm interested to know why you can not run two Sdpo Serial at the same time. B-> ---------------------------------------------------------------------- tetrastes · 2022-12-23 17:52 Quote from: pascalbythree on December 23, 2022, 10:15:19 amAnybody 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 · 2022-12-23 18:18 Quote from: pascalbythree on December 23, 2022, 12:06:44 pmSomebody got more simple code?  https://wiki.freepascal.org/Serial_unit ---------------------------------------------------------------------- alpine · 2022-12-23 18:57 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  S: RawByteString;begin   Chan1 := TMyChannel.Create;  Chan1.Port := '/dev/ttySOFT0';  Chan1.Speed := 4800; // The other params are hardcoded at line 87 in mychannel.pas   Chan2 := TMyChannel.Create;  Chan1.Port := '/dev/serial0';  Chan1.Speed := 4800; // The other params are hardcoded at line 87 in mychannel.pas   Chan1.Active := True;  // here exception may be trown, so you can enclose it in try...finally block  Chan2.Active := True;  // here exception may be trown, so you can enclose it in try...finally block   while not some_ending_condition_check do  begin    if Chan1.Get(S, 16, 10) > 0 then // Get accumulated bytes for 10ms up to 16 from chan1      {process received chars from chan1 in S};     if Chan2.Get(S, 16, 10) > 0 then // Get accumulated bytes for 10ms up to 16 from chan2      {process received chars from chan2 in S};     {other processing}  end;   Chan1.Active := False;  Chan2.Active := False;  Chan1.Free;  Chan2.Free;   ...   Please note that: Since the serial ports are internally buffered you can check them at some regular intervals without risking data lossYou 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 aboveYou 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. ---------------------------------------------------------------------- MarkMLl · 2022-12-23 20:27 Quote from: y.ivanov on December 23, 2022, 07:57:54 pmI 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 ---------------------------------------------------------------------- pascalbythree · 2022-12-24 14:51 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.