Thursday, September 29, 2016

Use Prologix USB-GPIB with Python (or any other language)

Prologix This USB to GPIB adapter by Prologix is cheap and pretty neat. It uses an FTDI chip to interface with USB, so the device shows up as a COM port when connected to a computer. The FTDI driver is available on Windows, Linux and Mac OS, so most users can use these devices without worrying about finding drivers.

The programming is relatively easy, as long as you can communicate through COM port. Since it is a virtual COM port over USB, the Baud rate can be set very high. The command strings are sent through the virtual COM port, buffered, then sent over GPIB. Therefore, a command string has been written to the COM port does not mean it has reached the slave on the bus. This asynchronous nature can cause some unexpected behavior. For example, if we send *IDN? to an equipment on the bus, Pyserial write function returns as soon as this string is in the buffer of COM port. If we start to read from the bus right away, the equipment does not understand why it is set to TALK. To avoid this problem, a short delay should be inserted between writing to the equipment and reading from it.

Some queries may take time to complete because the slave needs to collect data samples, process the data, ... Consequently, the query may timeout and return empty or partial information. Increasing timeout may help the situation but with side effects. This is because there are two timeouts - GPIB timeout and COM (serial) port timeout. In addition, longer timeout will delay many normal operations. This problem can be easily solved by increase the delay mentioned above without touching timeouts. The GPIB slave cannot send the response until it is set to TALK, therefore no information will be lost if we do not start the read. In the sample code linked below, the delay can be simply controlled for each query, so it is trivial to adapt to different use cases.

One feature of the adapter often causes problems is read-after-write. This is set by writing "++auto 1" to COM port. Although it is convenient in theory, some instruments report error when being set to TALK by some commands that do not require an answer. Therefore, it is recommended to disable this feature and read explicitly by "++read eoi" command.

A simple class written in Python can be found at 
https://bitbucket.org/qiang_self/prol_gpib/src The code has been tested on Windows 7 with Python 2.7.11 and 3.5.2.

Thursday, February 11, 2016

Easy way to run python script in Conda activated environments in Windows

Conda is a great tool to setup python and various packages/languages. It provides easy method to switch between environments, e.g., one can easily switch between python 2.7 and python 3.5 without resorting tedious hacks and tricks. However, when using it in Microsoft Windows, it is not as convenient as one wishes for.

In Windows, there are multiple ways to run a python script.
  1. You can set the path to python.exe in your PATH variable, then type "python path_to_your_script". Although you don't have to type the path to your python installation, you have to type the path to your script every time. You can use the script in the same directory as your command line environment, but this is an inconvenient restriction.
  2. In addition to setting the path to python.exe in PATH variable, one can also set the path to your scripts in PATH variable. Some distributions of python works well and you simply need to type "your_script_name" and Windows will call python.exe to run it. This did not work for me at least on Anaconda2-2.5.0-Windows-x86_64. I had to manually associate files with .py extension with python.exe installed by Anaconda. While this did work, there is a new problem - it always uses the root environment. In my case, the root environment is python 2.7. I installed python 3.5 through Conda and named it py35. After "activate py35", and run python.exe --version, it shows python 3.5. However, when I run my script, it still shows python 2.7. This is because when I type the name of the script, Windows loads the python.exe file (version 2.7 in this case) that I associated earlier. 
In order to take advantage of Conda's capability of switching environment, I tried a a slightly different approach. I created a file called run_python.bat and saved it in the same directory as my python scripts. You can save it anywhere as you like, just make sure to add the path to it in your PATH variable. Inside this file, there is only one line - @python.exe  %* Then I associated .py extension with this new batch file. Note that I did not put the path to python.exe in the batch file. It is intentional. Without an absolute path specified, Windows searches for it. As Conda switches the environment, the python.exe found by Windows will be different, which serves my purpose perfectly. By default, when you associate an file extension with a program, there is an entry created in Windows registry. Run regedit.exe, search for an entry called "run_python.bat" after you have associated the .py extension with it. There should be "shell:open:command" for this entry. The default value for command is "path_to_your_directory\run_python.bat" "%1", change it to "path_to_your_directory\run_python.bat" "%1" %* so that the arguments can be passed to your python script properly.

That's it. A simple trick that worked for me.

Monday, July 1, 2013

Vacation in Yosemite, Sequoia and Kings Canyon

We went to Yosemite National Park, Sequoia and Kings Canyon National Parks in mid June 2013 and stayed at Tenaya Lodge and Wuksachi Lodge respectively. The climate was perfect for light hiking and water was abundant in water falls, streams, and lakes. Among all the vista spots we visited, Tenaya Lake along Tioga Pass was our favorite. Tioga Pass was a beautiful scenic drive definitely worth spending time on it.
Bridalveil Fall
Bridalveil Fall
Mirror Lake
Mirror Lake
Mirror Lake down stream
Mirror Lake down stream
More photos can be found on my website or on flickr.

Monday, October 17, 2011

Eee Pad Transformer TF101 Honeycomb 3.2.1 Battery Drain Solution

Asus Android Tablet - Eee Pad Transformer TF101 is one of the best tablets available in market as of Oct 2011. Not only because of its reasonable price, but also because of its IPS LCD panel, and its smart keyboard/dock design. However, one of its drawbacks is the relatively short battery life without the extra battery inside the dock.

What makes the situation worse is after the recent upgrade to Honeycomb 3.2.1, the tablet started to drain much more power than it used to do, and that is regardless the dock being connected or not. I tried suggestions found online using Automateit or Auto airplane mode, but the system was still using a lot of power. It seemed that the system never went to sleep.

While I was suffering with this bug, I came across a "solution" accidentally and would like to share it with all affected users. By inserting a microSD card into the slot on the tablet itself, this problem went away! This can be easily verified when a dock is connected - the light next to charging connector blinks even when the tablet is supposed to be in sleep. After the insertion, the light stops blinking.

I hope Asus can solve this problem in their firmware soon so users who do not know this trick will be saved. However, till Asus fix the problem, this trick can help us use the tablet as it is intended to be.

P.S. My tablet and dock are B60 version, so I cannot say whether it will work on your hardware if it is a different version.

Update Nov 9, 2011
With the latest firmware update (Android is still 3.2.1), this battery drain issue has been fixed. No need to try the trick here anymore.

If you would like to improve the battery life further when the tablet is in sleep, use Auto Airplane mode app to set the tablet into airplane mode when the screen is off. The reason this extends battery life is that the system fetches information over the internet from time to time while it is in sleep mode, which consumes extra power. However, if you are using VoIP software and would like to receive incoming calls, you cannot and should not use this app.

When the tablet is docked, it seems that disabling the touch pad improves battery life further in sleep mode.

Wednesday, April 27, 2011

Receive voicemail and fax through internet for free in the US

Although we are living in an age of internet and text messages, occasionally we do need to be communicated with voice messages or faxes. For many people like me that do not have a landline phone at home, this can be difficult. In other situations, e.g., for privacy reason, we do not want to release our home phone number, it will be very convenient if we can receive voicemails and faxes through internet. In addition, it is much easier to keep an electronic copy of the fax than keeping the paper copy, not to mention redistribution is effortless with the electronic copy.

To my surprise, I came across this free service that provides a phone number in the US for us to receive voicemails and faxes. It is a real phone number, not an extension number! k7.net forwards voicemails and faxes to any email address that you specify. If you have control over your email accounts, you can also set up an alias that forwards incoming emails to multiple actual email addresses. If you are running a small business, you can set up an alias called fax@yourdomain and forward emails to all of your users. Sign up is very simple.

There are 2 limitations with this free service - (1) it assigns you a Washington State phone number, (2) it expires if there is no activity in 30 days.

One issue that you need to watch out is the format of the fax is TIFF that supports non-square pixels. Most fax machines send out images using these non-square pixels which makes the images look odd (wide and short). Gmail users can view (not the preview image when you read the email) these files online without any distortion and the default image viewer coming with Microsoft Windows can view them. I believe IrfanView also supports this format. As far as I can tell, for Linux users Okular and Evince can also display them correctly. Other OSes or viewers may have trouble with these images. The trick I use to assure compatibility is to convert the TIFF file to PDF using ImageMagick. This tool is included in almost every Linux distribution and can be installed on other OSes as well.

Monday, January 10, 2011

San Jose Airport

Hall way
San Jose Airport (airport code SJC) is one of my favorite airports when I travel by air. It is clean and well decorated, with reasonable security crew that was more efficient and polite than some other bigger airports. A lot of passengers are on business trips, therefore you will not see many tired people with excessive luggage. The airport had going been through renovations for years, now it finally stops giving you the impression of walking through a construction zone. There is also free Wi-Fi access so you can stay online while waiting for the flight.

Another favorite of mine is John Wayne Airport in Orange County (airport code SNA). It is even smaller. The free Wi-Fi access is not as reliable as SJC, but you do not need to walk as long to get to the gates as with SJC.

Life is Good
Storm is coming

Wednesday, December 23, 2009

Good headphones to go with MP3 players and computers

I didn't spend money and time on headphones for me to listen to music on MP3 players and computers at the beginning since I thought the sound from computers were too lousy to justify expensive headphones. However, after listening to music many hours daily for a while, I got frustrated with poor sound quality my Sony ear buds offered and started to do some research online. I found this Koss Portapro had pretty good reputation and Amazon had it for about $34, so I purchased a pair to try. To my surprise, the music sounded much better than I expected suddenly when I switched to Portapro. It was then I got hooked. I started to do more research and started to build my own DAC and Headphone Amplifiers (I am an Electrical Engineer with extensive design experience in test instruments and communication equipments). Following the recommendations on GoodCans (I found the author shares a lot of tastes in sound with me) I upgraded to Grado SR60 (now discontinued and replaced by SR60i). SR60 costs about $70, which offers great performance that not many competitors can offer. Amazingly, these 2 models were designed many years ago, and they still have about the best performance/cost ratio. They are also easy for computers/MP3 players to drive without an extra amplifier. I also use Koss KSC75 when I am traveling. I purchased these under $5 from RadioShack on sale. The sound is almost identical to Portapro with slightly stronger treble and weaker bass (because it does not sit on the ears as tight as Portapro with pressure from the headband).

Lately I've been using JVC HA-RX700 at work, which provides some sound isolation so I do not hear too many phone conversations around me. These have deeper bass than Portapro and SR60. These can be found at $35 online. This model is more sensitive, i.e., outputs higher volume than Koss and Grado products.

For listening inside flights, I use JVC HA-FX66A or Sennheiser CX300. These do not provide super isolation as more expensive models do, but they are good trade-offs between sound/isolation and cost.

If anyone asks for recommendation, I will not hesitate recommending any of the 4 bigger models I mentioned here. However, they are not very pretty or fashionable, so it may not be something suitable if you care more about look than sound and music.

It is important to note that these headphones give you best sound when your music is near CD quality. If you listen to a lot of music ripped at low rate or with noticeable distortions, these headphones will show these flaws with no mercy. I do have a recommendation for these situations - Sennheiser HD-555. I found one under $70 after mail in rebate. HD-555 is reasonable balanced, i.e., flat response over frequency, but lacks details that is revealed by the other headphones I recommended above. This is the reason the music sounds smoother, but it is also the reason the it is not on my favorite list, because I hardly listen to any music ripped at low rates.