Kevin's done an excellent job at explaining what the syntax error you got means and how to tackle it.
In addition to that I'd like to point out an issue I spotted in Simon's question as well:
float inByte = port.read();
According to Serial's documentaiton on read():
Returns a number between 0 and 255 for the next byte that's waiting in the buffer. Returns -1 if there is no byte, although this should be avoided by first cheacking available() to see if data is available.
There are couple of issues:
- EngduinoThermistor returns temperature as a float and that is not what
read()
returns. It returns one byte at a time. Let's say the temperature is 25.71
. That will be 5 bytes (2 5 . 7 1
which as bytes represented from 0 to 255 would be 50 53 46 55 49
). You would need to convert each byte to a character then append each character to a String until the new line (\n
) character is found, then the byte counter would reset. After the full string is put together, it can be converted to a float. I recommend using either readStringUntil() or a combination of bufferUntil() and readString()
- You are not checking if read() returns -1 which you should always do unless you use serialEvent()
I recommend double checking if the data is displayed correctly in Arduino's Serial Monitor first. If it is, close Serial Monitor (as you can open a single connection to a serial port at a time), then run your Arduino code.
You can also take advantage of this one weird trick: Arduino already knows what serial port and baud rate you're using and saves this as a preference (so you don't have to select the port every single time you restart it). This is actually stored in a Java Properties format that you can easily parse in Processing.
You can easily find the location to this file from the Arduino Preferences panel:

Here's a basic sketch that tries to read the serial port name and baud rate from Arduino preferences then read a string and print it to the console (while trying to handle some errors):
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import processing.serial.*;
String portName = null;
int baudRate = 9600;
Serial arduino;
void setup(){
readArduinoPrefs("/Users/George/Library/Arduino15/preferences.txt");
println("last used Arduino port",portName,"with baud rate",baudRate);
try{
arduino = new Serial(this,portName,baudRate);
}catch(Exception e){
System.err.println("ERROR connecting to Arduino port " + portName);
String message = e.getMessage();
if(message.contains("not found")) println("Please make sure your Arduino is connected via USB!");
if(message.contains("busy")) println("Please make sure the port isn't already opened/used by Serial Monitor or other programs!");
e.printStackTrace();
exit();
}
}
void draw(){
}
void serialEvent(Serial s){
println("received string",s.readStringUntil('\n'));
}
void readArduinoPrefs(String prefsPath){
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(prefsPath);
prop.load(input);
portName = prop.getProperty("serial.port");
baudRate = int(prop.getProperty("serial.debug_rate"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
On the pragmatic side, you can always type in the port name and baud rate as you configure it in Arduino manually, just remember to double check the syntax, in your case:
Serial(parent, portName, baudRate)
e.g.
new Serial(this, "/dev/tty.usbmodem1411", 9600);
as Kevin pointed out