Reading from Serial port that Arduino is outputting to: COM4 problem.
in
Integration and Hardware
•
1 year ago
Hi All,
Arduino is attached to COM4 and is running a program that writes data back to Serial:
- const int SENSOR=0;
- int val=0;
- void setup()
- {
- Serial.begin(9600);
- }
- void loop()
- {
- val = analogRead(SENSOR);
- Serial.println(val);
- delay(100);
- }
When I try to read output from COM4 into processing I use the follow code from the Processing Serial manual:
- import processing.serial.*;
- Serial myPort; // The serial port
- void setup() {
- // 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].
- // Open whatever port is the one you're using.
- myPort = new Serial(this, Serial.list()[2], 9600);
- }
- void draw() {
- while (myPort.available() > 0) {
- int inByte = myPort.read();
- println(inByte);
- }
The output from Processing is as follows:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
[0] "COM1"
[1] "COM3"
[2] "COM4"
gnu.io.PortInUseException: Unknown Application
at gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at ReadSerial.setup(ReadSerial.java:32)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.<init>()
at processing.serial.Serial.errorMessage(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at processing.serial.Serial.<init>(Unknown Source)
at ReadSerial.setup(ReadSerial.java:32)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I do not receive this error if I change this line:
- myPort = new Serial(this, Serial.list()[2], 9600);
to
- myPort = new Serial(this, Serial.list()[0], 9600);
or
- myPort = new Serial(this, Serial.list()[1], 9600);
but neither is my output displayed
1