Sunday, 15 May 2011

java - How Can I found out what is length of recieved message on serial port? -



java - How Can I found out what is length of recieved message on serial port? -

inputstream inputfromport;

seek { inputfromport = serial.getinputstream(); //serialport b=new byte[20]; inputfromport.read(b); reading=new string(b,"utf-8"); system.out.println(reading.length()); system.out.println("new message: " + reading); inputfromport.close(); serial.close(); }

result: new message: hello, world! -> ok 6 boxes symbols ->(which can't re-create here)i know appear 'cause length of b larger "hello,world!",it great if somehow know size of recieved message can initialize byte array b on size

check homecoming value of inputfromport.read(b);:

int readlength = inputfromport.read(b);

then have create string bytes received, part of byte array written phone call read():

string reading = new string(b, 0, readlength, "utf-8");

this way won't have "boxes symbols" after the "hello, world!"

java buffer inputstream

No comments:

Post a Comment