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
Many devices connected to desktop computers today are serial: phones, PDAs, etc. So you can connect to these as well.
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)
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
- many others
- when using the Arduino monitor, don't forget to press Stop in it, or close the program, otherwise you keep the port busy
- In general, you can only use one program that works with a port at a time
Serial Communication in Java
In java, setting up almost any kind of communication boils down to getting an InputStream and an OutputStream.
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)
- both SunSPOT and Arduino use rxtx
A how-to:
- if you used Arduino or SunSPOT you don't even need to download rxtx as you have it already under the SunSPOT and Arduino dirs
- in any environment:
- put RXTXcomm.jar
- in the CLASSPATH
- or in ...jre\lib\ext (/Library/Java/Extensions/ on the Mac)
- put RXTXserial.dll (Windows) or librxtxSerial.jnilib (Mac)
- in the PATH
- or in the current directory
- or in ...jre\bin (/Library/Java/Extensions/ on the Mac)
- in Eclipse
- drop the two files (the JAR and the native library) in your project dir
- put RXTXcomm in the Project's Java Build Path (in Project/Properties/Java Build Path)
- right-click on the Java class file and use "run as java application"
Example
- listing all available serial ports
- opening a port on 9600 baud, 8 data bit, 1 stop bit, no parity
- you need the name of the serial port. Take it from e.g. the Arduino program (Tools/Serial Port)
- on the Mac you can do a ls /dev/tty.usb* to find USB-serial devices
- on all platforms, see what new COM ports appear when you plug in the USB (of the Arduino, the SunSPOT or the USB-serial converter)
- reading data from it
- like 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
SunSPOT serial communication
- see example , save it in se/kth/csc/CommLecture.java
- on USB, the SPOT communicates exactly like the Arduino (9600 baud, 8 data bits, 1 stop bit, no parity)
- so you can monitor the output (System.out) of a SPOT from any serial monitor (from our Java program, from the Arduino program, etc)
- if you are connected to a Base Station, you cannot monitor another SPOT via that
- you can get the two streams via
- OutputStream os= Connector.openOutputStream("serial://usb")
- InputStream is= Connector.openInputStream("serial://usb")
- to communicate serially with other devices, there is a UART (universal asynchronous receiver transmitter) on the EDemoBoard
- transmission (TX) connected to D1
- reception (RX) connected to D0
- EDemoBoard.getInstance().initUART(9600, false);
- unlike other serial communication, there is no InputStream and OutputStream, but receiveUART(), sendUART() for one byte each. receiveUART() throws an IOException if there is nothing available to read (i.e. the read is non-blocking)
- on the Purple (next version, 3.0) SDK, there is a way to get more standard communication via Connector.openInputStream("edemoserial://usart?baudrate=9600")
- hough that's also affected by a bug.
- the bug is in the new Purple method EDemoBoard.UARTAvailable()
SunSPOT radio communication
See example , save it in se/kth/csc/CommLecture.java
Several protocols accepted:
- radiostream: reliable, buffered, stream-based
- StreamConnection conn = (StreamConnection)Connector.open("radiostream://0014.4F01.0000.190C:100");
- that is, radiostream://IEEE-address:Port
- both parties have to use the same port number
- conn.openInputStream() and conn.openOutputSream() provide the two communication streams
- the output stream has to be flush()ed for immediate transmission to occur
- radiogram: unreliable, datagram-based
- allows multiple clients to connect to a server (radiostream is one-to-one)
- datagrams are limited-length pieces of data, can also contain a destination
- can broadcast datagrams to all SPOTs that listen
- you can obtain quality data about a certain radiogram (e.g. strength: datagram.getRssi()) which can give a clue about how "far" the other party is (though radio communication quality varies a lot e.g. when lots of people come into a room)
- http
- HttpConnection conn= (HttpConnection)Connector.open("http://www.kth.se/index.html");
conn.setRequestProperty("Connection", "close");
InputStream is = conn.openInputStream();
- requires at least one base station being connected to a computer where the SocketProxy runs: ant socket-proxy
- socket
- http is based on it
- requires SocketProxy
- gives a socket to an internet host, which can be used e.g. to communicate with another program according to a protocol
Radio properties can be changed per connection, or only for the whole system:
- timeout
- broadcast number of hops
- channel and PAN number
- output power
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 sight", or good reflection
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
I2C
- just two wires for a large number of devices
- SunSPOT supports it on pins D2 and D3. May need 3V to 5V conversion