16 October, 2009

Serial Port Magic vol.3 (controlling pins)

Data is not the only thing that can go through serial port. Actually serial port can output some currency (3.. 12V or -3.. 12V) - getting interesting, hah! To look it a little closer you probably need multimeter to measure currency coming out of serial port pins.

I'm not going into detail to tell you what this and this pin is for. I have chosen DTR (Data Terminal Ready, nr.4) pin to play with - just look it up from picture. And you also have to look for Signal Ground - pin nr. 5. Those are the pins you're going to user to measure currency. For my Mac it is -6.3V. To change it to 6.3V you need couple of lines of code and to compile it. Now from command line just run
> ./setSerialSignal /dev/tty.usbserial 1 0
and the you go again. I haven't yet figured out how to use it, but thats a start all right.

Serial Port Magic vol.2 (loop-back cable)

Probably for now you have been surfing through entire Internet looking for way to emulate serial device. But the truth is - there is not common way to do it with software only. You have to do it hard way. And it's gonna cost you a little (10-15EUR).

To make a loop-back cable, first you need to go shopping for some stuff:
  1. USB/RS-232 adapter (TrendNet TU-S9) - works for Mac
  2. RS-232 female plug
  3. couple of inches thin wire



On the picture you can see the solution that worked for me. Just connect pins 2-3 and 7-8, you don't even need 1-4 but do it anyway. For testing ZTerm is good for Mac or what ever terminal client you like. Every thing is clear so far - good.

Not the thing with serial port is that two different applications try to read from is simultaneously, then only one of them actually receives the data and it's not guaranteed which one. Java library uses locking technic to allow only one app to access serial port. For my Mac locking files are located at /var/lock. Every time java app. opens serial port, it creates locking file into that folder and removes it after port is closed. To make one app to read from and another to write into serial port, what you do is first start reading app., then delete locking file and after that run you serial port writer. Simple, right!?

Serial Port Magic vol.1 (programming)


At some point you will have a need to do some hardware programming and thats where you meet Serial Port (RS-232). Don't be afraid - that doesn't necessarily mean low level C/C# coding. There are plenty of higher level API's for Java (java.comm and rxtx) and even for PHP and Python. So, let me tell you how to get started.

I have tried out RxTx library and it worked perfectly. Just follow the instructions on their wiki page and you should be pretty much safe. There are also some code samples that you could use. Couple of things you might run into are:
  • make sure you have RXTXcomm.jar in your classpath!
  • also make sure lock file directory has a writing access
And there you go. Simple as 1-2-3 :)