Custom BAUD rate lazarus / 32-Linux ---------------------------------------------------------------------- ccrause · 2018-04-07 10:57 I want to open serial connections and specify arbitrary BAUD rates. I can do this if I modify the SerSetParams procedure in serial.pp as follows: 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 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;  My question is whether changing the value of NCCS in FPC is the correct way (the maximum constant value declared for c_cc is only 16), at least on Linux 64 target? ---------------------------------------------------------------------- kupferstecher · 2018-04-07 13:30 I can't answer you, but doesn't the Baudrate depend on the clock frequency (of the serial controller)? So it maybe isn't possible to arbitrarily choose the baudrate? ---------------------------------------------------------------------- ccrause · 2018-04-07 15:16 Quote from: kupferstecher on April 07, 2018, 03:30:47 pmI can't answer you, but doesn't the Baudrate depend on the clock frequency (of the serial controller)? So it maybe isn't possible to arbitrarily choose the baudrate? You are right, but that doesn't mean that a serial controller cannot run at many more possible baud rates than the "standard" values.  This is required for example by the debugwire protocol, which runs at the target MCU clock/128.  So if the controller is running at 8 MHz serial communication needs to occur at 62500 bit/s.  So far I've tested that the following serial-USB converters can handle custom data rates: CP2102, FT232, the serial-USB bridge on an Arduino Uno and the serial port of Pololu's USB AVR programmer. It seems fairly common nowadays.