The programm given in
https://wiki.lazarus.freepascal.org/Hardware_Access#Serial_port_names_on_WindowsExample"FPC built in Serial unit" compile but crashes stating External SIGSEV
in pos
if (s[1]=#13)then status:=-1; { CR => end serial read }
I have nothing connected to COM1 yet
What is wrong with the example or what do I wrong?
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.
Thanks in advance