Serial Exception Problem
in
Core Library Questions
•
3 months ago
Hi everyone,
first of all I would like to apologize for not be used to forums rules and netiquette, but I'll try to do my best in order to avoid any mess. Here's the question. I wrote a program for reading data sent from my arduino 2009 board to the pc via serial port. The program itself is working fine, but here's the point: I need to manage the error rising from accidental removal of the usb cable which arduino is attached to. Even a generic exception doesn't work and the program crashes. The code part vulnerable to the error should be this
- void setup()
- {
- //..... usual setup stuff!
- portArbitration() ; //try to interrogate any available port from Serial.list() until it gets the arduino one
- }
- void draw()
- {
- background(0);
- while (myPort.available () > 0)
- {
- //read value until carriage return
- concentration=myPort.readStringUntil(13);
- if (concentration != null)
- {
- //display values from the sensor in a ControlP5 textfield
- sensorDisplay.setValue(concentration);
- //write_to_file code (that I have not completed yet ) goes here
- }
- }
- }
And when I disconnect usb cable, here's what I get:
- java.io.IOException: No error in nativeavailable
- at gnu.io.RXTXPort.nativeavailable(Native Method)
- at gnu.io.RXTXPort$SerialInputStream.available(RXTXPort.java:1532)
- at processing.serial.Serial.serialEvent(Unknown Source)
- at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
- at gnu.io.RXTXPort.eventLoop(Native Method)
- at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)
- java.lang.NullPointerException
- at processing.mode.java.runner.Runner.findException(Runner.java:925)
- at processing.mode.java.runner.Runner.reportException(Runner.java:870)
- at processing.mode.java.runner.Runner.exceptionEvent(Runner.java:813)
- at processing.mode.java.runner.Runner$2.run(Runner.java:699)
Is there any try - catch structure I could use to manage this? Maybe is a stupid question, but i'm quite new to Java and OOP. Thank you for every support!
1