Dies ist eine alte Version des Dokuments!
Beispielcode:
// This program shows how to read numbers in a human readable "ASCII" format from the Serial Port // Created 2013 by Felix Bonowski // This code is in the public Domain. import processing.serial.*; //the functionality for using the Serial Port lies in a speical library. This tells Processing to use it. Serial myPort; // Create a "serial port" object/variable. "Serial" is the type of the object (just like "int"), "myPort" is the name of the variable //this function gets called at the beginnign of the program void setup () { // Initialze Serial communication. There may be multiple serial ports on your computer, so we have to select one: String[] portNames=Serial.list();//Serial.list() returns an array of port names println(portNames); //print them to the user // initialize the Serial port Object using the last name from the list and a Baudrate of 9600 myPort = new Serial(this, portNames[portNames.length-1], 9600); // Set up the screen: size(1024, 512); // set size of the window background(0);// set background color to black stroke(255); // set the color of things we draw to white // make the loop run as fast as it can so we dont loose any data frameRate(2000); } //this function is called continously, just as "loop" in arduino void draw () { // get data from the Serial Port float[] data=readLineFromSerial(myPort); //check if data was received... if (data.length>0) { println( "\ngot fresh data!"); } //or do something with all values: for(int i=0;i<data.length;i++){ print(data[i]); print("\t"); } } //this function reads a line from the serial port and returns an array of numbers //multiple numbers can be received if they separated by tab stops ("\t") float[] readLineFromSerial(Serial port) { byte temp[] = port.readBytesUntil('\n'); //read data from buffer until a new line is finished // check if any data is available if (temp == null) { return new float[0]; //got nothing - return an empty array } else { String inBuffer= new String(temp); //convert raw data to a string inBuffer=trim(inBuffer); //cut off whitespace float[] numbers=float(split(inBuffer, "\t")); //cut into pieces at tab-stops and convert to an array of floats return numbers; //return the numbers we got } }