Thursday, October 27, 2011

Why you need to be careful when buying a USB serial adapter

I got a BAFO USB to RS232 Converter Adapter from DinoDirect. I thought I was getting a good deal. It was more expensive than the no-name adapters available elsewhere, but BAFO is an actual brand with support and drivers. I noticed that they were distributing a relatively recent Prolific PL-2303 drivers, which should mean they used PL-2303 chips which didn't trigger the code 10 error. What I got was a piece of junk:
  • Recent drivers fail to work with the adapter, giving a code 10 error. Even the most recent drivers downloaded from BAFO fail. (I wonder if it is really a BAFO product.)
  • The output is 0-5V. The adapter cannot output negative voltages like DinoDirect claims. (They write "The output voltage of our USB to RS232 converters (Pin 3: TX line) is about -9VDC".)
  • The plug is loose, and the USB connection is disrupted if the adapter is moved.
  • In Linux or in OS X with the open source drivers, the adapter starts endlessly repeating the last character that is received. (Prolific's drivers don't work in OS X either.)
  • The adapter cannot detect RS-232 break.
  • Timing may be inaccurate at higher baud rates
The adapter is usable for some applications in Windows with older drivers. However, watch out: some old drivers from Prolific cause bluescreens.

When I complained to DinoDirect, I was told I'd get a $5 giftcard with which I could "pay as cash" , but I was given a $5 off $20 coupon. When I tried to post a review listing the problems the review didn't get approved. A positive review for another item was approved immediately, and I got DinoPoints. When a second attempt to post the review didn't get approved either, I decided to write this blog post.

I should have done some research before dealing with DinoDirect. It seems like a lot of people dislike them. Some even feel that DinoDirect is a scam. I think it's pretty clear they're not a scam, but I can't trust the information on their website, or that they will respond to my problems in a satisfying way. (DealExtreme would probably give store credit for a product like this.) After browsing the DinoDirect web site more, I found a lot of questionable reviews and even questionable product information. For example, some macro diopters have information and reviews as if they're polarizers. Overall, many reviews seem questionable. I'm not saying that DinoDirect is posting fake reviews. Maybe some users post lots of reviews so they can get DinoPoints and free stuff. DealExtreme also offers points for reviews, but their reviews seem genuine. Go ahead and compare DealExtreme and DinoDirect reviews. Also, note that DealExtreme has a lot of activity in the per-product forums.

DinoDirect isn't all bad. They gave me a $1 gift card, I guess for creating my account. Then, I got 100 DinoPoints, corresponding to $1, for a short survey. When the flashlight I ordered wasn't in stock, live support they gave me a $3 gift card. Finally, I ordered the RICHONG RC-7001 LED Flashlight (1*AAA Battery) flashlight and received it. It's a decent little LED flashlight. It seems pretty rugged, and I like how it uses one AAA battery instead of several coin cells. Getting that for free partly makes up for the USB to serial junk I paid for.

Finally, if you really want to order from DinoDirect, two suggestions: Try to find some better information on the product elsewhere. Check for coupon codes, because DinoDirect is always running promotions. RetailMeNot has a good list.

Monday, October 24, 2011

The Unix serial port output buffer

Unix serial ports have associated input and output buffers in the kernel. When writing data to the port, if sufficient space is available in the output buffer, the write call returns after copying the data into the buffer. The hardware slowly outputs data from the buffer, freeing space for more data. A program can easily fill up the buffer, because the CPU works much faster than the serial port. When insufficient space is available in the buffer, a normal blocking write will wait. It will not simply wait for sufficient space; instead, it will wait for the buffer to empty to a specific low limit.

When simply sending large amounts of data, this behaviour is appropriate and efficient. Programs don't need to constantly wake to feed more data to the hardware. Instead, they can put a lot of data into the buffer and then sleep until the buffer gets to that low limit. However, for some applications the buffer is a problem.

Bytes that are already in the buffer form a delay between when a new byte is written and when it is actually output from the port. (All those bytes need to be output first.) As a result, serial responses and actions from serially controlled hardware may be delayed. If a program performs checks to see whether to continue sending data, that will also be delayed when a write call waits for the output buffer to empty.

In Linux the output buffer is one page (typically 4KB), and the process is woken when only 256 bytes (set via WAKEUP_CHARS) remain. That may seem like a small amount of memory, but at low baud rates, it corresponds to a long time. There is no standard way to change the buffer, and in Linux, it could only be changed by editing kernel source code and recompiling the kernel.

It is simplest to wait for data to be output after writes. This can be done by waiting for responses after writs or via tcdrain. (Other methods such as fsync and O_SYNC cannot be relied on for serial ports.) Unfortunately, this defeats the efficiency benefits of buffering.

When the issue is a need to stop output quickly, instead of a need for constant low latency, data in the buffer can be discarded via tcflush. However, this gets tricky. Since writes can wait for a long time, some form of asynchronous I/O is needed. Once the buffer is flushed, serial controlled hardware may be in an unknown state, which complicates things.

As an added benefit, tcflush and tcdrain also deal with the hardware buffer. In most built in serial ports, these buffers are tiny. However, USB to serial adapters can have large buffers.

Friday, October 14, 2011

In Linux, use the Realtek r8168 driver for RTL8168

I have a Gigabyte GA-P35-DS3R motherboard with a RTL8168 Ethernet chip.
When using the r8169 driver that's part of the Linux kernel, I often lose the connection after S3 suspend. Usually, taking the interface down and back up with ifconfig fixes it, but unloading and reloading the module is sometimes necessary. This problem has been around for a while, and it still exists in the current Linux Mint Debian Edition 3.0.0-1-amd64 kernel. Even adding r8168 to SUSPEND_MODULES doesn't help.

The best solution seems to be using the r8168 driver from Realtek instead. It solved the problems after suspend, and seems to have also sped up suspend a bit. It may have also fixed some hangs I used to get occasionally.

At first, version 8.025.00 refused to build because the src/Makefile didn't recognize that Linux 3 should be treated like Linux 2.6. I fixed this by changing the lines setting KEXT and KFLAG to:
KEXT := ko
KFLAG := 2x
After this, I had no further problems and autorun.sh correctly built and installed the driver. It renames r8169.ko to r8169.bak to deactivate that driver. After confirming that r8168 works properly, it's a good idea to blacklist r8169. It's also necessary to update the initial ramdisk, because r8169 may be loaded from there.


Friday, September 23, 2011

SkyDrive: 25 GB of free storage that's too annoying to actually use

For several years, I've known that I have a Windows Live SkyDrive account with 25 GB of free storage. However, I never actually tried to use it. Today I tried to use it and I see what's the catch: it's too annoying for storing large amounts of data.

I was hoping to upload all of my photos. Theoretically, it seemed like a good idea. The photo viewer is great, the per-file size limit is not an issue, and the overall size is more than other free services allow but only a fraction of the 25 GB limit.

The first problem is that the web uploader is quite limited. It can't upload folders, so I would have to manually create them and upload their contents. It doesn't even support drag and drop, except for in IE. Since uploading would be a lot of work, I rejected this option.

Then I learned that SkyDrive can be accessed via WebDAV and mounted as a drive in Windows. (It uses proprietary Passport authentication, so I'm not sure if any other clients can access it.) This was easy once I sorted out some firewall issues. Unfortunately, only document file types can be uploaded. Attempts to upload JPEG images and archives result in an error telling me that the file "is too large for the destination file system". It's not a file size issue because it happens with small files. I guess Microsoft only wants the WebDAV access to be used for Office documents.

Since PDFs are supported, I thought about using using them. JPEG images can be included in PDFs without re-encoding, and PDFs also support attachments for other file types. However, this is a kludge without any benefits besides WebDAV access, and so I decided to try ordinary multi-part archives instead.

Archives can be uploaded via the web interface, and files of 104,857,600 bytes (100 MB, 100 * 2^20) may be uploaded. Unfortunately, this is unreliable. The Silverlight uploader only seems to allow one 100 MB file to start uploading in a batch, and even that file seems to stop before the end. The upload seems to complete successfully, because the downloaded file is bit-identical to what I uploaded. The classic form based uploader also fails before the end, and I'm not sure if it's usable for large files.

Some per-service firewall rules don't work in Windows 7

The Windows 7 Firewall with Advanced Security allows you to specify a service in addition to a process. This is important because many services run under svchost.exe. Rules which simply reference svchost.exe may be too broad.

This works well with  the Windows Update and Windows Time* services. When outbound connections are disabled by default, rules can be created to permit those services. However, it doesn't always work.

In the past I spent a lot of time trying to figure out how to permit Windows Media Center schedule updates. I guess that part of the update is via the Background Intelligent Transfer Service (BITS). I said "I guess" because "netstat -b" sometimes showed them using the User Profile Service (ProfSvc), which doesn't make sense. Rules which permit outbound communication from BITS, ProfSvc, and even a large number of other services never accomplished anything. I finally gave up and created a rule permitting outbound communication on port 80 by svchost.exe (without specifying a service).

I was just trying to figure out how to allow the WebDAV client through the firewall. It obviously uses the WebClient service. In this case "netstat -b" also shows this. However rules permitting outbound communication by the WebClient service do nothing. I was forced to create a rule permitting outbound communication by svchost.exe on port 443 for SSL WebDAV access.

I'm not sure if these are bugs or intentional limitations. When creating a rule for svchost.exe, I am warned that "Windows services have been restricted with rules that allow expected behavior only. Rules that specify host processes, such as svchost.exe, might not work as expected because they can conflict with Windows service-hardening rules." Maybe Microsoft chose to not permit rules involving BITS and WebClient because they are general purpose communication services which could also be used for malicious purposes.  However, it doesn't make sense that a rule specifying svchost.exe and a particular port is allowed, but when the rule is further narrowed by specifying a service, it is ignored.

* If Windows Time Service is not allowed Internet access, the first "Update now" attempt will fail with a timeout. However subsequent attempts may appear to succeed, and the time when the clock was synchronized will be updated.

Thursday, September 22, 2011

Brown polarized sunglasses are great!

I used to wonder why someone would want brown or amber sunglasses. Why add a colour cast to everything? Now I know. The colour:
  • decreases sky brightness, equalizing the overall brightness of the scene
  • removes glare, while still making the scene appear bright in a pleasant way
  • accentuates green foliage and various other colours
  • makes hazy days seem sunnier
  • helps decrease glare when driving toward the sun
(Blue-grey sunglasses are the opposite. They dim things relative to the sky, they make foliage look worse, and they aren't very effective at decreasing glare. I don't want those ever again.)

Polarization is also an important enhancement. It:
  • decreases the intensity of annoying bright reflections from non-metallic surfaces, such as reflections from the paint of cars or bodies of water
  • increases colour saturation by removing reflections (Foliage reflects a lot of light, and looks much greener when that reflection is blocked. Water surfaces also reflect light, and when that reflection is blocked, the colours of the water itself stand out.)
  • makes the sky darker and bluer in certain directions, making clouds stand out and helping equalize the overall brightness of the scene
The only downside of polarization is that the intensity of the effect depends on the orientation of the glasses.  It is most optimal when the sun is highest in the sky. Near sunset, the sunglasses need to be turned sideways to create a strong effect. Sometimes, I thought about creating electronically controlled polarized sunglasses using camera filters and a microcontroller.

When buying polarized sunglasses, it's important to check whether the effect works well. Turn the glasses and block some reflections. Cheap glasses may not be able to block reflections very much, or the blocked reflections may show a strong purple colour cast.

Monday, August 29, 2011

Stores selling cheap stuff from China

There are many online stores which sell a wide variety of cheap stuff from China, usually with free shipping. The prices can be impressively low, but the quality is sometimes questionable.

DealExtreme seems to be the most popular one. So far, I've ordered several items from there, and overall I've had a good experience. I'm using the 10W 500-Lumen Multi-Color RGB LED Emitter Metal Plate (140 degree) in my RGB lamp, and I'm happy with it. Their 5mm white LEDs are also good, with a nice colour and good brightness. I also got a nice 55mm lens cap.

So far, I've only had one problem with DealExtreme, and that was resolved in a good way. I ordered what was at the time advertised as a Massa circular polarizer for my camera. The packaging and item I received made no mention of Massa, and the item looked different. When I contacted DealExtreme, I got some store credit. The polarizer does provide decent photos as long as I don't zoom much, and I really like some photos I took using it. I also see that DealExtreme updated the item description and photos, showing what I got. As a result of all of this, I'm satisfied.

My favourite thing about DealExtreme is the reviews and forum posts. They seem genuine, and they provide useful information about the products. Yes, DealExtreme does sell some bad products, but the reviews and other information make it possible to avoid most of these.

The only real complaint I have is that some simple actions (such as redeeming store credit or DX points) require customer service interactions. These things would be easier and faster if automated. However, customer service always responded promptly, so this is not a major issue.

The main thing that motivated me to write this post is my first purchase from DinoDirect and what I saw when I spent more time on that site. It's a big contrast; DealExtreme is much better. For example, check out what other people have to say about DinoDirect, and look at the quality of item reviews on DinoDirect.


Monday, August 15, 2011

I'm still using Adobe Reader

Despite the bloat and all the people who hate it, I'm still using Adobe Reader to view PDFs in Windows. Recently, it was slow on one huge PDF on my laptop, so I decided to try out some alternatives.

Sumatra PDF was pretty good and a bit faster, but zooming wasn't as user-friendly. I didn't like Foxit Reader's user interface. It seemed like an old badly designed Linux application. PDF-XChange Viewer was pretty good, but it also had an inferior zooming user interface. The biggest problem was that all three had slow search in large PDFs, without acceleration via saved indexes like Adobe's Fast Find.

It would have been nice to switch to something leaner, but it's not really a problem. I found that disabling 2D graphics acceleration in Adobe Reader resolves the performance issues on my laptop.