We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Syntax Question (Read 635 times)
Syntax Question
Jul 1st, 2008, 2:07am
 
Hello,
The code below is for trying to get 100, 3 digits plus 2 decimal points (3.94) plus a cr (string) from a pic (basic stamp).
/**
* Serial Datalogger
* listens for bytes received, and displays their ASCII value.
* This is just a quick application for testing serial data
* in both directions.
*/

import processing.serial.*;

Serial port;        // The serial port
// float thisByte;  // Variable to hold recieved values
int[] numbers = new int[100];
String s = new String(char(numbers));

PFont fontA;        // Font for printing

void setup() {

 size(200, 200);

 // Load and prep fonts to print to window
 fontA = loadFont("CourierNewPSMT-24.vlw");
 textFont(fontA, 24);

 // 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].
 port = new Serial(this, Serial.list()[0], 9600);

}

void draw() {
 background(0);
 text("Rec: " + s, 10, 130);
 // If there are bytes available in the input buffer,
 // Read them and print them:
 while (port.available() > 0) {
  s = port.read();
 }
}
I keep getting a syntax error. I am new to processing.
Thanks,
Curtis
Re: Syntax Question
Reply #1 - Jul 1st, 2008, 7:25am
 
Just by eyeballing, I don't see really a syntax error, and you don't tell us which it is, but a couple lines is suspicious:

 int[] numbers = new int[100];
 String s = new String(char(numbers));

You don't initialize (nor use) numbers array, so you init the string with a bunch of null chars. Perhaps legal, but in all cases, I don't see the point.
Just declare:

 String s = "";

Ah, I see. In the doc, we have:

void draw() {
  while (myPort.available() > 0) {
    int inByte = myPort.read();
    println(inByte);
  }
}

ie. read() returns an int (the refs, are, for some reason, missing the type of the return value for the functions, even if it can often be guessed from the examples).
So

 s = port.read();

should make an error, since you try to shoehorn an int into a string.
You should use:

 s = String.valueOf(port.read());

instead.
Page Index Toggle Pages: 1