User Tools

Site Tools


courses:2010.01.19.serial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

courses:2010.01.19.serial [2010/01/19 07:32]
130.239.61.133 created
courses:2010.01.19.serial [2010/01/19 07:44] (current)
130.239.61.133 added code examples
Line 1: Line 1:
 +====== Serial communication ======
  
 +==== Serial what ??? ====
 +
 +[[http://​en.wikipedia.org/​wiki/​Serial_communication|Serial communiction]] from wikipedia.
 +
 +[[http://​www.tigoe.net/​pcomp/​code/​serial-communicationm|Serial Communication]] from Tom Igoe.
 +
 +We will use the [[http://​en.wikipedia.org/​wiki/​RS-232|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), [[http://​homepage.mac.com/​dalverson/​zterm/​|Zterm]],​ [[http://​www.furrysoft.de/?​page=goserial|goSerial]],​ [[http://​freeware.the-meiers.org/​|CoolTerm]]
 +
 +Windows: [[http://​www.chiark.greenend.org.uk/​~sgtatham/​putty/​|Putty]],​ HyperTerminal on Windows (buggy), [[http://​freeware.the-meiers.org/​|CoolTerm]],​ [[http://​braypp.googlepages.com/​terminal|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 ==== 
 +
 +[[http://​arduino.cc/​en/​Reference/​Serial|Serial]] from Reference
 +
 +<​code>​
 +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);
 +}
 +</​code>​
 +
 +==== Serial Communication in Processing ==== 
 +
 +[[http://​processing.org/​reference/​libraries/​serial/​index.html|Serial library]]
 +
 +<​code>​
 +// 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);​
 +    }
 +  }
 +}
 +</​code>​
 +
 +
 +==== 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