Recent

Author Topic: LazSerial error  (Read 6347 times)

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
LazSerial error
« on: January 30, 2017, 10:46:17 pm »
Just started getting this error when trying to Close the Serial Port.

Project SerialData raised exception class 'External: SIGSEGV'.
 In file 'gconv_close.c' at line 35


I thought I had fixed it by using "Ser1.Active:=False;" instead of "Ser1.Close;" but I am just getting the error less often, not fixed.

Anyone have thoughts on how to actually fix this?

Code: [Select]
procedure TfrmMain.btnCommOpenClick(Sender: TObject);
begin
  if Comm1.Active then Exit;
  try
    Comm1.Device:=edSerial.Text;
    Comm1.Open;
    CommStrRaw:=Comm1.ReadData;  // Clear anything in the Buffer
    CommStrRaw:='';
    memoSerial.Clear;
    if Comm1.Active then
    begin
      pnlSerial.Color:=clLime;
      memoSerial.Lines.Add('OPENED '+Comm1.Device);
    end;
  except
    ShowMessage('FAILED to OPEN '+Comm1.Device);
    memoSerial.Lines.Add('FAILED OPEN'+Comm1.Device);
  end;
end;

procedure TfrmMain.btnCommCloseClick(Sender: TObject);
begin
  if not(Comm1.Active) then Exit;
  Comm1.Active:=False;  // <--- This used to be Comm1.Close;
  pnlSerial.Color:=clRed;
  memoSerial.Lines.Add('CLOSED '+Comm1.Device);
end;
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: LazSerial error
« Reply #1 on: January 31, 2017, 06:44:45 am »
hello,
strange, because the close procedure of TLazserial is :
Code: Pascal  [Select][+][-]
  1. procedure TLazSerial.Close;
  2. begin
  3.   Active:=false;
  4. end;

i have no problem to open and close port with the sertest example  of TLazserial on Ubuntu 14.04 LTS 64 bits Lazarus 1.6 FPC 1.3.  Have you the problem with this example ?
What are you doing in your program ?
Have you tried the flush and purge methods before closing the port ? :
Quote
procedure Flush; virtual;

Waits until all data to is sent and buffers are emptied. Warning: On Windows systems is this method returns when all buffers are flushed to the serial port controller, before the last byte is sent!
===========================================================
Public    procedure Purge; virtual;

Unconditionally empty all buffers. It is good when you need to interrupt communication and for cleanups.

flush is used in the deviceclose  procedure (called when you put active to false ) :
Code: Pascal  [Select][+][-]
  1. procedure TLazSerial.DeviceClose;
  2. begin
  3.   // flush device
  4.   if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
  5.     FSynSer.Flush;
  6.     FSynSer.CloseSocket;
  7.  //   FSynSer.Purge;
  8.   end;
  9.  
  10.   // stop capture thread
  11.   if ReadThread<>nil then begin
  12.     ReadThread.FreeOnTerminate:=false;
  13.     ReadThread.MustDie:= true;
  14.     while not ReadThread.Terminated do begin
  15.       Application.ProcessMessages;
  16.     end;
  17.     ReadThread.Free;
  18.     ReadThread:=nil;
  19.   end;
  20.  
  21.   // close device
  22.   if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
  23.     FSynSer.Flush;
  24.     FSynSer.CloseSocket;
  25.   end;
  26. end;          

Friendly, J.P


Edit : TlazSerial is now in Github here with a 0.2 version :
Quote
V 0.2  01/2017 : BaudRates for UNIX fixed - synaser files units renamed (with laz prefix) to avoid conflict with the original synaser files.
                 scan port for linux improved
« Last Edit: January 31, 2017, 07:13:30 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: LazSerial error
« Reply #2 on: January 31, 2017, 08:53:23 am »
I have no problems either.

Can you adapt your exception handling a bit and report back which exception there actually is?
Code: Pascal  [Select][+][-]
  1. // NOT this
  2. ShowMessage('FAILED to OPEN '+Comm1.Device);
  3. //But THIS at a minimum:
  4. on e:Exception do  // needs sysutils, of course
  5.   ShowMessage(e.message,'|','FAILED to OPEN '+Comm1.Device);
  6.  
That will give you more meaningful exception information.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: LazSerial error
« Reply #3 on: January 31, 2017, 09:31:29 pm »
Thanks for the replies, bit nothing works. It bombs before it gets to handle the exception.

I have put Try/except in everything associated with the Comm1 and still it gives the " In file 'gconv_close.c' at line 35" error. Never triggering an exception.

I can't seem to trap it with stepping through either. It appears to be right after the Comm1.close; statement but before the very next line of code is processed. It never gets to change the Panel color or to the "except".

{edit} Just saw a "Run Error 216" flash by. What is that? {/edit}

Code: [Select]
    try
      Comm1.Close;
      pnlSerial.Color:=clRed;
      memoSerial.Lines.Add('CLOSED '+Comm1.Device);
    except
      on e:Exception do  // needs sysutils, of course
        ShowMessage(e.message+'|'+'FAILED to CLOSE '+Comm1.Device);
    end;
« Last Edit: January 31, 2017, 09:34:59 pm by HatForCat »
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: LazSerial error
« Reply #4 on: January 31, 2017, 10:07:20 pm »
It appears to be right after the Comm1.close; statement but before the very next line of code is processed.
Put up some showmessage between the lines, just as you did for the exception. It is annoying to click away those messageboxes but will tell you which line exactly bails out.

Quote
{edit} Just saw a "Run Error 216" flash by. What is that? {/edit}
Runtime errors:
Quote
216 General Protection fault
The application tried to access invalid memory space. This can be caused by several problems:
1 Dereferencing a nil pointer.
2 Trying to access memory which is out of bounds (for example, calling move with an invalid length).
Meaning you are accessing something that isn't there anymore (or never wasn't).
« Last Edit: January 31, 2017, 10:10:11 pm by molly »

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: LazSerial error
« Reply #5 on: January 31, 2017, 11:25:15 pm »
Nothing to do with LazSerial a such. It was Lazarus not handling an array being passed-by-reference.

It worked fine normally when data was streaming in and out. But it seems that **sometimes,** when the Serial was closed abruptly, the array was caught part way through an iteration of indexes. I put in a "delay(300);" after each time the array was being passed and solved the problem.

I changed the array to Global so it didn't need passing and it all went away. I did not want to leave the Delay-kludge in place. A bit of a weird Lazarus-bug I suspect, but have no inclination to delve into why/where/how. :)
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

Thaddy

  • Hero Member
  • *****
  • Posts: 14367
  • Sensorship about opinions does not belong here.
Re: LazSerial error
« Reply #6 on: February 01, 2017, 06:53:50 am »
What we would do is pass the array as const, not declare it global.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

HatForCat

  • Sr. Member
  • ****
  • Posts: 293
Re: LazSerial error
« Reply #7 on: February 01, 2017, 05:47:58 pm »
What we would do is pass the array as const, not declare it global.
OK, thanks, but Global was actually the way I had back in Delphi, I should have left it as was and not try to "fix" it with some streamlining when I moved to Ubuntu and Lazarus :)
Acer-i5, 2.6GHz, 6GB, 500GB-SSD, Mint-19.3, Cinnamon Desktop, Lazarus 2.0.6, SQLite3

 

TinyPortal © 2005-2018