Lazarus
Free Pascal => Beginners => Topic started by: jcaser1948 on August 27, 2020, 01:30:25 pm
-
The programm given in
https://wiki.lazarus.freepascal.org/Hardware_Access#Serial_port_names_on_Windows
Example"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
-
The programm given in
https://wiki.lazarus.freepascal.org/Hardware_Access#Serial_port_names_on_Windows
Example"FPC built in Serial unit" compile but crashes stating External SIGSEV
in pos
:'(
if (s[1]=#13)then status:=-1; { CR => end serial read }
You're not checking whether s actually contains anything, and s[1] doesn't exist.
I did the last maintenance on FPC's serial unit, but didn't write that test program. The real problem is that there's no check of the result of the SerOpen() call.
Is the code you've quoted cut-and-pasted from that article? If so please emphasise that fact when you do something like that, otherwise emphasise what changes you've made. Oh, and please try to sort out your usage of the [ code ] tag :-)
MarkMLl
-
Thanks for your prompt answer
Yes I did cut and paste the example .Could be possible to put in the Example"FPC built in Serial unit " an example that works?What should I modify to get that example working?
-
Fix the example, then update it. It's a wiki page :-)
I use that unit heavily and there's a couple of minor mods I'd like to make to it, but I use neither Windows nor Solaris these days so am not in a position to test it as thoroughly as I did originally- which I guess is one of my problems.
Now looking at your problem, first find the serial.pp unit in the FPC sources, you should find that the interface part is implementation-agnostic. Look at the definition of the SerOpen() function, and modify the example to do something sensible when it fails.
MarkMLl
-
Found the source code in C:\lazarus\fpc\3.2.0\source\packages\rtl-extra\src\win