Migrating to stand-alone drivers this simple?

This evening I did a quick test to get an idea of what problems I would encounter when I would separate the drivers from the rest of the system (event handling and stuff).

So I created a new Delphi project of type Console Application, entered the code to create an instance of the PLCBUS (hey, I’m mentioned there :-)) driver class (line 15) and just watched what would happen. Of course I had to create a dirty-looking loop because all the important stuff happens in a thread inside the driver, but I was surprised about how quickly I could turn this driver into a stand-alone executable – in a matter of minutes!

program DrvPLCBUS;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Utils in 'CoreUtils.pas',
  PLCBUS in 'DriversPLCBUS.pas',
  PLC1141 in 'DriversPLC1141.pas';

var
  Driver: TPLC1141;

begin
  Driver:=TPLC1141.Create('PLCBUS', True, TPLC1141CommandHandler);
  Driver.Run;
  repeat
    delay(100);
  until false;
  Driver.Shutdown;
end.

Of course, this doesn’t mean this driver is already functional in the way it is now: it will connect to the PLCBUS hardware by opening a TCP/IP connecting, but that’s it.

That’s because I have no way of sending commands to this stand-alone driver, cause in the ’embedded’ way (i.e. running inside my monolithic Domotica system) it would receive messages from internal message handlers – in this state, the internal message handler has only got 1 listener, namely the driver itself and nothing else…

But that’s where MQTT will come to the rescue!

Another great thing is that I can extend the driver code for stand-alone operation and still use the same driver in the embedded way. This is possible because Delphi’s System unit provides an IsConsole variable with which you can decide what has to be done in both cases like this:

if IsConsole
then Writeln(Output, S)
else Logger.Add(S);

This way, in embedded mode everything will be logged to a file while the console version will write everything to standard output. So when I start the stand-alone driver I see this:

PLCBUS driver in console mode

This is great; just what I had in mind for a smooth transition: I can keep on extending my system as it is (build new versions, add new hardware drivers) and work on stand-alone mode for the drivers at the same time – no Big Bang!

Tagged . Bookmark the permalink.

3 Responses to Migrating to stand-alone drivers this simple?

  1. jwestra says:

    Sounds really good. Do you plan on making any of this Open Source?

  2. Yes, I have plans to do so, but not in a few days; there are some other projects that will go first. And I’ll have to check if I’m allowed to put everything in the open as well… so it may take some time. Is there something you’re particularly interested in?

  3. Pingback: Testing drivers as Windows services - Digits Domotica Blog

Leave a Reply

Your email address will not be published. Required fields are marked *