Charlipaedia's Posts
Nairaland Forum › Charlipaedia's Profile › Charlipaedia's Posts
1 (of 1 pages)
Hello guys, am trying to read a data input from an Avery Weighing Scale into an application but I keep getting an output like this: ***** plus small boxes. Please I urgently need to rectify this challenge urgently can anyone please help. Thank you. Below is the code am using for this: import gnu.io.*; import java.io.*; import java.util.*; public class USBTesting implements Runnable, SerialPortEventListener { static CommPortIdentifier portId; static Enumeration portList; InputStream inputStream; SerialPort serialPort; Thread readThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); portId = (CommPortIdentifier) portList.nextElement(); //System.out.println("Welcome: "+portId); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM4" ) {// if (portId.getName().equals("/dev/term/a" ) {USBTesting reader = new USBTesting(); System.out.println("Welcome" ;} else{ System.out.println("Port not found" ;} } } } public USBTesting() { try { serialPort = (SerialPort) portId.open("USBTesting", 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(9600, 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 { 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[8]; String str = ""; int NEW_LINE_ASCII = 10; String logText = ""; try { int numBytes = 0; while (inputStream.available() > 0) { numBytes = inputStream.read(readBuffer); //System.out.println("Data" ;} System.out.println(new String(readBuffer,0, numBytes, "UTF-8" );//System.out.println(str); } catch (IOException e) {System.out.println(e);} break; } } } |
Please can also get a mail for past pwc question chidicharles@outlook.com thanks in anticipation |
Please I also need the pwc past question chidicharles@outlook.com thanks |
1 (of 1 pages)
) {