[fpc-pascal] Serial port access on Darwin PPC

Brad Campbell brad at wasp.net.au
Tue Sep 23 14:24:07 CEST 2008

Jonas Maebe wrote:
> On 23 Sep 2008, at 12:51, Brad Campbell wrote:
>> Can any Mac people tell me why
>> FHandle := fpOpen(Port, O_RDWR or O_NOCTTY);
>> (Where FHandle is a longint and port is '/dev/tty.usbserial-A400379A')
>> .. just locks up waiting ?
> No, I can't tell you why this happens. Does the same happen if you do it 
> in a C program?
>> Obviously I'm doing something dumb, I just don't know how to debug it. 
>> No strace on MacOS.
> There's druss on Mac OS X 10.5.x and higher, and ktrace/kdump on Mac OS 
> X 10.4.x and lower. The rosetta stone for Unix is your friend: 
> http://bhami.com/rosetta.html

Ok, I think I've found an obvious bug in rtl/unix/serial.pp.

tios.c_cflag is used to obtain the baudrate constant, but it's not then cleared before or'ing the 
other constants into it. Strangely enough this works on linux x86, but breaks badly in MacOS PPC.

This patch makes it work for me on Linux X86 and OSX PPC.
I'm not sure about the side effect of existing users to adding the O_NONBLOCK in there, but this 
_must_ be in place for the port to open in MacOS if there is no carrier detect.

Personally I'm wrapping this in a class and using select() on the FD to tell if I can read/write.. I 
wonder if there would be any value in me adding blocking/nonblocking/timed read/write procedures to 
this unit?

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

  function SerOpen(const DeviceName: String): TSerialHandle;
-  Result := fpopen(DeviceName, O_RDWR or O_NOCTTY);
+  Result := fpopen(DeviceName, O_RDWR or O_NOCTTY or O_NONBLOCK);

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

-  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;
Index: rtl/darwin/Makefile.fpc
--- rtl/darwin/Makefile.fpc	(revision 11816)
+++ rtl/darwin/Makefile.fpc	(working copy)
@@ -16,7 +16,7 @@
        errors terminfo termio video crt mouse keyboard console \
        variants dateutils convutils stdconvs \
        sysconst cthreads strutils rtlconsts cwstring bsd fmtbcd \
-      clocale
+      clocale serial

  rsts=math varutils typinfo classes variants dateutils sysconst rtlconsts

Dolphins are so intelligent that within a few weeks they can
train Americans to stand at the edge of the pool and throw them
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fpc.patch
Type: text/x-diff
Size: 1196 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20080923/03ad5803/attachment.patch>

More information about the fpc-pascal mailing list