Thursday, April 15, 2010

jamod: Some FAQ's

1) How do I debug when using jamod?

The debug outputs of the library can be activated by setting the boolean system property net.wimpi.modbus.debug at startup time of the JVM as true.

This will allow you to see the actually exchanged Modbus messages encoded as hex.

Standard JVM's have a D option for this purpose:
-D=
                  set a system property
e.g.:
Fangorn:~/development/java/jamod wimpi$ java -Dnet.wimpi.modbus.debug=true \
  -cp build/classes net.wimpi.modbus.cmd.DITest localhost:5555 0 4 3
Connected to localhost/127.0.0.1:5555
Request: 00 00 00 00 00 06 00 02 00 00 00 04 
Response: 00 00 00 00 00 04 00 02 01 50 
Digital Inputs Status=00001010 
Response: 00 01 00 00 00 04 00 02 01 50 
Digital Inputs Status=00001010 
Response: 00 02 00 00 00 04 00 02 01 50 
Digital Inputs Status=00001010   

If you get something like a class not found exception for net.wimpi.modbus.debug, then you did not specify the parameter to set a system property correctly.

If you are using an IDE, please refer to the corresponding manual, that usually should provide the ways and means to pass parameters or system properties for run configurations.

Once you see the messages, you will usually be able deduce what is going wrong with the communication with your specific device and your specific implementation (also see FAQ 4 below please).

2) Why don't you use <insert your favorite logging tool>?

jamod was written with the intention to be as portable as possible, including to devices with extremely constrained resources (e.g. SNAP, Tini). In these devices, some forms of logging may not make as much sense as on your fully equipped desktop machines.

Given the fact that commonly you just need to "debug" while establishing and adjusting the communication, but very few times afterwards, we decided that the best choice was to solve it in the current form and let the community leverage the open source to derive the versions to their liking (which actually has happened).

3) Why can't I build/use this in <insert your favorite IDE>?

You sure can. Your best bet is to use either ant or maven integration, both of which are provided by all mainstream IDE's. If there is no such support, I would suggest to obtain either ant or maven (depending on what you want to build; maven build does not support any resource constrained devices yet) to achieve local builds.

To use the build artifact (i.e. the jamod library packed into a JAR), you will need to place it in the classpath of your own project, respectively establish the dependency (in maven or your IDE).

4) Why does jamod not work with <insert your device>?

The most common answer for this question is that:
a) you have not read your devices manual
b) you are not addressing correctly

It may sound harsh, but please, try the following first:
a) Debugging (see FAQ 1 above): checking the message flow will usually help you to figure out what is going wrong.
b) Double check your reference: there may be some offset that you didn't consider. Remember, Modbus addressing starts at 0; unlike other tools, jamod does not introduce any artificial offset (i.e. will pass directly into the message what you specify).


Hope this helps.

Thursday, April 08, 2010

How to send mail with Quercus using Commons email

I have been running Quercus in Tomcat 6 to provide PHP, which is an easy way to quick-and-dirty-scripting in a Java Container, with a language many people can handle.

Most of the time things work just as expected. However, there is one thing that can cause a mayor headache: sending eMail through an external SMTP server, more if SSL is required (like in the case of using Google Apps offers as mail service).

After looking at some options, I finally decided to take a simple route:
1) drop commons-email-1.2.jar into the shared library directory (obtained from here)
2) use the Java Interface offered by Quercus

While the whole thing can be packaged into a module for sure, the solution is so simple (albeit not portable to standard PHP or executable on the CLI) that I didn't bother.
Add the following script snippet to your (Quercus powered) PHP file:
[...]
$mailserver=....
#Configure variables here
[...]
import org.apache.commons.mail.SimpleEmail;

$m = new SimpleEmail();

#Uncomment if you need debugging
#$m->setDebug(true);

$m->setHostName($mailserver);
$m->setSmtpPort($mailport);

#Uncomment if you need SSL
#$m->setSSL(true);

#Authentication
$m->setAuthentication($mailuser, $mailpassword);

$m->addTo($mail_to, $mail_to_fn);
$m->setFrom($mail_from);
$m->setSubject($mail_subject);
$m->setMsg($mail_body);
$m->send();

Configure the variables, and you are done. Note that commons-email also supports attachments and HTML mail (see the User Guide), so this solution should be pretty easy to get up and running, but also flexible enough to handle your mailing problems at hand.

The beauty of this approach is that you can get JavaMail debug messages in your tomcat log that help you to fix your configuration.

Enjoy.

Tuesday, March 09, 2010

Custom Quartz Composer Patches: How to include a shared library

I have been struggling a little bit to make my "DataMatrixDecoder" patch load libdmtx as a shared library.

Actually it is pretty simple:
instead of the @executable_path, the install name needs to contain @loader_path.

Here some tools that can help you to make things work and check that things worked out the intended way (credits go to Mac OS X Hacking Tools):
1) install_name_tool to change recorded install names of a mach-o-binary.

2) lipo: to check if the universal binary magic worked out e.g.:
orion:Release wimpi$ lipo -detailed_info QuartzDM.plugin/Contents/MacOS/QuartzDM 
Fat header in: QuartzDM.plugin/Contents/MacOS/QuartzDM
fat_magic 0xcafebabe
nfat_arch 3
architecture x86_64
    cputype CPU_TYPE_X86_64
    cpusubtype CPU_SUBTYPE_X86_64_ALL
    offset 4096
    size 18872
    align 2^12 (4096)
architecture i386
    cputype CPU_TYPE_I386
    cpusubtype CPU_SUBTYPE_I386_ALL
    offset 24576
    size 21460
    align 2^12 (4096)
architecture ppc7400
    cputype CPU_TYPE_POWERPC
    cpusubtype CPU_SUBTYPE_POWERPC_7400
    offset 49152
    size 21956
    align 2^12 (4096)
3) otool to check and display info about the mach-o-binary like it's dependencies. e.g.
orion:Release wimpi$ otool -L QuartzDM.plugin/Contents/MacOS/QuartzDM 
QuartzDM.plugin/Contents/MacOS/QuartzDM:
 /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 15.0.0)
 /System/Library/Frameworks/Quartz.framework/Versions/A/Quartz (compatibility version 1.0.0, current version 1.0.0)
 @loader_path/libdmtx.dylib (compatibility version 1.0.0, current version 1.0.0)
 /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 123.0.0)
 /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 227.0.0)
 /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 550.0.0)
 /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 38.0.0)
 /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 751.0.0)