Administrative issues
- attendance
- journals, send in a version, URLs preferred
- March 1st -> Feb 28th
- phidget analog sensors can be used with Arduino. Just give them 5V and Ground, and the third pin will give you a voltage that you can read with analogRead(). The resistive sensors can be used also with Stamp's RCTIME.
Serial Communication
See practical how-to for USB-serial conversion
Serial= bits traveling one by one over a line
- When does the next bit begin?
Aynchronous= no clock line,
- separate clocks at the two ends
Two parties know several communication parameters:
- data rate (baud)
- number of data bits
- parity check?
- stop bits
Then they can coordinate some communication
Voltage level
- RS232 levels: -12V=1, +12V=0
- normal logic levels: 5V=1, 0V=0
- which is why RS232 is referred to as "inverted" from the logic-level point of view
- RS232 allows longer-wire communication
- but has large voltage swings so it consumes a lot of energy
- "line drivers" do the conversion automatically
2 lines (serial and ground) needed for 1-way communication
3 lines (RX, TX, ground) needed for 2-way communication
- RX of a device linked to TX of another
Typical serial setups
- An installation reads sensors and sends all its data to a computer
- An installation or a computer read a complex sensor (accelerometer, RFID reader)
- Two installations communicate with each other
- On computers
- you can do serial communication in Java, Macromedia Director, Processing...
- for environments that don't have a serial library (Flash?), you can use a converter from serial to TCP/IP (SerialServer)
- behaves like a net server but talks to a device...
- needed for communication to computers, or for monitoring with serial monitors
- pin 5 is Ground
- pin 3 is host TX (other party RX), 22 K resistor for surviving higher (12V) voltage
- pin 2 is host RX (other party TX)
- you can use your hack cable as a serial communication cable, just ignore the ATTN line. Line order:
- Ground
- ATTN (ignored)
- device (controller) RX (host TX)
- device TX, (host RX)
- you can connect it directly "near" the Stamp on the breadboard but then you lose a Stamp pin (the one near ATTN)
- if you use the same cable for both programming and communication, remember to close the connection before programming
Serial monitors
- needed to test your serial program on the controller
- you can typically both type and read in them
- on some you need to set "echo on" to see what you type
- typically let you easily select the communication parameters
- Windows Hyperterminal
- Arduino Serial monitor
- choose the port from Tools/SerialPort, you can monitor any port, not only the Arduino USB-serial o
- you'll have to switch it back when you program it
- although Arduino can't be programmed through ports > COM9, it can still monitor them
- BASIC Stamp
environment
- Run/Debug/New
- no need for a stamp to use it
- Mac Terminal
- many others
A little BASIC Stamp echo program, with communication indicator LEDs.
Serial Communication in Java
http://java.sun.com/products/javacomm/
- current version seems to be available only for Solaris and Linux, javax.comm
- old Windows version here
- rxtx.org implements the Sun API but you can use it even without importing javax.comm.* but gnu.io.* (the rest of the java text stays the same)
A how-to:
- in the minimal version of using RXTX without Sun's API
- simply put RXTXcomm.jar in the CLASSPATH or in ...jre\lib\ext (/Library/Java/Extensions/ on the Mac)
- and RXTXserial.dll (Windows) or libSerial.jnilib (Mac) in the PATH or in the current directory or in ...jre\bin (/Library/Java/Extensions/ on the Mac)
- in Eclipse
- just drop the two files in your project dir,
- put RXTXcomm in the Project's Java Build Path
- right-click on the Java class file and use "run as java application"
Example
- opening a port on 9600, reading data from it
- the "input" part of a serial monitor
Arduino serial
Arduino uses its serial port for programming (during bootloader run), and communication (incl. debugging)
-
That means that if you use Arduino's port for communication from another application, Arduino environment won't be able to program it
-
So stop your Java (or Flash or Processing or...) program, or close the connection (hang up) in HyperTerminal
- Sometimes the Arduino environment tries to upload, but it hangs.
- Then the port may remain busy and no application can use it to communicate with the Arduino sketch
- in an extreme case, take out the USB cable so the port dissapears
If you need more serial ports, use SoftwareSerial (has limitations!)
The led blink counting blinks out on serial
- we look what happens if we mess up the baudrate
If using the standard 0 and 1 ports (not USB)
- you need this typically to communicate to other devices (another Arduino, a Stamp, accelerometer...)
- if USB is connected, watch the RX and TX LEDs
- since your communication may conflict with the FTDI circuit, it is preferred to power Arduino externally and disconnect the USB
- a 10 KOhm from RX to ground is also recommended, so the circuit doesn't get "noise" at startup, messing up the bootloader
- even then, you will need to arrange your protocol so that the other communciation party will not mess up the bootloader at startup.
- Arduino cannot communicate directly from its digital ports (either through Serial or SoftwareSerial) to a RS232 connector of a computer
- regardless if the RS232 is built-in or USB-serial converted
- this is due to differences in voltage levels
- you can do it through a line driver
- but you rarely have reasons to avoid the USB, even if you power the Arduino separately
BASIC Stamp serial
- powerful serial communication, including timeout, parity check
- see SERIN (manual page 393) and SEROUT
- the modes are a bit cryptical
- 9600 baud, 8 data, No parity, 1 stop, non-inverted: 84
- 9600 baud, 8 data, No parity, 1 stop, inverted: 16468
- the BASIC Stamp has "inverted" modes, which can talk to RS232 even if they use logic levels
- the Stamp SIN and SOUT are "truly" RS232 (have line drivers)
- the other pins
- can normally drive RS232 pins if inverted
- can read RS232 levels if inverted, through a 22 kOhm resistor that limits current
- reading serial data from Arduino and writing it inverted to a RS232
Other issues
Data format (binary, ascii)
Synchronization, protocol
Serial timeout / freeze, buffers
Other communication modes
Many boil down to serial.
IRda
- serial communication over infrared
- requires direct line of site, or good reflection
USB
Bluethooth
- serial, there are adapters
- Java programming
- JSR 82, lets you do much more, including programmatic set-up of Bluetooth connections
- Bluetooth is programmed serially itself! Often JSR-82 software will need a serial adapter and driver
Ethernet-serial
WLAN-serial