[fpc-devel] serial under linux, SerOpen blocks

peter green plugwash at p10link.net
Thu Aug 18 11:10:48 CEST 2011


Armin Diehl wrote:
> I need some hint regarding opening a serial (ttyUSB) port using the 
> unit serial. Minicom works fine under my user id. When i call 
> SerOpen('/dev/ttyUSB0'), fpopen blocks forever. When i first start 
> minicom on that port, SerOpen and all other functions of serial.pp 
> works fine.
I don't use serial.pp but here is how I open a serial port for basic 
communication (no flow control, modem control or stuff like that) under 
linux. I remember this taking some experimentation to get right.

procedure tlserial.open;
var
  fd : longint;
  config : termios;
  baudrateos : longint;
begin
  fd := fpopen(device,O_RDWR or O_NOCTTY);
  
  if isatty(fd)=0 then begin
    writeln('not a tty');
    halt(1);
  end;
 
  fillchar(config,sizeof(config),#0);
  config.c_cflag := CLOCAL or CREAD;
  cfmakeraw(config);
  case baudrate of
    50:     baudrateos := B50;
    75:     baudrateos := B75;
    110:    baudrateos := B110;
    134:    baudrateos := B134;
    150:    baudrateos := B150;
    200:    baudrateos := B200;
    300:    baudrateos := B300;
    600:    baudrateos := B600;
    1200:   baudrateos := B1200;
    1800:   baudrateos := B1800;
    2400:   baudrateos := B2400;
    4800:   baudrateos := B4800;
    9600:   baudrateos := B9600;
    19200:  baudrateos := B19200;
    38400:  baudrateos := B38400;
    57600:  baudrateos := B57600;
    115200: baudrateos := B115200;
    230400: baudrateos := B230400; 
    else raise exception.create('unrecognised baudrate');
  end;
  cfsetispeed(config,baudrateos);
  cfsetospeed(config,baudrateos);
  config.c_cc[VMIN]  := 1;
  config.c_cc[VTIME] := 0;
  if   tcsetattr(fd,TCSAFLUSH,config) <0 then begin
    writeln('could not set termios attributes');
    halt(3);
  end;
  dup(fd);
  closehandles := true;
end;



More information about the fpc-devel mailing list