topical media & game development

talk show tell print

graphic-processing-site-examples-Libraries-Serial-SerialDuplex-SerialDuplex.pde / pde



  
Serial Duplex by Tom Igoe. Sends a byte out the serial port when you type a key listens for bytes received, and displays their value. This is just a quick application for testing serial data in both directions.

  
  
  import processing.serial.*;
  
  Serial myPort;      // The serial port
  int whichKey = -1;  // Variable to hold keystoke values
  int inByte = -1;    // Incoming serial data
  
  void setup() {
    size(400, 300);
    // create a font with the third font available to the system:
    PFont myFont = createFont(PFont.list()[2], 14);
    textFont(myFont);
  
    // 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  FTDI adaptor, so I open Serial.list()[0].
    // In Windows, this usually opens COM1.
    // Open whatever port is the one you're using.
    String portName = Serial.list()[0];
    myPort = new Serial(this, portName, 9600);
  }
  
  void draw() {
    background(0);
    text("Last Received: " + inByte, 10, 130);
    text("Last Sent: " + whichKey, 10, 100);
  }
  
  void serialEvent(Serial myPort) {
    inByte = myPort.read();
  }
  
  void keyPressed() {
    // Send the keystroke out:
    myPort.write(key);
    whichKey = key;
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.