How to add a serial port - CatGenius/catgenius GitHub Wiki
Introduction
It is not absolutely necessary to have a serial port installed in order to upgrade your box to CatGenius. You box will run as well without is as it does with. But for beta testing it is handy to have this port available because the software uses it to log what it's doing and what it's state is. This information may be crucial to diagnose problems. Additionally, the serial port will give you access to more advanced features of CatGenius in future releases of the firmware.
The Port
The micro controllers used, PIC16F877A and PIC16F1939, offer a Universal Asynchronous Receiver/Transmitter (UART), know to most of us a serial port or COM port. The CatGenie controller board already has traces that lead the pins of this interface to a connector. However, this connector was not mounted while assembling the board. Many manufacturers do this because their production type is mostly just an evolved prototype and to save costs they can simply leave off the components not used for normal operation.
On our board the UART signals are available on non-mounted connector J18. The meaning of each pin is even silk screened on the board:
Pin Signal Function
1 GND Ground
2 Tx Data transmit
3 Rx Data receive
4 5v Power supply
To most of us familiar with serial ports, these signals will look familiar.
TTL
The UART offered by the micro controller on our board uses different signaling levels, known as TTL. The main difference with RS-232 is the signaling level used: A signal is valid as a mark when it's voltage is ~5v. A signal is valid as a space when it's voltage is 0v. Interfacing these two signals directly would produce a space on RS-232 if a mark is issued on the TTL side, and an invalid signal if a space is issued. Fortunately a couple of clever integrated circuits are on the marked to solve this commonly occurring problem.
RS-232
Unfortunately these signals cannot be connected to a serial port directly. Common PCs have serial ports using UARTs too, but these ports are using the RS-232 specification for their signaling. Of the the many things this specification dictates is the signaling level: A signal is valid as a mark when it's voltage is between -3v and -15v. A signal is valid as a space when it's voltage is between 3v and 15v. Please note that the range between -3v and 3v is not covered. A signal within this range is considered to be invalid, a feature used to detect defective cabling. To make our controller UART signal compatible with the UART of a PC, we must adapt it to meet the RS-232 standard.
The level shifter
Circuits that convert one type of signaling level into another type are commonly referred to as level shifters. A very common one is made by Maxim under the type number MAX232, but other manufactures offer similar circuits as well. Using the application note diagram in the data sheet you can build your level shifter with very few extra external components. But since we're not the first ones who need to solve this problem, many ready made boards are on the market, all at a price too low to switch on the soldering iron for:
- Futurlec
- Sparcfun
- Acroname
When you buy a ready made converter, be sure to get a 5 volt version. There are also 3.3 volt and even 1.8 volt version available, but those will not work and will probably be damaged when connected.
Connecting the converter
Connecting the converter is actually straight forward. The ground signal should be connected to the ground signal and the supply voltage to the 5v. However, some manufacturers use a somewhat ambiguous labeling of the other two signals, Tx and Rx. Most manufacturers label the signals as what they do on the connector they are part of. So the signal labeled Rx is an input signal and needs to be connected to the output signal of the controller board, Tx. But some manufacturers label the signal as a function of the board. In that case the signal labeled Rx is an output signal, carrying the signal the board is receiving on it 9-pin D-connector. So the best thing to do is to to check the datasheet of the integrated circuit used. In case of a MAX232, pin 9 and 12 are TTL outputs, pin 10 and 11 are TTL inputs. Follow the traces from the pin marked Rx on converter board and if this pin ends at either of the 2 input pins of the MAX232, it needs to be connected to the Tx pin of the CG controller board. If it would be connector to either of the 2 output pins (unlikely), it should be connected to the Rx pin of the CG controller board. After that, the remaining Rx/Tx lines on the converter/controller can be interconnected too and that finishes the serial port.
Connecting it to your computer
The serial port as described above can be connected to a regular PC directly using a straight (not cross) cable with a male and a female 9-pin D-connector. This cable can easily be as long as 15 meters, but in that case it may be a better idea to use a serial port over Ethernet extender. I have very good experiences with the NetMedia SitePlayer. The charming part of those is that they are in fact TelNet servers, so they are compatible to a broad range of telnet programs. But they can also be used in conjunction with a Windows driver that just adds an extra serial port to your computer. A development suit is available for these as well, so it could even be turned into a stand alone CatGenius server with a website on it to configure and control it.
Receiving the data
Now CatGenius is connected to your computer, all you need is a serial terminal emulation program to read the data from the serial port, display it on your screen and optionally save it to a file. Windows has a - somewhat dated - standard program for this on board: Hyperterm. Alternatively you can use free and better alternatives as Putty, ExtraPutty or Tera Term.
Communication parameters
CatGenius firmware communicates over the serial port at 19200 bits per second, with 8 data bits, no parity and 1 stop bit. At the end of each line, it send a line feed (LF) character only, as Unix like computers do. If you are running on Windows, you may need/want to configure your terminal emulation program to add a carriage return (CR) character itself. ExtraPutty has a feature for this: 'Implicit CR in every LF' under Terminal options.
When your computer is connected, your terminal emulator is running, everything is configured properly and you power up your box with CatGenius installed, it should announce itself as below:
*** CatGenius *** Power-on reset Brown-out reset Set mode 0