Wednesday, March 10, 2010

rxtx and blocking

Writing this down so that others (and me too when I’ve forgotten about it again) could hopefully save an hour or two when trying to figure out a problem they are having with serial port communication using the rxtx libraries. Some hits on this on Google but not many(any?) with the complete description and solution of the problem.

The problem

If you ever tried using the serial port libraries for java, rxtx.org, you might have encountered a … different way of (sort of) handling blocking I/O. It can sort of be non-blocking, returning –1 on reads if nothing is available.

The rxtx way

But surely the rxtx library has thought of this and there is some way of setting this up or use it so it works for the simple blocking read case? Of course. This is how it is supposed to work: You have two parameters to play with: timeout and threshold. According to the source code setting the timeout to 0 (none) and threshold to 1 (requiring at least 1 byte before returning) should give us normal, by InputStream defined, blocking reads.

Still doesn’t work!

The problem is that even when setting it up like this there is a bug in the current stable release (2.1.7r2). The threshold parameter is always set to 0!From the source code:

/* TESTING ttyset.c_cc[ VMIN ] = threshold; */
ttyset.c_cc[ VMIN ] = 0;

The confusing part is that this was also the case in 2004 and reported on the mailing list and fixed, but it was either not really fixed or has come back again (a regression). There is actually a new bug report that for some reason I couldn’t find at first. I eventually found it going throw the pre-release package source code and found an otherwise not published change log (the web page doesn’t show change logs after the last stable version, its available in CVS though).

Solution

  1. It is fixed on HEAD, so you can use the latest pre-release version (2.2-series) or compile it from CVS.
  2. Make an ugly workaround along the lines of:

int read(InputStream in) throws IOException {

    int b;
    while ((b=in.read()) == -1) {
        try { Thread.sleep(10); } catch (InterruptedException e) { }

    }

    return b;

}

Then you do: read(in) instead of in.read().

And/or do a readFully version for byte arrays, read(byte[]) or similar:

byte[] data = new byte[length];
int read = 0;
do {
    int n = in.read(data, read, length-read);
    read += n;
} while (read < length);

Thursday, July 02, 2009

Estimation and deliberate discovery

Excellent blog post (as usual) from Dan North. Release level estimation this time. Well worth a read. The perils of estimation -Dan North

Sunday, February 08, 2009

Sync files over the network/internet using P2P

Today I was reminded how efficient, simple and non-intrusive it is, I haven’t thought about it much at all since I started using it. It just works, the signature of a nice piece of software. For Windows and Mac Live Sync is just that. P2P file sync for your personal needs. If you have more than one computer, and ever have the need to just simply have access to the same files or simple programs, I highly recommend that you try it out if you haven’t already.

Currently these are my private sync shares (yes you can share with others too, with different levels of access):

  • Tools –All computers, including work computers, participate in synchronizing this share. Programs like: process explorer, cpu-z, notepad2, winmerge, putty etc
  • Documents –My personal computers share this.
  • Backgrounds –Background pictures I like and encounter. If I find a new one somewhere I just add it to this folder and it is automagically sync:ed to all others. On some computers they are used as screen savers as well (windows built-in photo album screen saver). Computers with Windows 7 switch between these backgrounds automatically.
  • Transfer –To be able to simply transfer files between computers. Just drop files in this local folder and they appear on the other computers. Regardless of firewalls etc.
  • Favorites – Bookmarks for your selection of browser. Just share the folder where your browser keeps your bookmarks. Then without any hassle or logins somewhere you always have all your favorites/bookmarks right where you want them, in your browser as normal, on all your computers.

Does anyone have any other neat ideas of what you can share like this? Music I don’t really need to share or even download as Spotify has taken care of all that in a great way, and it is legal too.

Monday, December 22, 2008

Lägenhet till salu

(Swedish only, putting up my apartment for sale)

Så var det dags att sälja lägenheten, en fin 2:a i Mölndal/Göteborg på 65kvm, låg avgift, 100mbit/s bredband. Känner du någon som är intresserad, tveka inte! Visningar i januari.

Länk till mer om lägenheten

Update: Sold!