Darin Greaham
6/21/2006 9:32:00 PM
First thing to do is decide which route you want to take: USB serial
adapter or TCP serial server.
USB Serial Adapter:
===================
You'll need the ruby-serialport library compiled and installed on OS X.
Then you'll need a KeySpan USB serial adapter. There are other
companies that make USB serial adapters but thus far, only KeySpan
seems to provide a universal driver. A driver for OS X is required no
matter which USB serial port adapter you use. If you're still using
PPC Macs, then there are several choices, but if you're on Intel Macs,
KeySpan is your best bet.
Code for USB Serial Adapter:
(NOTE: this isn't production-level code, just a sample - thus no error
handling)
# require the ruby-serialport library
require 'serialport'
# create the KeySpan single serial port USB adpater
port = SerialPort.new("/dev/tty.KeySerial1")
# KeySpan's driver also provides this sort of device name as well -
# this is needed when you're using
# more than one (like if you use their 4-serial port model)
#port = SerialPort.new("/dev/tty.USA19H7d2P1.1")
# if you use the 4-serial port USB adapter, your TTY's might look
# like this:
# /dev/tty.USA49W7d2P1.1 - 1st serial port
# /dev/tty.USA49W7d2P2.2 - 2nd serial port
# /dev/tty.USA49W7d2P3.3 - 3rd serial port
# /dev/tty.USA49W7d2P4.4 - 4th serial port
#
# these are documented and you can see them via Terminal, just go
# to /dev and do 'ls'
# configure the serial port
port.baud = 9600
port.data_bits = 8
port.stop_bits = 1
port.parity = SerialPort::NONE
port.flow_control = SeralPort::NONE
port.read_timeout = 0
# BTW: the stuff below here is the same if you use the USB or TCP
# approach - only the means of connecting to the serial port changes
# - the sending & receiving of data stays the same!
# all the functionality / methods of the standard Ruby IO class is
# available via ruby-serialport use IO::putc to send a single 'a'
# character
port.putc 'a'
# use IO::getc to read a single byte from the serial port
# of course you can put this in a loop and keep reading until
# something causes you to exit the loop
x = port.getc
# send a bunch of single characters
port.putc 'a'
port.putc 'b'
port.putc 'c'
# why am I using putc and getc? for us, we're dealing with instruments
# and we need to send / receive single characters at a time - you have
# to watch out for calls like IO::readlines, as it's looking for a
# separator, which might not be there - depends on what you're
# communicating with at the other end of the serial port.
TCP Serial Server:
===================
Just about any TCP-based serial port server should work. Most have 3
modes:
1. Virtual COM port - typically Windows only - used if you have an
existing app that communicates via COM port
2. Paired - you can tie two of the serial servers together - makes a
really long virtual serial port
3. TCP - this is what you want - you communicate over sockets to the
serial port(s)
NOTE: you can 1, 2, 4, 8, 16 or 32 serial-port serial servers - just
depends on your needs.
In TCP mode, you can setup the serial servers two ways: (1) TCP Server
or (2) TCP Client.
I find it easier to setup the serial servers as TCP Servers, each
serial port is accessible as a separate TCP port.
So if your serial server's IP address is 192.168.123.110, then serial
port #1 might be on port 4001 while serial port #2 might be on port
4002.
You setup the baud and all of that stuff for the serial port on the
serial server itself - most have a web and telnet interface.
Code for TCP Serial Server:
# using TCP, need Ruby's standard Socket library
require 'socket'
# connect via TCP to serial port #1
port = TCPSocket.new('192.168.123.110', '4001')
# now just use Ruby::IO methods
port.putc 'b'
port.putc 'c'
x = port.getc
# again, the Ruby::IO methods that work best for you and your
# needs might be different than ours
===
Hope that helps. It's really pretty simple - just decide which
hardware works best for you and that decides which connection method
you need to use (ruby-serialport or TCP). Once the connection to the
serial port is done, the sending & receiving of data is the same and
it's just plain ol'Ruby::IO methods.
Darin