Macintosh Serial input and output

Macintosh computers no longer have serial output hardware (appart from the modem port), but OSX has a serial driver. The easiest way to get a serial port is to buy a Keyspan USB to serial converter (see photo) . To get it to work you will have to install a serial driver . The local agent for Keyspan is Fosh Australia. Keyspan has been bought by TrippLite.
Step 1. Set up Keyspan USA-28x USB Twin Serial Adaptor
  1. Download and install Keyspan driver for USA-28x USB Twin Serial Adaptor - also for USA-19HS high-speed serial adapter with DTE male connector.
  2. plug Keyspan USA-28X USB to Serial adapter into a USB port
  3. Run the Keyspan Serial Assistant to check driver installation and port number
Serial assistant will show two ports You can also check existence of ports with System Preferences -> Network
Step 2. Test the serial link - you need
  1. a program running on the Macintosh - Zterm or one you write
  2. an external device that can display characters - dumb terminal or LCD display
  3. a cable between the Keyspan USA-28X and the external device
Step 3. Download a dumb termial emulator
  1. Download a copy of Zterm or goSerial (a Cocoa program with similar functionality
  2. Setup Zterm as shown in diagrams below
    1. Settings->Modem Preferences - select serial port - use port 1
    2. Settings->Connection - select data format, name device and select echo
  3. To commence serial i/o select Dial->Keyspan - should get "Direct Connect Keyspan"
  4. Type on Mac keyboard and see characters appear in Zterm window (local echo is on)
Step 4. Simple loop back test - if you wany to
  1. make a loop back connector to connect Tx to Rx (either to plug into Mini Din8 or DE9 cable)
  2. In Zterm, type a character
  3. It should appear twice: local echo and loop back

Loop back connections for Mac DE9 cable on right

Step 5. Choosing the external device
  1. if you have a dumb terminal you can use it (see end of this page for possible cable)
  2. otherwise purchase a Scott Edwards BPI 216 serial LCD module, download manual
  3. Also available from Parallax and Jameco electronics
  4. Images below show data appearing on display (left), Zterm window (right) and connections (middle)
Step 6. Make a cable to connect the LCD display to the Keyspan USA-28X
  1. Option 1 - cut an apple serial cable (DIN8 to DIN8) in two
    1. solder the wires to the LCD or use a prototypng card
  2. Option 2 - use an apple serial DIN8 to Apple DB9 female cable (see R/C servo page)
    1. make a cable with an Apple DB9 male on one end and solder the other end to LCD
Wires are colour coded
One end matches pin numbers
Other end swapped
  • 1&2
  • 3&5
  • 6&8
Step 7. Test the serial link
  1. plug everything together and start Zterm
  2. Characters you type on Zterm should appear on the display
  3. As the characters will run off the end you need to ............
Step 8. Write a program to send characters to the LCD
  1. Download the manual
  2. Look at LabVIEW program below, and get a copy of LabVIEW student edition
If you are having problems with VISA with Intel Macs - VISA Find Resource.vi and VISA resource name selector, try using a string constant instead.

LoopBackTest.vi uses a string but only finds 1 of the 2 Keyspan ports.

See: file for information on using LabVIEW with Intel Macs

A LabVIEW program consists of two parts:
  1. Code - written in a graphical language
    1. Icons representing functions (vis) are dropped, and
    2. connected using wires representing data flow
  2. Front Pannel - graphical user interface, where
    1. user enters data - e.g. selects serial port, and
    2. program outputs results and reports errors etc.
You can download this program

WriteStringLCD.vi (56Kb) - CTRL click to get file

Algorithm:
  • get a reference to the serial port (VISA resource)
  • get the baud rate (9600)
  • Configure the serial port
  • Combine 4 strings:
    • Instruction
      • ASCII 254 (change to instruction mode)
      • ASCII 129 (move cursor to line 1 character 1)
    • Data - string constant (Welcome to LCD)
    • Instruction
      • ASCII 254 (change to instruction mode)
      • ASCII 195 (move cursor to line 2 character 3)
    • Data - String entered on front pannel (hello Danny)
  • write the string of ASCII characters to the serial port
  • where it appears on the LCD display

Macintosh Java serial

Java can use serial i/o if a serial library is installed. Two libraries have been defined for use with Java: Javax.com and GNU.io. Code for developing serial in Java can be obtained from SerialIO and RXTX
RXTX produces both a Javax.com library ( rxtx-2.0-7pre1) and a gnu.io library (rxtx-2.1-7r2.zip (Final)) for Download. The folllowing instructions are for Version 2.1.7 with the assumption that you can recompile your application to use gnu.io. The installation for Version 2.0 is different. Both can be painful.

Installing and testing gnu.io

  1. If you havn't installed and tested your serial hardware and driver (Keyspan - see above) and tested it with a loop back cable do so before you go any further.
  2. Down load rxtx-2.1-7r2 from RXTX and find in /MACOSX_IDE the files librxtxSerial.jnilib (320Kb) and RXTXcomm.jar (112Kb)
  3. Copy them to HardDrive/Library/Java/Extension - where HardDrive is the name of your OSX boot drive
    1. the compiler will find them there without linking them into the project
    2. BUT if you get namespace conflicts you will need to put them elsewhere an link them in XCode
  4. Inorder for rxtx-2.1-7 libraries to conform to MacOSX security, spool and lock folders have to be set up:
    1. Run the Terminal utility (/Applications/Utilities/Terminal)
    2. check whether /var/spool/uucp exists
    3. if it doesn't exist then create it
      1. sodo mkdir /var/spool/uucp
        1. sodo - do as super user (admin password); mkdir - make a new directory
      2. sodo chmod g+w /var/spool/uucp
        1. so that permissions are drwxrwxr-x
        2. chmod - change mode (permissions)
    4. check that each intended user is a member of the uucp group
      1. sudo niutil -readprop / /groups/uucp users
    5. if not then make them a member by:
      1. sudo niutil -appendprop / /groups/uucp users <username>
    6. Check whether /var/lock exists
    7. if it doesn't exist then create it
      1. sudo mkdir /var/lock
  5. You should reboot your computer to ensure that OSX has picked up all these changes
Keyspan USB to serial port with 2 loopback cables

Inside: Apple serial cable

Outside: DE9 DCE loop back cable

Testing Java Serial

Example programs can be found on the RXTX and Javax.com web sites. These can be used for loopback tests or as serial applications. Note: the use of import gnu.io.*; instead of //import javax.comm.*;

SerialDemo is a Java Swing application that displays a GUI to enable you to select a port and open it, and experiment with various port settings. It is set to no echo, so characters you type on the top pannel will appear on the bottom pannel when a loopback cable is connected. When connected to a serial device, it will support bi-directional interaction. An XCode project is available for download. (100kb .sitx) (.sitx files can be expanded with Stuffit expander from Allume Systems)

Serial Demo Run Log

[Session started at 2006-07-18 10:05:24 +1000.]
Experimental: JNI_OnLoad called.
Stable Library
===============================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
PORT NAME gnu.io.CommPortIdentifier@66afb3 /dev/tty.KeySerial1
RXTX Warning: Removing stale lock file. /var/lock/LK.003.009.002

TwoWaySerialComm Run log

[Session started at 2006-07-18 10:16:34 +1000.]
Experimental: JNI_OnLoad called.
Stable Library
===============================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
/dev/tty.modem - Serial
/dev/cu.modem - Serial
/dev/tty.KeySerial1 - Serial
/dev/cu.KeySerial1 - Serial
/dev/tty.USA28X2b2P2.2 - Serial
/dev/cu.USA28X2b2P2.2 - Serial
6 ports found
Current owner is null
Name is TwoWaySerialComm
RXTX Warning: Removing stale lock file. /var/lock/LK.003.009.002
Serial port is /dev/tty.KeySerial1
keys typed on keyboard appear here (no loop back connector)

A second program: TwoWaySerialCom (XCode project, .sitx 60Kb) shows how to write a serial connection without a GUI - NOTE: you have to hard code the port name in line 164.

Cocoa Serial Programming

Two classes for accessing serial ports are available for download from harmless in Germany

Macintosh wireless cable replacement

Severalcompanies offer wireless serial links based on Bluetooth technology, such as the BCR01 from Air2u shown connected to a Macintosh on left and the other end to the EiMU INS sensor from CSIRO on the right. BCR01 is available in Australia as a Cable Replacement Kit.
The kit comes with two transceivers both with DTE male connectors and a null modem connector to change one to a DCE. A null modem crosses over the rx and tx signals
The kit comes with a Windows application for changing the settings from the default: 115200 baud, flow control ON, handshake ON.

Probably uses a hardware line (DTR?) to switch to configuration mode so that it can use the serial line to talk to the Bluetooth card.

A loopback connector may be used with Zterm to test whether the wireless link is working RS232 signals are named from the DTE point of view.
  • DTE - computer, terminal
  • DCE - Modem
Cable used in photo above to connect to mac DE9 cable

Emulating an IBM PC Serial Port

Many devices come with a DE9 socket ready to plug into an IBM PC serial port
  • Computer - a DTE male socket
  • Device - a DTE female plug
Wiretek make a single port USB to serial cable with the Prolific chip

A Mac OSX driver can be downloaded

Programming is the same as for the Keyspan, except that the VISA serial close vi will cause a fail to open on subsequent runs, so it has to be removed.

Beware: these USB chips don't have serial numbers
(Keyspan do) so if you plug more than 1 into your
Mac it is very difficult to tell which is which in software.

Another way to emulate a PC port is to make a cable to plug into the Keyspan

Note: the Mac serial port (mini-DIN 8) is only ± 5volts while the PC (DE9) is ± 12 Volts

  • This becomes a problem when the device is sourcing its power from the serial port
  1. Cut an Apple serial cable in two
  2. Wire a DE9 male connector to the end
Return to Phillip McKerrow or site Index
Page updated Friday 26th August 2009