const
TCGETS2 = $802C542A; // Not defined in FPC
TCSETS2 = $402C542B; // Not defined in FPC
NCCS = 19; // value in FPC (32) doesn't work/seems incorrect
type
{$PACKRECORDS C}
Termios_ = record
c_iflag,
c_oflag,
c_cflag,
c_lflag : cardinal;
c_line : char;
c_cc : array[0..NCCS-1] of byte;
c_ispeed,
c_ospeed : cardinal;
end;
procedure _SerSetParams(Handle: TSerialHandle; BitsPerSec: LongInt;
ByteSize: Integer; Parity: TParityType; StopBits: Integer;
Flags: TSerialFlags);
var
tios: termios_;
customBAUD: boolean;
begin
FillChar(tios, SizeOf(tios), #0);
customBAUD := false;
case BitsPerSec of
50: tios.c_cflag := B50;
75: tios.c_cflag := B75;
110: tios.c_cflag := B110;
134: tios.c_cflag := B134;
150: tios.c_cflag := B150;
200: tios.c_cflag := B200;
300: tios.c_cflag := B300;
600: tios.c_cflag := B600;
1200: tios.c_cflag := B1200;
1800: tios.c_cflag := B1800;
2400: tios.c_cflag := B2400;
4800: tios.c_cflag := B4800;
19200: tios.c_cflag := B19200;
38400: tios.c_cflag := B38400;
57600: tios.c_cflag := B57600;
115200: tios.c_cflag := B115200;
230400: tios.c_cflag := B230400;
{$ifndef BSD}
460800: tios.c_cflag := B460800;
{$endif}
else
begin
customBAUD := true;
tios.c_cflag := CBAUDEX;
tios.c_iflag := IGNPAR or IGNBRK;
tios.c_oflag := 0;
tios.c_lflag := 0;
tios.c_ispeed := BitsPerSec;
tios.c_ospeed := BitsPerSec;
tios.c_cc[VMIN] := 0; // Return as soon as one byte is available
tios.c_cc[VTIME] := 5; // 0.5 seconds timeout per byte
end;
end;
tios.c_cflag := tios.c_cflag or CREAD or CLOCAL;
case ByteSize of
5: tios.c_cflag := tios.c_cflag or CS5;
6: tios.c_cflag := tios.c_cflag or CS6;
7: tios.c_cflag := tios.c_cflag or CS7;
else tios.c_cflag := tios.c_cflag or CS8;
end;
case Parity of
OddParity: tios.c_cflag := tios.c_cflag or PARENB or PARODD;
EvenParity: tios.c_cflag := tios.c_cflag or PARENB;
end;
if StopBits = 2 then
tios.c_cflag := tios.c_cflag or CSTOPB;
if RtsCtsFlowControl in Flags then
tios.c_cflag := tios.c_cflag or CRTSCTS;
tcflush(Handle, TCIOFLUSH);
if customBAUD then
FpIOCtl(handle, TCSETS2, @tios)
else
tcsetattr(Handle, TCSANOW, Termios(pointer(@tios)^));
end;