29 June 2006

RS-232 Sneaky Snooper

Have you ever had two devices that communicate through a serial connection (RS232, not USB!), and you want to know what they're saying to one another? For instance, suppose you've got some kind of peripheral that connects to a PC through a COM port, and there's some special user interface (UI) software that you run on the PC that knows how to talk to the device. But now, because you like hacking things, you want to write your own program to talk to the device. The only problem is, there's no manual that defines what the message structure is.

Usually you can work out the baud rate, parity, stop bits etc by connecting a good freeware terminal program like TeraTerm to the PC running the UI software, instead of the device, and then trying different settings of the terminal program until you get something that looks right. But more often than not, the UI will choke when it doesn't get the expected responses - and how could it, from a terminal program? Once you've got the right serial settings, you can turn the terminal program around and connect it to the device, and start guessing commands. Sometimes a capture of what the UI was sending will help here, but often you just won't be able to enter the right stuff fast enough.

This is where this project comes in. Wouldn't it be easier to see how the UI and the device talk, if you could just watch both sides? The following circuit will let a 3rd computer, running your terminal program, eavesdrop on the connection and capture the entire interchange to a log file that you can then pick apart at your leisure.

I know this works because I built and used this today, capturing a complex interchange between a GUI and a device that involved several message trades for each button clicked, over a 38400 N81 connection.



How It Works

Basically, the RS-232 transmitters idle at something like -9 to -12 Volts, called the 'mark' level . Whenever one of the devices transmits data, its Tx line will toggle between this mark level and the +9 to +12 volt 'space' level, before coming to rest back at the mark level. The trick is to get the Rx line of the eavesdrop computer to go to the space level when either (or both) of the other two do.

Here, the Tx line of the eavesdropping computer is used as a supply of negative voltage for what amounts to an old-fashioned resistor-diode logic 'OR' gate. When everything was idle, all my Tx lines were at about -11V. Now, assume that the first computer sends a byte. Every time its Tx line goes positive, the diode associated with that line becomes forward biased and conducts, with the current being limited to somethign near 2mA by the 47K resistor. The other side's diode will be reverse biased, preventing the signal from looping back to the sending device's Rx. The 2mA current across the resistor presents the eavesdrop computer's Rx line with nearly the full space level from the first computer; in my case, around +10.4V, well within spec for a 232 space. Similarly, if the second computer sends, the eavesdrop computer sees that data as well, with the role of the diodes reversed. One thing this circuit cannot do, however, is listen in if (or when!) both sides talk at the same time; the signals will mush together and the eavesdrop computer will decode the result as garbage. But as long as the first two take turns sending messages, everything works fine.

28 June 2006

Divisibility Testing for 7

Everyone learns how to test numbers for divisibility sometime around the 3rd or 4th grade, I think. Tests for 2 and 5 are trivially simple. Tests for 3 and 9 are nearly as simple, owing to some magic to do with 9 being one less than the base of the number system we use*.

Testing 4 works by virtue of the fact that only the last two digits matter. For example, does 86,753,096 divide by 4? Since 100 divides by 4, you can ignore 86,753,0__ and focus on the 96 part. Then the rule is, if the 10's digit is odd, the 1's digit has to be 2 or 6; if 10's are even, 1's have to be 0, 4, or 8.

Testing 6 comes down to (a) is it even? and (b) does 1/2 of it test for 3?

But it seems like most people are told there isn't an easy test for 7. But wait! If your number divides by 7, then you should be able to take 7 away from it any number of times, and the difference will also divide by 7. Taking a multiple of 7 away from the number under test preserves its modularity with respect to 7, which means that the remainder after actually doing the division will be the same. We can use this to shrink a large number down into a smaller number that is easier to test.

So, here's how to test for 7:

First, if your number starts with a 7, you can just drop that off immediately, which is equivalent to subtracting off 70, or 700, or even 70,000,000,000,000; you get the idea.

Next you can get rid of unpleasant digits like 8 or 9 by just subtracting 7 from them where they are. For example, 1891 ÷ 7 is the same as 1121 ÷ 7, because all you did was take away 700, and then 70.

Now consider the number 1001; it turns out that 1001 = 7 * 11 * 13. Any multiple of 1001 is therefore also a multiple of 7. This means you can take away the leading digit of your number if you also subtract it from the 4th digit in!


Here's an example: does 186,753,091 divide by 7?

First get rid of that leading '1' by subtracting off 100,100,000:
186,753,091 – 100,100,000 = 86,653,091 .. shorter by one digit.

Can we do it again? Well, looking at 86,653,091, maybe we could take out 80,080,000 but that leaves 5 – 8 at the fourth place. But remember, in mod 7 arithmetic '8' and '1' are the same thing (as are '9' and '2'). So the leading '8' can be turned into '1' by subtracting 70,000,000 which is yet another multiple of 7. So now the problem is:

16,653,091 – 10,010,000 = 6,643,091

Now we're going to add a multiple of 7 to get rid of the leading 6!

6,643,091 + 1,001,000 = 7,644,091 which becomes 644,091

Do the same thing again to get rid of the leading 6: add 100,100 to get 744,191 and drop the leading 7 and all that's left is 44,191

Now eliminate the leading 4 by removing 40,040 = 4 * 1001 * 10

44,191 – 40,040 = 4,151

Hmm.. 1 – 4 at the last digit? Nah. Go up! Add 3,003 = 3 * 1001

4,151 + 3,003 = 7,154 which becomes 154

And now you have a three digit number and 1001 is no more use. Sadly, the only way out is to just divide whats left by 7, but at least this is better than doing the whole nine digit number. With very little practice, you can look at a large number and almost just write down the three-digit result.



* What works for 9 in base 10 also works for 4 in base 5, 7 in base 8, even 15 in hexadecimal, as long as you add up the digits in the base you're testing in!

Eclipse

For those of you who don't know, 'Eclipse' is a programming tool (IDE) that makes managing larger Java projects a breeze. Even smaller Java projects benefit greatly from it, as the IDE is invaluable in keeping names consistent, especially when you start needing to use packages. Probably half of those who do know what Eclipse is would argue that NetBeans is better. While NetBeans may be better at laying out user interfaces, I prefer the snappy response of Eclipse when dealing with actual code.

Now, as good as Eclipse is once you've gotten under control, actually getting it under control can be a little daunting. There are some gotchas that the Eclipse development team seem to be overlooking (as of ver 3.1.2, anyway). Either that, or I am uncommonly blind. For instance:

- HOW do you provide feedback to the developers? (beyond blogging it like this)

- why is it not possible to see what workspace you're using?
couldn't this be visible someplace, w/o having to cancel out of a 'change workspace'?

- why does 'mark occurrences' only function in that annoying time-delay automatic mode?
how about an option to only change the marked word in response to ctrl-k, or something?

- why can't the package explorer offer something better than a flat list for projects?
ok, so java package names aren't directories; but my projects sure are!

- the 'Find/Replace' dialog doesn't offer to wrap back to the top once the end of the file
is reached, somewhat misleadingly reporting 'string not found'

Apart from things like that, I would give Eclipse 4.9 stars out of 5. For anyone learning to program these days, I would recommend going and getting Eclipse and the latest JDK from Sun (faster w/o NetBeans) if you don't already have one. Set up the JDK first, then unpack Eclipse someplace like C:\eclipse. I could probably be convinced to post the details of how to get set up in a future posting.

23 June 2006

Principle of Least Surprise

I'm constantly surprised by my computer, and that's not a good thing. A folder that last time showed as 'list' suddenly shows as 'icons' (the 'printers' folder is especially guilty of this). Sometimes 'new folder' is the top item on the File menu, sometimes it's 2/3 of the way down. Sometimes when I search for a word, MS Word/Adobe/any browser you can name will display it at the top edge of the view, sometimes at the bottom edge.

I think this sort of thing is at odds with the way the human brain works. If you perform the same action repeatedly, part of your brain stores that motion and you get so you don't have to think about all the details of an action, you just do it. Also, the brain seems good at remembering where things are in space. If your computer kept your icons where you last left them, you would be able to find them faster. The way things are with software today, you just can't make use of all this neural circuitry you have; every time something isn't where you left it, you have to bring in your full attention and puzzle it out. If software writers want to enable their users to be more productive, they should try leveraging the human ability for fast reactions by making reliably predictable interfaces.

Well, this is clearly not a very big problem, but it's my opinion that it would be nicer if computers were just a little less animated.

20 June 2006

SuperDog!

Is it a bird? Some kind of ugly plane? No! It's my flying dog!
The mild(ill?)-mannered canine, awaiting the next call to duty:

18 June 2006

No Past, No Future, Only Now!

Sometimes in physics you see those time-space diagrams, where space is horizontal and time is vertical. They may be called 'Feynman diagrams', but that might mean something else. Anyway, you hear a lot about space and time being this one four-dimensional continuum, and there seems always to be this implication that, if you had the ability to see in four dimensions, and could stand outside the universe, that you could look at the universe and see everything just laid out. And so, where does the concept of 'free will' fit, if your future is as solid as your past? And for that matter, where, in this static 4D hypercube of everything-ness, is there room for your perception that there is a past, present, and future?

Sometimes people will say that now-ness is like a 3D cross section that 'slides through' the 4D snapshot of everything. Well, when does that happen? This is as unsatisfactory as the homonculus theory was for explaining consciousness. The static 4D interpretation just trades away the in-universe concept of time for some hypothetical, outside-the-universe being's sense of time. That, AND you're still stuck with 'destiny'. And besides, how can anyone be 'outside' the universe?

So, the particular problem is, how does the 'sense of now' work? Why is 'now' special? It seems either that the static 4D everything view (with its implication of destiny) precludes a sense of now, and we're just deluded, or that there is some mechanism by which now-ness is extracted from it. Supposing it's right, and that somehow, now-ness happens, is there only one?

To see what I mean, imagine you have a paperback book, and oh say, 100 friends that all read at exactly the same speed. Read the first page of the book. Now, tear it out and give it to one of your 100 friends. When you finish the 2nd page, pass it to your friend, who passes the 1st page to someone else. In this analogy, the book itself is like the 4D static universe; there's no inherent 'now' in it, the entire history of the story is there all at once. In the act of reading it, you create a 'now' that passes through the history of the book. But so does your friend, and your 2nd friend, and so on. In this way, you could have an arbitrary number of 'nows' applied to the same history. No particular 'now' is better than any other. Our experience of 'now' is the most direct experience we have of the world, and in this scenario, there would be an infinite number of other 'nows' in which YOU would be happening; in which YOU would be experiencing; in which you would be as real as your are, well, now.

And that's just creepy!

So what if there isn't this 4D static construct? What if there is only NOW. Well, that would leave the future open, which probably makes a lot of us feel better about getting out of bed in the morning, but it would also mean that there's no past, wouldn't it? But, "I remember the past!" you say. Well, you think about the future, too, right? What if they're both just, well, not there? Predictions of the future get less accurate the further out you go, and data on the past gets less trustworthy the further back you look. It's like there's just this cloud of probability surrounding the now.

And since the speed of light is finite, if I stand 670 million odd miles away from you, I am in effect 1 hour in your past. Now even without relativity, imagine two flashbulbs go off simultanesouly, halfway between us, but one is red and 1 foot closer to you, and the other is green, and 1 foot closer to me. You see the red flash and then 1 ns later, the green one. I see the green one first, then the red one. Your history and my history aren't even the same history.

Maybe those zen guys have it right. Live in the moment. Time is an ineffable mystery. Don't just have a donut, really enjoy it.

Odd Poetry for the Day

air conditioner
makes everyone happiest
when set on high cool

Cheap Nixie Tube Power Supply

I've seen several nixie tube projects out there where people have spent unfortunate amounts of money on DC-DC converters or other regulated supplies, just to get the +170V DC anode supply. I would like to point out that a 1N4007 and a 10uF cap, and good old 120V 60Hz would do the job. The peak swing on line voltage is 120 * sqrt(2) = 169.7; coincidence? I think not! Also, persistence of vision being what it is, (and probably the neon doesn't go dark instantly) even half-wave rectified AC would probably suffice. Besides, the charming retro-flicker could only add to the appeal of those glowy digits, right?

Embarassingly Nerdy Humor

Ok, I'll admit making 'klingon programmer' jokes is pretty nerdy, but there are a lot of them out there. If you don't believe me, just google it. Anyway, here's my contribution:

Klingons do not program in 'C', Klingons program in 'K'! Your inferior CPU is incapable of running 'K'!

Special Effects

Brace yourself, it's rant mode time. What!? Again? Yeah - this time it's those cute little effects that seem to be sprinkled liberally throughout just about every new piece of software. Menus that have shadows, and fade in, and fade out. Annoying little splat-the-rat status boxes that slide in, announce something, and slide back out. OK, so maybe some people like them, but some people don't. The particular focus of my ire today is the fact that a lot of things now just don't give you the option to turn them OFF. Why is it, that some otherwise great piece of software, such as 'picasa', won't clue in to the fact that I have all those things turned OFF in my system, and follow suit? Hey, if there's a menu item to turn them off (in Picasa, say) that I missed, let me know! In fact, someone ought to put up a page for stuff like that. Well, enough said on that topic for today, I guess.

16 June 2006

When Free Isn't Really Free

Ok, here's a bit of a rant. What happened to the word free?! The best part of the internet used to be getting free stuff. Now if you search for 'free' stuff, at least 99.5% of the hits are really 'free trial' - and that usually means something that stops working before you've properly had a chance to see if you like it. Now, don't get me wrong, shareware is a great concept, but that's not what I'm on about. My point is that, if you search for 'free', you should get good old freeware. There just seems to be a problem where the stuff that isn't really free turns up ahead of the stuff that really is.

Ok, so, that's a lame rant, but that's all I have today. So now I shall do something about it. Try
this! (At least it was free stuff as of 16 June 06..)

15 June 2006

Retrocomputing Gadgets I'd Like to See (Apple)

If I had the time, I would look into using Express PCB (or something similar) to build a serial card for the venerable old Apple2, based on some kind of UART that has a FIFO in it, for example, the 16550. By having the UART buffer bytes, it seems like it ought to be possible to attain higher baud rates than just clunky old 2400. Of course the challenge would be to make it compatible with the 'super serial card' - which might be just not possible, as the old machines exposed a lot of hardware to the software. Anyway, if I could just IN#1 with it I'd be happy. If anyone goes for it, post it to applefritter and/or let me know!

14 June 2006

Inaugural Rant

Congratulations, you've found your way back to the beginning. Either that, or it's still just early days.