Note that there are some explanatory texts on larger screens.

plurals
  1. POTesting Java Serial Port Program
    primarykey
    data
    text
    <p>I have done the code to read byte streams from serial ports.The problem is I am unable to test the code as I dont have a device that interacts with these ports..</p> <p><strong>I have already used emulator</strong> but on running the code and the emulator <strong>nothing happens.There is no progress and no error.</strong></p> <p><strong>Here is the code</strong></p> <pre><code>class SimpleRead implements Runnable, SerialPortEventListener{ CommPortIdentifier portId; Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; /** * Method declaration * * * @param args * * @see */ public void main(String[] args) { boolean portFound = false; String defaultPort = "/dev/term/a"; if (args.length &gt; 0) { defaultPort = args[0]; } portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals(defaultPort)) { jTextArea1.setText("Found port: "+defaultPort); portFound = true; SimpleRead reader = new SimpleRead(); } } } if (!portFound) { jTextArea1.setText("port " + defaultPort + " not found."); } } /** * Constructor declaration * * * @see */ public SimpleRead() { try { serialPort = (SerialPort) portId.open("SimpleReadApp", 2000); } catch (PortInUseException e) {} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); } catch (UnsupportedCommOperationException e) {} readThread = new Thread(this); readThread.start(); } /** * Method declaration * * * @see */ public void run() { try { Thread.sleep(20000); } catch (InterruptedException e) {} } /** * Method declaration * * * @param event * * @see */ 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[20]; try { while (inputStream.available() &gt; 0) { int numBytes = inputStream.read(readBuffer); } jTextArea1.setText(new String(readBuffer)); } catch (IOException e) {} break; } } } </code></pre> <p><strong>Note:</strong> I am using javax.comm and Netbeans IDE. Thank You</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload