Recent

Author Topic: LazSerial detecting the connection has dropped  (Read 3821 times)

KarenT

  • Full Member
  • ***
  • Posts: 120
LazSerial detecting the connection has dropped
« on: September 16, 2018, 03:42:02 pm »
OK, so I am over my little fit of pique now that 1.8.2. has solved the issue. :) Ubuntu 16.04, Acer i5 Laptop,  Lazarus 1.8.2.

Using LazSerial with an Arduino Mega, I monitor a constant stream of data over Serial and it works well. But, when left running overnight, I often come in of a morning and it has disconnected for some reason. This happens about three times a week.

Is there some way I can monitor the serial stream and if it stops for say, two minutes, then restore the connection, or at least try?

I tried using "OnStatus" code below and manually reboot the Arduino to make it drop the line, it never gets to the Restart line.

Code: Pascal  [Select][+][-]
  1.   if not(ser1.Active) then
  2.   begin
  3.     RestartSerial;  <<-- never gets to go to this procedure
  4.   end;
  5.  

How can I monitor that line status and try to reconnect when it goes down? Code snippets anywhere?

Thanks.

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: LazSerial detecting the connection has dropped
« Reply #1 on: September 16, 2018, 05:28:16 pm »
I don't know much about the serial code for the AD but I do know some things about
micro controllers.

 I would put in a timer to measure the elapse time the readings come in on the average
and then jump to a safety zone if far exceeds this time well after you have taken some readings.

 From what you are saying I maybe that you are using a call to capture a complete line and then
the device connected maybe failing had way through that so not to send a complete line...

 Try to use a character read and build your own lines, that way you can better debug what is
happening in your code..
The only true wisdom is knowing you know nothing

KarenT

  • Full Member
  • ***
  • Posts: 120
Re: LazSerial detecting the connection has dropped
« Reply #2 on: September 16, 2018, 09:44:56 pm »
Try to use a character read and build your own lines, that way you can better debug what is
happening in your code..

Thanks, but...

I am already reading byte-by-byte. It's just that the entire connection must be dropping out. I have just had the Arduino running for three days now using Arduino local serial terminal and not my program. The IDE terminal has not lost a beat. So, I guess it is something Lazarus is doing that causes the serial to drop. Maybe Lazarus/FPC is doing some sort of house-cleaning and momentarily drops the serial  line.

When I come in of a morning and it has stopped, I just have to re-open the port and it is off again so it is not like it has locked-up or crashed. I think I will have to move to a different programming system, per previous rant :) as Lazarus is not handling long term running at all well. I thought it might have been brief power outages but it all connects up fine if I pull the plug or reboot the Arduino.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: LazSerial detecting the connection has dropped
« Reply #3 on: September 17, 2018, 06:14:50 am »
hello,
Is there some way I can monitor the serial stream and if it stops for say, two minutes, then restore the connection, or at least try?
you can use a timer to monitor  your serial line. Something like that :
1 - Put a Timer component on your Form
2 - Set the interval property to two minutes and set the enabled property to False
3 - Define a boolean global variable :
Code: Pascal  [Select][+][-]
  1. var SerialDataAlive: Boolean;
4 - Initialize the variable to False  in the onCreate event of your form
5 - Put this code in the received event of your serial port :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.SerialRxData(Sender: TObject);
  2. var Str : string;
  3. begin
  4.   if Not SerialDataAlive then Timer2.enabled := true;
  5.   SerialDataAlive := True;
6 - Put something like that on the OnTimer event of your timer :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.Timer2Timer(Sender: TObject);
  2. begin
  3.   If SerialDataAlive then
  4.     begin
  5.       SerialDataAlive := False;
  6.     end
  7.   else
  8.     begin
  9.      Memo.Append(DateTimeToStr(Now) + ' : No Data since a while');
  10.      // Action to DO (ex : stop the timer,  read status of serial port, reconnect serial,
  11.      // restart communication etc ...
  12.     end;
  13. end;  

Can you show us your code to see if all is OK ? 
Have you  this kind of code in your Onstatus event :
Code: Pascal  [Select][+][-]
  1. procedure TFMain.SerialStatus(Sender: TObject; Reason: THookSerialReason;
  2.   const Value: string);
  3. begin
  4.   case Reason of
  5.     HR_SerialClose : StatusBar1.SimpleText := 'Port ' + Value + ' closed';
  6.     HR_Connect :   StatusBar1.SimpleText := 'Port ' + Value + ' connected';
  7. //    HR_CanRead :   StatusBar1.SimpleText := 'CanRead : ' + Value ;
  8. //    HR_CanWrite :  StatusBar1.SimpleText := 'CanWrite : ' + Value ;
  9. //    HR_ReadCount : StatusBar1.SimpleText := 'ReadCount : ' + Value ;
  10. //    HR_WriteCount : StatusBar1.SimpleText := 'WriteCount : ' + Value ;
  11.     HR_Wait :  StatusBar1.SimpleText := 'Wait : ' + Value ;
  12.  
  13.   end ;
  14.  
  15. end;

Friendly, J.P
« Last Edit: September 17, 2018, 06:44:32 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: LazSerial detecting the connection has dropped
« Reply #4 on: September 18, 2018, 06:03:01 pm »
I think I will have to move to a different programming system, per previous rant :) as Lazarus is not handling long term running at all well.
It's always easier to blame the tool then the person behind the keyboard. I have both serial and tcp/ip synapse connections running 24/7 in heavy industry for years. Both Windows and Linux. No leaks and no communication stops. Do you really expect that some library will automatically reconnect in case of problems? What should be the default behavior in such a case? That is all for a developer to think about. If you don't handle such stuff, there is no magic that will do it for you, whatever tool you choose. Btw, do you use threads in your application?

There is also a possibility that problem is on Arduino side. From power problems you defend with brown out detection - you set it with AVR fuses. From freezing problems you defend with a watchdog in your Arduino software. Any Arduino application aiming to run 24/7 should think about both. Next time you face the same problem, do not plug off or reset Arduino. Instead unplug your PC serial cable with Lazarus application and connect something else to check if Arduino it self still responds to communication. If it doesn't, then it is probably one of two things I mentioned.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018