User Tools

Site Tools


courses:2010.01.19.serial

Serial communication

Serial what ???

Serial communiction from wikipedia.

Serial Communication from Tom Igoe.

We will use the RS-232 standard. Asynchronous binary data across devices.

Transmit(TX) and receive(RX) lines/channels

Serial terminal/console/application ???

Very useful to view/edit/send serial data. Many options available

Mac OSX: Terminal/screen (included), Zterm, goSerial, CoolTerm

Windows: Putty, HyperTerminal on Windows (buggy), CoolTerm, Bray's Terminal

HOW TO USE:

  • Select the proper serial port (COM# on PC, /dev/usbXXXXXX.tty on Mac)
  • Select the right communication speed. 9600 baud is the default on most things.
  • Remember, only one application can access the serial port at one time. Close your connections when done (or trying to upload sketch to Arduino)

Serial Communication on Arduino

Serial from Reference

int analogValue = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog input on pin 0:
  analogValue = analogRead(0);

  // print it out in many formats:
  Serial.println(analogValue);       // print as an ASCII-encoded decimal
  Serial.println(analogValue, DEC);  // print as an ASCII-encoded decimal
  Serial.println(analogValue, HEX);  // print as an ASCII-encoded hexadecimal
  Serial.println(analogValue, OCT);  // print as an ASCII-encoded octal
  Serial.println(analogValue, BIN);  // print as an ASCII-encoded binary
  Serial.println(analogValue, BYTE); // print as a raw byte value

  // delay 10 milliseconds before the next reading:
  delay(10);
}

Serial Communication in Processing

Serial library

// Example by Tom Igoe

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
String myString = null;
Serial myPort;  // The serial port

void setup() {
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  myString = myPort.readStringUntil(lf);
  myString = null;
}

void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      println(myString);
    }
  }
}

Call And Response

Create your own packet. String butchery!

Etch-A-Sketch

courses/2010.01.19.serial.txt · Last modified: 2010/01/19 07:44 by 130.239.61.133