Sunday, September 18, 2011

Reading and Writing to the parallel port on Windows 7 (and all post NT win OSs)

Recently one of my cousins from the electronics field wanted me to help him out on a problem; He had some electronic circuits which were controlled via a PC parallel port. These worked properly on Win98 machines but gave trouble on post NT win OSs (2000, XP, Windows 7 etc.). 

The problem was related to the difficultly in reading/writing to the parallel port in the newer OSs. In Win98 and older OSs, user programs could directly read/write to underlying hardware ports without restrictions. However the newer OSs had prevented this, and only kernel mode apps could write directly to the hardware ports.

The first option explored was to use UserPort, a kernel mode driver. The approach of UserPort is to manipulate the IOPM (I/O Permission Bitmap), so that user applications can be granted permission to directly write to the hardware. It uses a undocumented function (Ke386IoSetAccessProcess in NTOSKRNL.EXE) in order to achieve this.

However the UserPort driver didn't seem to behave correctly on windows 7. It would randomly freeze up the machine upon service start/stop operations. This could probably be a bug in the driver implementation, and not necessarily a problem of the approach, but the fact that a undocumented function has to be used, makes it that much unreliable.

The second option explored was to use inpout32.dll from here. This uses a more elegant approach where the user application calls the read/write of the dll, which in turn uses a embedded kernel driver (Hwinterface.sys) to do the actual hardware read/write (via DeviceIOControl API). The full details of its inner workings are explained here.

However my program in concern was written in java, and the inpout32.dll didn't have the necessary support to directly call it via JNI. One workaround would have been to write a bridge dll with JNI support, which in turn called inpout32.dll. 
The other option was to directly modify the inpout32.dll (since the source was available) to support JNI calls, but luckily someone had already done it here - as jnpout32.dll. This worked right out of the box, solving my problem at hand.



SugarSync - A powerful alternative to DropBox (cloud sync)

I'm currently trying out alternatives to DropBox, which has a (rather annoying) limitation of being able to sync only one folder. Putting everything I want to sync in a single folder doesn't suite my taste of folder organization. Also the free 2GB disk quota doesn't feel sufficient either (you could pay to get more though).

The alternative SugarSync seems to be more powerful with multiple folder sync and more control. Also it offers 5GB of free disk quota which is quite attractive. I have synced 4 devices so far, including my office laptop, home laptop, home desktop, and my Android mobile phone, and all seem to work very well.


SugarSync referral link  (to get me some additional disk quota ;)