Saturday, February 18, 2012

Playing With Helper Apps

Part 1: The Command Line Parameters.

For one of our Real Studio application, we choosed to create a helper app to handle serial communications and data parsing as a background process. In order to setup the Serial and the IPC path needed by the helper app, we decided that we will use command line parameters.

I wrote a few helper applications in the past, but I never have to use command line parameters as the IPC path they'll used was always known before compiling and that the setup parameters were send to the helper by using IPC. As I wasn't used to deal with command line parameters, I created a simple test app exposing the content of the Args() array in the ConsoleApplication.Run event and allowing me to play with it in a Terminal window.

The code is as following:

Function Run(args() as String) As Integer
  Print "--"
  Print "ArgsTester was launched with the following arguments:"
  Print " "
  // Print the content of Args()
  For i As Integer = 0 to Args.Ubound
    Print " Args( " + Format( i, "#00" ) + " ) = """ + args( i ) + """"
  Next
  Print " "
  Print "We're done."
  Return 0
End Function

That's it. I called the application 'ArgsTester' and build it.

After a few tests I noticed that, accordingly to Real Studio's documentation, the first element of the array always contains the path to the application. But, and that's not documented, the last element is always empty. There is a bug report about that: Feedback #15573. Feel free to add yourself. Also, it doesn't seem to  happen on Windows.

Now, the helper app needs some data from the main app in order to initialize itself:
  • A serial port and the settings for it.
  • A communication channel to talk with main app
  • The kind of protocol it should expect from the serial device
For the serial port setup, it'll use the '-s' ( or '--serial' ) followed by a comma separated value that will hold the serial parameter in this particular order:

SerialPortSystemIndex,Baud,BitNumber,StopBit,Parity[,CTS][,DTR][,XON]

The communication channel will be provided by an IPCSocket, so the options tag will be '-i' ( or '--ipc' ) and the value will be a Unix-style path to be used with the IPCSocket.Path property. The protocol will be set with the option tag '-p' ( or '--protocol' ) followed by the name of the protocol.

Let's try this with ArgsTester with the following command line:

argstester -s 0,6,3,0,0 -c /var/tmp/ipc.test.channel -p myProtocol

and see the result:


Pretty easy to handle from here, isn't it?

One last thing... In order to get rid of the last empty element on Mac OS X you can just do:

  #If TargetMacOS Then
    Call args.Pop
  #endif


In the ConsoleApplication.Run event before parsing the arguments.

To be continued...

No comments:

Post a Comment