Note that there are some explanatory texts on larger screens.

plurals
  1. POread from commport doesn't return full result on java
    primarykey
    data
    text
    <p>I was trying to read from a com port using the rxtx API. The com port was connected to a micro controller, so that every time I pressed a button on the micro controller board, it will return me a series of byte number counting from 0x01 to 0xff. I wanted to display these number on my java console, but it seemed to be able to read up to 0x40. All the other byte numbers after seemed to get lost. I was pretty sure the microcontroller was working well, since I tested in another terminal program which then gave out the correct result. So I suspect there is something wrong with my inputstream. Is there anyone who could kindly help me locate the problem? Following is my java code, which is a typical serial com port read example code I found from google.</p> <pre><code>import gnu.io.CommPortIdentifier; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import gnu.io.UnsupportedCommOperationException; import java.io.*; import java.util.*; public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM7")) { // if (portId.getName().equals("/dev/term/a")) { SimpleRead reader = new SimpleRead(); } } } } public SimpleRead() { try { serialPort = (SerialPort) portId.open(this.getClass().getName(), 2000); } catch (PortInUseException e) {System.out.println(e);} try { inputStream = serialPort.getInputStream(); } catch (IOException e) {System.out.println(e);} try { serialPort.addEventListener(this); } catch (TooManyListenersException e) {System.out.println(e);} serialPort.notifyOnDataAvailable(true); try { serialPort.setSerialPortParams(115200, 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 { //System.out.println("1"); 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[4049]; try { while (inputStream.available() &gt; 0) { int numBytes = inputStream.read(readBuffer); } for(Byte bytenum: readBuffer)System.out.print(Integer.toHexString(bytenum)+" "); } catch (IOException e) {System.out.println(e);} break; } if (serialPort != null) { try { // close the i/o streams. inputStream.close(); } catch (IOException ex) { System.out.println(ex); } // Close the port. serialPort.close(); } } </code></pre> <p>}</p> <p>And Here is the partial result ( note that byte number after 40 is not read successfully) 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 0 0 0 0 0 0 0</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    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.
    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