Recent

Author Topic: Example"FPC built in Serial unit" in example programm crashes  (Read 1534 times)

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Example"FPC built in Serial unit" in example programm crashes
« 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?
Code: Pascal  [Select][+][-]
  1.  

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.
Code: Pascal  [Select][+][-]
  1.  

Thanks in advance



MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Example"FPC built in Serial unit" in example programm crashes
« Reply #1 on: August 27, 2020, 02:11:45 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 }

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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Example"FPC built in Serial unit" in example programm crashes
« Reply #2 on: August 27, 2020, 02:18:00 pm »
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?

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: Example"FPC built in Serial unit" in example programm crashes
« Reply #3 on: August 27, 2020, 02:29:15 pm »
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Example"FPC built in Serial unit" in example programm crashes
« Reply #4 on: August 27, 2020, 05:50:43 pm »
Found the source code in C:\lazarus\fpc\3.2.0\source\packages\rtl-extra\src\win

 

TinyPortal © 2005-2018