EasyManuals Logo

Arduino uno User Manual

Arduino uno
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #274 background imageLoading...
Page #274 background image
byte currentByte = serialPort.readBytes(1)[0];
while (currentByte != NEWLINE) {
line[i++] = currentByte;
currentByte = serialPort.readBytes(1)[0];
}
return new String(line);
}
}
Although this program defines a class named
AnalogReader
, its not very object-
oriented. We only define it because everything in Java has to live in a class
context.
The
main
function implements the protocol for our Arduino sketch. First, we
make sure that the name of a serial port was set on the command line. Then
we use this name to initialize a new
SerialPort
object. To open the serial port,
we call the
openPort
method. After a two-second pause, we configure the serial
ports parameters.
In the loop that follows, we send the string a0 to the serial port using
Serial-
Port
s
writeString
method. Afterward, we read the result by invoking the
readLine
function and print it to the console.
Currently, jSSC doesnt offer a readLine function, so we have to write our
own. The function reads the Arduinos response byte by byte using the
readBytes
method, because jSSC doesnt offer a method for reading a single byte.
readLine
appends all bytes read to the byte array named
line
until it detects a newline
character (ASCII code 10). Finally, it converts the byte array into a
String
object
and returns it.
Heres how to compile and use the program:
maik> javac -cp jssc.jar AnalogReader.java
maik> java -cp jssc.jar:. AnalogReader /dev/tty.usbmodem24321
a0: 496
a0: 433
a0: 328
a0: 328
^C
AnalogReader
does exactly what its intended to do: it permanently prints the
values of the analog pin 0. Accessing a serial port in Java is a piece of cake
if you use the right libraries.
Note that jSSC also allows you to write object-oriented code. It has a
Serial-
PortEventListener
interface that makes it easy to decouple the handling of serial
report erratum discuss
Serial Communication Using Various Languages 261
www.it-ebooks.info

Table of Contents

Other manuals for Arduino uno

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino uno and is the answer not in the manual?

Arduino uno Specifications

General IconGeneral
Form factorArduino
CertificationRoHS, FC, CE
Processor model-
Processor frequency- MHz
Microcontroller modelATmega328
Microcontroller frequency16 MHz
DC input voltage7-12 V
Operating voltage5 V
DC current per I/O pin40 mA
Flash memory0.032 MB
Maximum internal memory- GB
SRAM (Static Random Access Memory)2 KB
EEPROM (Electrically Erasable Programmable Read-Only Memory)1 KB
Wi-FiNo
Number of analog I/O pins6
Number of digital I/O pins14
Weight and Dimensions IconWeight and Dimensions
Board dimensions53.4 x 68.6 mm

Related product manuals