Difference between revisions of "Serial unit"

From Free Pascal wiki
Jump to navigationJump to search
Line 1: Line 1:
 
Unit '''Serial''' in FPC supports work with serial port. It provides many Ser* functions.
 
Unit '''Serial''' in FPC supports work with serial port. It provides many Ser* functions.
 +
 +
==Example of usage==
 +
 +
<syntaxhighlight lang="pascal">
 +
Program TestSerialPortCom;
 +
{
 +
  Usage options:
 +
  TestSerialPortCom  => uses default COM1
 +
  TestSerialPortCom 8 'Hello' => uses COM8 and output 'Hello' before waiting for an answer
 +
 
 +
  the program will open a serialport and output 'Hello', after that the code will wait until
 +
  a CR (#13) is received, or a key is pressed.
 +
}
 +
uses
 +
  serial, crt;
 +
 +
VAR
 +
  serialhandle : LongInt;
 +
  ComPortName  : String;
 +
  s,tmpstr,txt : String;
 +
  ComOut,ComIn : String;
 +
  ComPortNr    : integer;
 +
  writecount  : integer;
 +
  status      : LongInt;
 +
  BitsPerSec  : LongInt;
 +
  ByteSize    : Integer;
 +
  Parity      : TParityType; { TParityType = (NoneParity, OddParity, EvenParity); }
 +
  StopBits    : Integer;
 +
  Flags        : TSerialFlags; { TSerialFlags = set of (RtsCtsFlowControl); }
 +
  ErrorCode    : Integer;
 +
 +
BEGIN
 +
  ComPortNr:= 1;
 +
  tmpstr:= '';
 +
  txt:= '';
 +
 +
  writeln('Parameters ', ParamCount);
 +
  if (ParamCount>0) then
 +
  begin
 +
    tmpstr:= ParamStr(1);
 +
    val(tmpstr, ComPortNr, ErrorCode);
 +
 +
    if (ParamCount>1) then
 +
    begin
 +
      txt:= ParamStr(2);
 +
      {val(tmpstr,ComPortNr,ErrorCode);}
 +
    end;
 +
  end;
 +
 +
  str(ComPortNr,tmpstr);
 +
 +
  ComPortName:= 'COM'+tmpstr+':';
 +
  writeln('Using '+ComPortname);
 +
 +
  serialhandle := SerOpen(ComPortName);
 +
  Flags:= [ ]; // None
 +
  SerSetParams(serialhandle,9600,8,NoneParity,1,Flags);
 +
 
 +
  s:=txt; // use the input text
 +
  writeln('OUT '+s);
 +
  s:=s+#13+#10; { CR + LF }
 +
  writecount:= length(s);
 +
 +
  status:= SerWrite(serialhandle, s[1], writecount );
 +
 +
  // The next line is for debugging only!
 +
  writeln('status: ', status, '  writecount: ', writecount);
 +
 +
  if status > 0 then
 +
  begin
 +
    writeln('Waiting for answer');
 +
    { wait for an answer }
 +
    s:='';
 +
    ComIn:='';
 +
    while (Length(Comin)<10) and (status>=0) and not keypressed do begin
 +
 +
      status:= SerRead(serialhandle, s[1], 10);
 +
      if (s[1]=#13) then status:=-1; { CR => end serial read }
 +
 +
      if (status>0) then ComIn:=ComIn+s[1];
 +
      if (status>0) then begin
 +
        writeln(status,' ',length(ComIn),' ASCII ',ord(s[1]),' INP ',ComIn);
 +
      end;
 +
    end;
 +
  end
 +
  else
 +
    writeln('ERROR - Unable to send.');
 +
 +
  SerSync(serialhandle); { flush out any remaining before closure }
 +
 +
  SerFlushOutput(serialhandle); { discard any remaining output }
 +
 +
  SerClose(serialhandle);
 +
END.
 +
</syntaxhighlight>
  
 
==When timeout starts==
 
==When timeout starts==

Revision as of 16:28, 19 September 2022

Unit Serial in FPC supports work with serial port. It provides many Ser* functions.

Example of usage

Program TestSerialPortCom;
{
  Usage options:
  TestSerialPortCom  => uses default COM1
  TestSerialPortCom 8 'Hello' => uses COM8 and output 'Hello' before waiting for an answer
  
  the program will open a serialport and output 'Hello', after that the code will wait until
  a CR (#13) is received, or a key is pressed.
}
uses
  serial, crt;

VAR
  serialhandle : LongInt;
  ComPortName  : String;
  s,tmpstr,txt : String;
  ComOut,ComIn : String;
  ComPortNr    : integer;
  writecount   : integer;
  status       : LongInt;
  BitsPerSec   : LongInt;
  ByteSize     : Integer;
  Parity       : TParityType; { TParityType = (NoneParity, OddParity, EvenParity); }
  StopBits     : Integer;
  Flags        : TSerialFlags; { TSerialFlags = set of (RtsCtsFlowControl); }
  ErrorCode    : Integer;

BEGIN
  ComPortNr:= 1;
  tmpstr:= '';
  txt:= '';

  writeln('Parameters ', ParamCount);
  if (ParamCount>0) then
  begin
    tmpstr:= ParamStr(1);
    val(tmpstr, ComPortNr, ErrorCode);

    if (ParamCount>1) then
    begin
      txt:= ParamStr(2);
      {val(tmpstr,ComPortNr,ErrorCode);}
    end;
  end;

  str(ComPortNr,tmpstr);
 
  ComPortName:= 'COM'+tmpstr+':';
  writeln('Using '+ComPortname);

  serialhandle := SerOpen(ComPortName);
  Flags:= [ ]; // None
  SerSetParams(serialhandle,9600,8,NoneParity,1,Flags); 
  
  s:=txt; // use the input text
  writeln('OUT '+s);
  s:=s+#13+#10; { CR + LF }
  writecount:= length(s);

  status:= SerWrite(serialhandle, s[1], writecount );

  // The next line is for debugging only!
  writeln('status: ', status, '   writecount: ', writecount);

  if status > 0 then
  begin
    writeln('Waiting for answer');
    { wait for an answer }
    s:='';
    ComIn:='';
    while (Length(Comin)<10) and (status>=0) and not keypressed do begin

      status:= SerRead(serialhandle, s[1], 10);
      if (s[1]=#13) then status:=-1; { CR => end serial read }

      if (status>0) then ComIn:=ComIn+s[1];
      if (status>0) then begin
        writeln(status,' ',length(ComIn),' ASCII ',ord(s[1]),' INP ',ComIn);
      end;
    end;
  end
  else
    writeln('ERROR - Unable to send.');

  SerSync(serialhandle); { flush out any remaining before closure }

  SerFlushOutput(serialhandle); { discard any remaining output }

  SerClose(serialhandle);
END.

When timeout starts

Q: I'm trying to figure out when the timeout timer in SerRead/SerReadTimeout starts.

A (by FPC developer Christo Crause): FPC uses the OS provided functionality to interact with the serial port. On Windows the timeout seems to start when the read request is made - Link. On POSIX (at least Linux) it depends on the specific set of flags specified , scroll down to the discussion on canonical/noncanonical mode for the details - Link.