Friday 10 June 2011

Connecting To A Serial Device


For this post we're going to talk to a device via our computer, using a program called Minicom, which is a serial terminal.

First, pick on a device. A modem, or some other device with a serial port. An old modem is probably the most likely device you might have to hand.

Not all devices with a serial port can be interacted with easily using a terminal. Some devices use a packet based protocol, where each command is a block of characters called a packet, which may have a start character, an end character and a checksum. This is rather difficult to talk to using a terminal like Minicom. Other devices are command based, like on a command line terminal. We can type in words, letters or numbers, and get a response from the device. And this is the type of kit we are going to talk to in this post.

____________________



Having the manual for the device would also be useful, as this will aid us with the cabling, and some other settings. Having the serial cable supplied with the device will make the task even easier.

If your equipment  doesn't have a serial cable supplied, then you will need to purchase one, or make one. The cable that attaches the device to your computer obviously needs the right connectors at both ends: D9 or D25 (or something more exotic).

The cable also needs to be either a straight through cable (modem) or a crossover cable (null-modem). In serial comunication terms, the units you are connecting are either a DTE or DCE - Data Terminal Equipment or Data Communications Equipment. If we go back to the start of serial communications, it was mainly used to connect a terminal to a modem. The terminal is the DTE and the modem the DCE. In this case - connecting DTE to DCE, we require a straight cable. If two terminals are connected together - or two PCs - they are both DTEs, so a crossover cable is needed.

Once we have our equipment cabled together we can turn to setting up Minicom. If it's not been used before, there's a bit of configuration to start with.

Install minicom with your distributions software package installer. It should be included in your distributions software repositories - I've not found a distribution that doesn't include it.

To configure Minicom the first time, run, as root (or sudo for Ubuntu), like this:

 minicom -s

Select 'Serial port setup', then chose your setup - the important ones are the 'Serial Device' and 'Bps/Par/Bits'.

Option 'A -Serial Device' is your serial port. To find out what your serial port is called, you can follow the instructions in this previous post, HERE.

Option 'E - Bps/Par/Bits' is the baud rate, parity and number of data and stop bits.

The baud rate (or speed or bits per second) may be detailed in the manual for your device, in which case select this baud rate. Your device may provide switches for you to select the baud rate, so just pick one, and set it on your device, and in Minicom. It doesn't matter which, as long as they are the same. If your device is a modem, modern domestic ones often detect the baud rate you are using, and configure themselves to use this. A values of 9600, 19200 or 115200 should work, if you think your device can auto set the baud rate.

For the parity and stop bits, again the manual is the best place to find these. If, however, they are unknown, try 8 data bits, no parity, and 1 stop bit.

Once the 'Serial Device' and 'Bps/Par/Bits' have been dealt with, exit the 'Serial port setup' section, back to the first configuration menu, by pressing the escape or return key.

Minicom's default setup is for transferring data via a modem, so it helpfully sets up the modem, by sending various configuration strings. Now, we may not want this configuration, if we are not talking to a modem. Where modem setup is not required, we therefore have to clear the strings, which is done, thus:

In the 'Modem and dialing' section, select 'A' for 'Init string', and clear the string. Select 'B' next, for 'Reset string', and clear this string, as well. Exit this menu with a return or escape.

Once we have our configuration, use the 'Save setup as dfl' option, and then Exit the configuration menu.

We are finally at the point where we can try and talk to our device. If it is a modem you are trying, the at command is a good start, and you may get this:

 at
 OK


Here, the modem has replied to our at prompt, with an 'OK'.

Minicom can now be started by a normal user. You may want to save different configurations for different devices. This is possible with the 'Save setup as..' option, although you must be root to use this feature. You can, however alter the configuration, then do another
'Save setup as dfl' without being root

No comments:

Post a Comment