Camis

Controlling devices with characters

It is quite amazing how high level is the things that we do nowadays when writing code or using devices. If you are programming in Python, you can just import some optimized libraries and use their toolbox to solve your problem, and sometimes the library may even have a function to solve the exact problem you have, in one line.

At our photonics lab, we have spectrometers that can connect with the computer via a simple USB cable, and control them using - you guessed it - a Python library with the functions ready to use in one line.

However, I was recently tasked with creating a Python program to control and older device, more specifically a monochromator, which is used to infer the intensity of light at a given wavelength, from 1999 it seems? Anyway, the problem is that there is no library for using such a thing, not from a high level perspective like our other spectrometers.

The first issue came with the connection. There is no USB connection there, but a DB25

dbconnection

This connection is part of the RS-232 standard for serial transmission of data. Briefly, in this standard bytes are sent between two devices at an agreed rate, like 9600 bits per second. And this connector has 25 pins, each with its own goal.

dbconnection

The DB25 seems to be quite old. In fact, the DB9 connection is more commonly used, in part because most of the pins in the DB25 were not used. So I needed to get my hands on a DB25 to DB9 adapter, and then a DB9 to USB adapter.

So, now I just need to connect it to my computer and the case is over, right? Not quite. The computer can't communicate with it right away. So, you need to get the Hardware ID of the "Unknown Device" from the device manager of the computer and hope to find the right driver to install on Google.

Assuming you've made it, congratulations! Now it's the time to open your Python code, and import pyserial, to start a serial communication with the device. But.. how? Oh yeah, you don't have a manual and there is no one at the lab who knows how to work with the monochromator anymore.

So you go and send an e-mail to the company currently responsible for this monochromator model, wait some days, and they actually answer with useful information! The guy at the company unfortunately doesn't know how to answer some questions because the model is "quite old", but at least he sends you a detailed manual, probably from a comparatively recent model, but it probably still applies.

So, it seems the device works by receiving bytes, or characters, or strings. You name it. Each byte has a different meaning. If I send a <29> (decimal), byte then I am signaling that I want to change the wavelength, and the device responds with the same byte to indicate it received the information. Then you send 3 bytes to indicate the wavelength you want in hundredths of nanometers in base 256, since each byte can go from 0 to 255. The device then answers with a byte indicating the status of the operation (if it succeeded or failed, and why, according to a table) and a end byte to indicate it has ended. This byte communication is quite funny, because considering each byte as a character, then some operations start with an '$' or other random characters like '!' or '%'.

It is a very cool funny thing to code with, because I've never seen something like this before, but it makes a lot of sense, at least considering the context of the technology it uses.

Right now, I've coded many of its functions to, in the future, make a graphical interface (GUI) to use it like a normal program in the computer. I still need to figure out how to avoid communication errors and make sure everything works as expected. I may update about this project in the future!