Permalink
Browse files

Added some basic (and not functioning) serial communication in

Processing. It's giving an unexplained null pointer exception.
  • Loading branch information...
1 parent cda6bdf commit 648938052a49453433beb024e6a8a9d4fc69ac63 Chris Hodapp committed Sep 5, 2011
Showing with 49 additions and 0 deletions.
  1. +49 −0 shiftbrite_master/shiftbrite_master_processing/shiftbrite_master_processing.pde
@@ -0,0 +1,49 @@
+import processing.serial.*;
+// =============================================================================
+Serial serialPort;
+
+void setup() {
+
+ try {
+ serialPort = initSerial("/dev/ttyUSB0", 9600);
+ } catch(Exception e) {
+ print("Error opening serial port: " + e.toString());
+ }
+ print("Successfully opened serial port!");
+
+ String test = "Test message!";
+ byte[] test_bytes = test.getBytes();
+ try {
+ serialPort.output.write(test_bytes);
+ } catch(IOException e) {
+ print("Error writing to serial port: " + e.toString());
+ }
+}
+
+void draw() {
+ //print("Test");
+}
+
+// "name" = name of serial port (e.g. /dev/ttyUSB0). If left blank, the first
+// one found will be chosen. 'baud' is baud rate.
+Serial initSerial(String name, int baud) throws Exception {
+ Serial port = null;
+ String serialName = name;
+ if (name == null) {
+ String[] ports = Serial.list();
+ print("Found " + ports.length + " serial ports!\n");
+ for(int i = 0; i < ports.length; ++i) {
+ print("Serial port " + i + ": " + ports[i] + "\n");
+ }
+ if (ports.length == 0) {
+ throw new Exception("No serial ports found!");
+ }
+ serialName = ports[0];
+ }
+ print("Trying serial port " + serialName + "\n");
+
+ port = new Serial(this, serialName, baud);
+ return port;
+}
+
+

0 comments on commit 6489380

Please sign in to comment.