Results 1 to 2 of 2
- 11-03-2012, 12:56 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 1
- Rep Power
- 0
unreported exception java.io.ioexception must be caught or declared to be thrown
Hi all,
I'm new to java n forum.....
I am developing java application that can read/write to comm port.
While i was reading i don't have any problem
but while write with timer i got this error .....
any one please help me....
Here is my code..
Java Code:import java.io.*; import java.util.*; import javax.comm.*; import java.util.Timer; import java.util.TimerTask; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; String messageString = "AT+CMGL=" + '"' + "REC READ" + '"' + '\r' + '\n'; InputStream inputStream; //public OutputStream outputStream; OutputStream outputStream; public SerialPort serialPort; Thread readThread; private int times = 0; //Thread writeThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); Timer timer = new Timer("Printer"); MyTask t = new MyTask(); timer.schedule(t, 0, 2000); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM9")) { // if (portId.getName().equals("/dev/term/a")) { SimpleRead reader = new SimpleRead(); } } } } public SimpleRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {System.out.println(e);} try { inputStream = serialPort.getInputStream(); outputStream = serialPort.getOutputStream(); } catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {System.out.println(e);} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {System.out.println(e);} readThread = new Thread(this); readThread.start(); } public void run() { try { /*//private int times = 0; times++; if (times <= 15) { outputStream.write(messageString.getBytes()); } else { System.out.println("Timer stops now..."); //Stop Timer. t.cancel(); }*/ Thread.sleep(20000); } catch (InterruptedException e) {System.out.println(e);} } public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) { case SerialPortEvent.BI: case SerialPortEvent.OE: case SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: case SerialPortEvent.CTS: case SerialPortEvent.DSR: case SerialPortEvent.RI: //case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: byte[] readBuffer = new byte[200]; //String Str2 = "AT+CMGL=" + '"' + "REC UNREAD" + '"' ; try { while (inputStream.available() > 0) { int numBytes = inputStream.read(readBuffer); } System.out.print(new String(readBuffer)); /*if (outputStream.available() > 0) { outputStream.write(messageString.getBytes()); }*/ /*try { outputStream.write(messageString.getBytes()); } catch (IOException e) { System.out.println(e); }*/ //outputStream.writeBytes(Str2 + '\r' + '\n'); } catch (IOException e) {System.out.println(e);} //try //{ // outputStream.write(messageString.getBytes()); //} //catch (NullPointerException e) //{ // System.out.println(e); //} break; } } } class MyTask extends TimerTask { //OutputStream outputStream; //String messageString = "AT+CMGL=" + '"' + "REC READ" + '"' + '\r' + '\n'; //times member represent calling times. private int times = 0; //outputStream = serialPort.getOutputStream(); public void run() { times++; if (times <= 15) { /*try { outputStream.write(messageString.getBytes()); } catch (IOException e) { System.out.println(e); }*/ outputStream.write(messageString.getBytes()); System.out.println("Timer is alive..."); } else { System.out.println("Timer stops now..."); //Stop Timer. this.cancel(); } } }Last edited by quad64bit; 11-09-2012 at 05:11 PM. Reason: readability
- 11-03-2012, 01:42 PM #2
Re: unreported exception java.io.ioexception must be caught or declared to be thrown
Moved from Advanced Java.
Please post your code in [code][/code] tags so that it retains its formatting.
Go through the Exceptions and Basic I/O parts of this tutorial: Trail: Essential Classes (The Java™ Tutorials)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Urgent help - unreported exception java.sql.SQLException; must be caught or declared
By Priya5791 in forum New To JavaReplies: 2Last Post: 04-27-2012, 02:43 PM -
Error:unreported exception java.io.IOexception; must be caught or declared to be thro
By misterbreadcrum in forum New To JavaReplies: 7Last Post: 03-21-2012, 08:07 AM -
unreported exception java.io.IOException
By fluffaykitties in forum New To JavaReplies: 11Last Post: 03-07-2011, 01:59 AM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-14-2011, 11:39 PM -
Error: unreported exception java.io.IOException; ??
By jonsamwell in forum New To JavaReplies: 5Last Post: 08-24-2008, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks