[fpc-devel] [PATCH] unix/serial.pp mods for darwin

Brad Campbell brad at wasp.net.au
Wed Oct 1 08:41:46 CEST 2008

Jonas Maebe wrote:
> On 30 Sep 2008, at 17:06, Brad Campbell wrote:
>> These additions/changes make serial.pp work for me on MacOS X 10.4 and 
>> 10.5, both PPC and Intel.
>> Comments?
> Do you also know why these changes are necessary? Adding special cases 
> from a particular platform without any comments (other than "it works 
> now") is unlikely to work in the long term

Index: rtl/unix/serial.pp
--- rtl/unix/serial.pp	(revision 11843)
+++ rtl/unix/serial.pp	(working copy)
@@ -70,7 +70,14 @@

  function SerOpen(const DeviceName: String): TSerialHandle;

This is requires as MacOS will block on the open unless Carrier Detect is asserted. So you need to 
open the port non-blocking and then clear the non-blocking flag. Found this in some apple sample 
code on the apple dev site.

+  Result := fpopen(DeviceName, O_RDWR or O_NOCTTY or O_NONBLOCK);
+  { Remove the O_NONBLOCK }
+  If Result > 0 then
+    FpFCNTL(Result, F_SETFL, 0);
+  {$ELSE}
    Result := fpopen(DeviceName, O_RDWR or O_NOCTTY);
+  {$ENDIF}

  procedure SerClose(Handle: TSerialHandle);
@@ -127,7 +134,11 @@
    tios.c_ispeed := tios.c_cflag;
    tios.c_ospeed := tios.c_ispeed;

This is required as Darwin does not use enums for the baud rate, and simply use the whole numbers. 
In this case some of the baud rates end up asserting weird flags in the control bits. Darwin uses 
c_ispeed/c_ospeed rather than extract the baud rate from c_cflag.

+{$ifndef DARWIN}
    tios.c_cflag := tios.c_cflag or CREAD or CLOCAL;
+  tios.c_cflag := CREAD or CLOCAL;

    case ByteSize of
      5: tios.c_cflag := tios.c_cflag or CS5;
@@ -146,9 +157,10 @@

    if RtsCtsFlowControl in Flags then
      tios.c_cflag := tios.c_cflag or CRTSCTS;

This is actually only required on 10.5 on Intel, but it does not seem to hurt on the other versions.
If left in place, this just blocks and stalls the program.

    tcflush(Handle, TCIOFLUSH);
-  tcsetattr(Handle, TCSANOW, tios)
+  tcsetattr(Handle, TCSANOW, tios);

Dolphins are so intelligent that within a few weeks they can
train Americans to stand at the edge of the pool and throw them

More information about the fpc-devel mailing list