Recent

Author Topic: Starnge behaviour  (Read 4692 times)

BLL

  • Sr. Member
  • ****
  • Posts: 276
Starnge behaviour
« on: June 14, 2016, 11:36:10 am »
Hi

I hope I am posting this in the right place!

I produced a program on my RasPi, model 2 B, using fpc 3.1.1 and lazarus 1.5. It works fine.

I have now moved to a RasPi model 3 with fpc 3.0.0 and lazarus 1.7. It uses Jessie as opposed to Raspbian on the model 2.

fpc and lazarus were installed from the script at: http://cache.getlazarus.org/download/raspberry/setup.sh.

lazarus seems to work fine as I made a simple program and it worked as expected.

After that, I copied just the executable of my program to the model 3. It runs but there are 3 areas of problems:
1). The close button on the main form, when clicked, stops the program but does not close the window. Need to use kill to get rid of it.

2). I am not getting data from either of 2 USB ports, ttyUSB0 and 1, although data can be sent/received to the connected devices via putty.

3). I have a thread timer which is doing precisely nothing!

Next, I copied all the source code and built. It built fine, but on running it, it behaved as the executable from the model 2 had.

It therefore seems that the code being produced is not right for processor or os?

I notice that in most units on the model 2's lazarus, there is the line {$mode objfpc}{$H+}
whereas on the model 3's lazarus, it is {$mode delphi} . I think these statements tell the compiler the target? Why delphi?

The units for the serial port routines, sdposerial.pas, synafpc.pas, synaser.pas and synautil.pas don't have the {$mode objfpc}{$H+}, but instead start as below:

//old Delphi does not have MSWINDOWS define.
{$IFDEF WIN32}
  {$IFNDEF MSWINDOWS}
    {$DEFINE MSWINDOWS}
  {$ENDIF}
{$ENDIF}

//Kylix does not known UNIX define
{$IFDEF LINUX}
  {$IFNDEF UNIX}
    {$DEFINE UNIX}
  {$ENDIF}
{$ENDIF}

{$IFDEF FPC}
  {$MODE DELPHI}
  {$IFDEF MSWINDOWS}
    {$ASMMODE intel}
  {$ENDIF}
  {define working mode w/o LIBC for fpc}
  {$DEFINE NO_LIBC}
{$ENDIF}
{$Q-}
{$H+}
{$M+}

So, with fpc we get $mode delphi and $H+. I don't profess to understand what these terms do!

I changed the $mode statements in my source units to {$mode delphi}. Now when I compile, I get an error in Sdposerial.pas:
"Error variable identifier expected".

In the following procedure:

procedure TComPortReadThread.Execute;
begin
  try
    while not MustDie do begin
      if Owner.FSynSer.CanReadEx(100) then
        Synchronize(@CallEvent);
    end;
  finally
    Terminate;
  end;

end;

the line with Synchronize(@CallEvent) is highlighted.

I presume there needs to be changes to the directives in the serial units?

Can anyone suggest what the problem is and how to move forward please?

Thanks

Brian

rvk

  • Hero Member
  • *****
  • Posts: 4447
Re: Starnge behaviour
« Reply #1 on: June 14, 2016, 11:59:03 am »
Don't change the targets (i.e. mode Delphi) in the Synapse library files. The source code needs that target to compile correctly. The Delphi mode changes the compiler to emulate some of the behavior of Delphi 4 till Delphi 7.
See for the modes:
http://www.freepascal.org/docs-html/prog/progap4.html#progse62.html

If you can compile without error and you say your program acts exactly as the one compiled on model 2 then the compilation is correct but there are some things in your program that are not quite right.

Do you run your program as root or as user pi? Does the user pi have sufficient rights to read the ttyUSBx? (just a wild guess)

You say the timer doesn't work. Maybe you can strip down the program to a bare minimum to see if it still doesn't work. If it doesn't you could place the code here so we can investigate where is goes wrong.


Edit: Wat version of Synapse did you use?
« Last Edit: June 14, 2016, 12:01:40 pm by rvk »

wp

  • Hero Member
  • *****
  • Posts: 7933
Re: Starnge behaviour
« Reply #2 on: June 14, 2016, 12:07:40 pm »
See this for compiler modes: http://www.freepascal.org/docs-html/user/userse33.html

In essence, this is a way to allow various syntax definitions for source files. Delphi, for example, is more relaxed on usage of pointers, objfpc is more strict. If you say {$Mode} then this means that this file contains the Delphi syntax, and the same with {$Mode objfpc}. Therefore, do not change the compiler mode of an already existing file unless you know the differences.

If new files have mode Delphi now, but you had fpc before, and want this back permanently you should create a new project, open "Project" > "Project Options" > "Compiler options" > "Parsing", select "Object Pascal" in the combo "Syntax mode", make sure to have "Use ansistrings" checked. Finally check "Set compiler options as default" (at bottom left) and click "OK". Obviously getlazurus preferred the Delphi mode.
 if a file had been working before do not change the compiler mode
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starnge behaviour
« Reply #3 on: June 14, 2016, 01:22:42 pm »
Hi both for the replies. I will look at the links you have given.
What I don't understand is if the program worked fine on the model 2 and doesn't on the model 3 is that it's problems with my code. If that were the case, surely it shouldn't have worked on the model 2?

Since new projects seem to work, does it not suggest that something in the project setup needs changing, due to changes in o/s or lazarus?

Brian

rvk

  • Hero Member
  • *****
  • Posts: 4447
Re: Starnge behaviour
« Reply #4 on: June 14, 2016, 01:37:18 pm »
Hi both for the replies. I will look at the links you have given.
What I don't understand is if the program worked fine on the model 2 and doesn't on the model 3 is that it's problems with my code. If that were the case, surely it shouldn't have worked on the model 2?
It could be something simple as not being able to read from the ttyUSBx. And when that happens it could be that you didn't program any checks in so that certain things just don't work. So it could be just one very small thing that escalates in your entire program not working in multiple places.

This is a good opportunity to find out what exactly isn't working and building in safeguards against it (messages like "It doesn't work because ....."). After that you can fix the real problem and your program should work again.

Since we don't have a single piece of code we can't help you track down the actual problem. So my previous suggestion still stands. Try to create a minimal example (i.e. communication with the port or the working of the thread) and see if it also fails. If it fails we can help you further with that code.

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starnge behaviour
« Reply #5 on: June 14, 2016, 08:07:30 pm »
Hi, Progress at last. I hsve read up on the compiler modes and now follow that. I have put all my units back to {$mode objfpc}{$H+}. I found that the problem was with a USB plug making poor contact with its socket. The program is now mostly working except when I use the code:
RunCommand(progPath + 'te923con', [''], wxTemp); te923con, which produces on screen a string of weather values from a weather station needs root priveliges to run. From the commans line, I can use sudo ./te923con and if running my program directly gksudo in a script but how do you run a program from within lazarus with elevated priveliges so you can debug it?
Thanks to all those who have contributed and helped me out of my woes.

Brian

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starnge behaviour
« Reply #6 on: June 14, 2016, 08:17:06 pm »
Hi wp

I tried your steps for getting new projects to use fpc instead of delphi, but it hasn't worked!

It already had Object Pascal selected. Ansi Strings was ticked. The set compiler options as default was not checked, so I checked it and did OK.
Restarted lazarus and created a new project and unit1 still has {$MODE delphi} at the top!!

Brian

rvk

  • Hero Member
  • *****
  • Posts: 4447
Re: Starnge behaviour
« Reply #7 on: June 14, 2016, 08:36:25 pm »
I found that the problem was with a USB plug making poor contact with its socket.
Ok, you need to address that in your code. You need to catch faulty connections and end/free certain things so that your program doesn't crash or does not respond. (Good error-handling is also part of a good program)

Quote
RunCommand(progPath + 'te923con', [''], wxTemp); te923con, which produces on screen a string of weather values from a weather station needs root priveliges to run.
If at all possible I would find out a way to run te923con without root privileges. It's never a good idea to just make your program run as root just because one certain program needs it.

Quote
... but how do you run a program from within lazarus with elevated priveliges so you can debug it?
You could try running lazarus with gksudo. (But as you see the chain gets longer and longer what you are willing to run as root :))

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Starnge behaviour
« Reply #8 on: June 15, 2016, 11:14:18 am »
Thanks for the reply. I have now added error handling for the port connections. Strangely, whereas te923con needs sudo to work from the console, it works fine when called by the RunCommand!
Thanks for all the help.
Can close this now.

Brian

 

TinyPortal © 2005-2018