java - Getting unrecognized characters from inputStream -
i reading data weigh bridge using java comm api
. below code:
import java.io.*; import java.util.*; import javax.comm.*; public class parrot 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("com1")) { parrot reader = new parrot(); } } } } public parrot() { try { serialport = (serialport) portid.open("simplereadapp", 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() { system.out.println("in run method"); try { thread.sleep(20000); } catch (interruptedexception e) {system.out.println(e);} } public void serialevent(serialportevent event) { switch(event.geteventtype()) { case serialportevent.data_available: byte[] readbuffer = new byte[20]; try { int availablebytes = inputstream.available(); system.out.println(availablebytes+" bytes available read"); while (inputstream.available() > 0) { int numbytes = inputstream.read(readbuffer); } system.out.print(new string(readbuffer)); } catch (ioexception e) {system.out.println(e);} break; } } }
below 2 screen shots, 1 hyper terminal , 1 above java program:
hyper terminal (with terminal font)
java program
i want same characters hyper terminal.
it doesn't correctly reading stream. have
byte[] readbuffer = new byte[20]; try { int availablebytes = inputstream.available(); system.out.println(availablebytes+" bytes available read"); while (inputstream.available() > 0) { // overwrites bytes previous read int numbytes = inputstream.read(readbuffer); } // doesn't give buffer end points , doesn't give encoding system.out.print(new string(readbuffer)); } catch (ioexception e) {system.out.println(e);}
should be
byte[] readbuffer = new byte[20]; int bytesread; try { int availablebytes = inputstream.available(); system.out.println(availablebytes+" bytes available read"); while ((bytesread = inputstream.read(readbuffer)) != -1) { system.out.print(new string(readbuffer,0,bytesread,charset.forname(encoding)); } } catch (ioexception e) {system.out.println(e);}
where encoding
string
proper encoding, eg "utf-8".
if want single string
has of content:
byte[] readbuffer = new byte[20]; int bytesread; stringbuilder sb = new stringbuilder(100); try { int availablebytes = inputstream.available(); system.out.println(availablebytes+" bytes available read"); while ((bytesread = inputstream.read(readbuffer) != -1) { sb.append(new string(readbuffer,0,bytesread,charset.forname(encoding)); } system.out.println("the content -> " + sb); } catch (ioexception e) {system.out.println(e);}
edit: answering comment here: if want separate line, need add new-line stringbuilder
within loop. boxes because not have proper encoding or content isn't valid character data. don't know how programmatically determine encoding. it's know beforehand. other problem may constants use when setting com port stream
serialport.setserialportparams(9600, serialport.databits_8, serialport.stopbits_1, serialport.parity_none);
are sure correct?
fyi: list of encodings.you'll want use value middle column ("canonical name java.io api , java.lang api").
Comments
Post a Comment