28.12.2013 Views

Serial Programming - upload.wikimedia....

Serial Programming - upload.wikimedia....

Serial Programming - upload.wikimedia....

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Serial</strong> Java<br />

end-of-file indication to, for example, return to the modem command mode.<br />

import javax.comm.*;<br />

...<br />

InputStream is = port.getInputStream();<br />

BufferedOutputStream out = new BufferedOutputStream(new<br />

FileOutputStream("out.dat"));<br />

/**<br />

* Listen to port events<br />

*/<br />

class FileListener implements <strong>Serial</strong>PortEventListener {<br />

/**<br />

* Handle serial event.<br />

*/<br />

void serialEvent(<strong>Serial</strong>PortEvent e) {<br />

<strong>Serial</strong>Port port = (<strong>Serial</strong>Port) e.getSource();<br />

//<br />

// Discriminate handling according to event type<br />

//<br />

switch(e.getEventType()) {<br />

case <strong>Serial</strong>PortEvent.DATA_AVAILABLE:<br />

}<br />

//<br />

// Move all currently available data to the file<br />

//<br />

try {<br />

int c;<br />

while((c = is.read()) != -1) {<br />

out.write(c);<br />

}<br />

} catch(IOException ex) {<br />

...<br />

}<br />

break;<br />

case ...:<br />

...<br />

break;<br />

...<br />

}<br />

if (is != null) is.close();<br />

if (port != null) port.close();<br />

6.2.6 Handling multiple Ports in one Application<br />

6.2.7 Modem Control<br />

JavaComm is strictly concerned with the handling of a serial interface and the transmission of<br />

data over that interface. It does not know, or provide, any support for higher-layer protocols,<br />

e.g. for Hayes modem commands typically used to control consumer-grade modems. This is<br />

simply not the job of JavaComm, and not a bug.<br />

104

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!