Recent

Author Topic: {$MODE Delphi} verses {$mode objfpc}{$H+}  (Read 419 times)

jcdammeyer

  • Jr. Member
  • **
  • Posts: 67
  • Embedded System Developer
    • Automation Artisans Inc.
{$MODE Delphi} verses {$mode objfpc}{$H+}
« on: August 06, 2020, 07:53:18 pm »

I have created an application that inside its' own create method just does this.
Code: Pascal  [Select][+][-]
  1.   // Create the interface form that handles all the physical I/O
  2.   CommConnection := TCommForm.FormCreate(
  3.            CANUSB_COM,                    // For now hard coded to USB CAN dongle.
  4.             'CANopen',                     // Form Caption
  5.             '\Software\AutomationArtisans\CANopenCom',   // Where to store config.
  6.             Sender);
  7.  
  8.   CommConnection.OnRxMsg := SerialRxData;
  9.  

After that the method
Code: Pascal  [Select][+][-]
  1.  SerialRxData(Sender : TObject; var s : string);

is called to parse the string sent by the CANUSB dongle.  When I switch over to an MCP2515 on a Pi or the internal CAN device on a Beagle I'll format the messages from those to look like what the CANUSB sends so the application won't know the difference.  Just depends on what is passed to the FormCreate method.

Well guess what? In Lazarus this line kept failing with a missing parameters message. 
Code: Pascal  [Select][+][-]
  1.   CommConnection.OnRxMsg := SerialRxData;


I even created a simple two form "hello world" program to bring it down to basics.  Still no luck.  Eventually I had to comment out:
// {$mode objfpc}{$H+}
Replacing it with:
{$MODE Delphi}

After that it worked.  And it still works on the Beagle running Linux.

Inside the main form I have:
Code: Pascal  [Select][+][-]
  1.     // Connection to the CAN hardware
  2.     CommConnection : TCommForm;
  3.     // Called by the DoRxMsg method in TCommForm.
  4.     procedure SerialRxData(Sender : TObject; var s : string);
  5.  

And the attached commform.pas shows the associated types and properties that make it all happen.

My question is:
If I went back to
{$mode objfpc}{$H+}

What needs to be done to be able to pass that callback function using the objfpc mode?  Or is that even possible in non-Delphi mode?


marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8727
  • FPC developer.
Re: {$MODE Delphi} verses {$mode objfpc}{$H+}
« Reply #2 on: August 07, 2020, 10:21:07 pm »
Yes, this is the main "feature" that sets objfpc apart from Delphi.

jcdammeyer

  • Jr. Member
  • **
  • Posts: 67
  • Embedded System Developer
    • Automation Artisans Inc.
Re: {$MODE Delphi} verses {$mode objfpc}{$H+}
« Reply #3 on: August 07, 2020, 10:27:59 pm »
I did try the @ in front of the function but for some reason it didn't appear to work.  I had tried a few other things at the same time so maybe I was distracted.  Thanks for the suggestions. 

And sorry for locking the thread.  It was totally unintentional.
John

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: {$MODE Delphi} verses {$mode objfpc}{$H+}
« Reply #4 on: August 07, 2020, 11:06:27 pm »
I did try the @ in front of the function but for some reason it didn't appear to work.  I had tried a few other things at the same time so maybe I was distracted.  Thanks for the suggestions. 
Must be indeed as we all seem to agree that was the issue  :)

Quote
And sorry for locking the thread.  It was totally unintentional.
Don't worry too much about it. It is/was simply strange as even if you made a mistake and know the answer it is common here to post your answer (as courtesy to other readers) or just write "never mind, solved it myself" (former is preferred).

Just let us know if you are still having issue(s) with that code (or other code for that matter).

jcdammeyer

  • Jr. Member
  • **
  • Posts: 67
  • Embedded System Developer
    • Automation Artisans Inc.
Re: {$MODE Delphi} verses {$mode objfpc}{$H+}
« Reply #5 on: August 07, 2020, 11:55:03 pm »
Just let us know if you are still having issue(s) with that code (or other code for that matter).

About to try that now.  Thanks.   I will post my results.

jcdammeyer

  • Jr. Member
  • **
  • Posts: 67
  • Embedded System Developer
    • Automation Artisans Inc.
Re: {$MODE Delphi} verses {$mode objfpc}{$H+}
« Reply #6 on: August 08, 2020, 12:20:01 am »
I changed two things.
First in all the files changed mode back.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. //{$MODE Delphi}
  3.  
 
Then only one single change and a compile and run.
Code: Pascal  [Select][+][-]
  1. CommConnection.OnRxMsg := @SerialRxData;
 

And the results are... drum roll... 

Success! 

And given that I was sure I'd tried that already I'm surprised.  But I must have been doing something else weird at the same time.

BTW. When this CANopen Lite monitor is done I will be releasing the source code that will run on PCs, Raspberries and Beagles.  It will use the USB port for the CANUSB dongle (or any micro that follows that protocol)  but also SocketCAN on the Beagle and Pi which will use either the Beagle internal CAN device or an SPI based MCP2515.

But of course the connection to socket CAN won't be C but FPC inside Lazarus.
https://www.beyondlogic.org/example-c-socketcan-code/



 

TinyPortal © 2005-2018