java - inputstream.available() is 0 always -
i have no idea of happening code. getting no errors , no response well. writing data serialport , waiting response activating port.notifyondataavailable(true);
event not triggered , inputstream.available() returns 0 always. might wrong ? using rxtx in linux.
edit
package testconn; import forms_helper.global_variables; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.printstream; import java.io.unsupportedencodingexception; import java.util.logging.level; import java.util.logging.logger; import javax.comm.*; import java.util.*; /** check each port see if open. **/ public class openport implements serialporteventlistener { static enumeration portlist; static commportidentifier portid; static string messagestring; public static serialport serialport; static outputstream outputstream; inputstream inputstream; static boolean outputbufferemptyflag = false; private bufferedreader is; private printstream os; public void open() { enumeration port_list = commportidentifier.getportidentifiers(); while (port_list.hasmoreelements()) { // list of ports commportidentifier port_id = (commportidentifier) port_list.nextelement(); if (port_id.getname().equals("/dev/ttys1")) { // attempt open try { serialport port = (serialport) port_id.open("portlistopen", 20000); system.out.println("opened successfully:"+port); try { int baudrate = 9600; // port.setserialportparams( baudrate, serialport.databits_7, serialport.stopbits_1, serialport.parity_even); port.setdtr(true); port.setflowcontrolmode(serialport.flowcontrol_none); system.out.println("properties set"); } catch (unsupportedcommoperationexception e) { system.out.println(e); } try { //input = new serialreader(in); port.addeventlistener(this); system.out.println("listeners attached" + this); } catch (toomanylistenersexception e) { system.out.println("too many listeners"); } port.notifyondataavailable(true); //port.notifyonoutputempty(true); //sendmessage(port,"@pl"); //port.close (); try { = new bufferedreader(new inputstreamreader(port.getinputstream())); } catch (ioexception e) { system.err.println("can't open input stream: write-only"); = null; } try { os = new printstream(port.getoutputstream(), true); } catch (ioexception ex) { logger.getlogger(openport.class.getname()).log(level.severe, null, ex); } try { inputstream = port.getinputstream(); system.out.println("inputstream" + inputstream.available()); outputstream = (outputstream) port.getoutputstream(); os = new printstream(port.getoutputstream(), true, "us-ascii"); } catch (ioexception e) { system.out.println(e); } //set created variables global variables global_variables.port = port; global_variables.inputstream = inputstream; system.out.println(inputstream); system.out.println(outputstream); global_variables.outputstream = outputstream; global_variables.os = os; } catch (portinuseexception pe) { system.out.println("open failed"); string owner_name = port_id.getcurrentowner(); if (owner_name == null) { system.out.println("port owned unidentified app"); } else // owner name not returned correctly unless // java program. { system.out.println(" " + owner_name); } } } } } public static void sendmessage(serialport port, string msg) { if (port != null) { system.out.println(msg); try { byte[] bytes = msg.getbytes("us-ascii"); try { global_variables.outputstream.write(bytes); system.out.println(bytes.length); global_variables.outputstream.flush(); } catch (ioexception ex) { logger.getlogger(openport.class.getname()).log(level.severe, null, ex); } } catch (unsupportedencodingexception ex) { logger.getlogger(openport.class.getname()).log(level.severe, null, ex); } system.out.println("opened successfully:"+msg.getbytes()); //global_variables.outputstream.write(msg.getbytes()); //global_variables.outputstream.flush(); //global_variables.os.print(msg); system.out.println(global_variables.outputstream); try { thread.sleep(2000); // sure data xferred before closing system.out.println("read called"); //simpleread read = new simpleread(); //int read = global_variables.inputstream.read(); //system.out.println("read call ended"+read); } catch (exception e) { } } } public void serialevent(serialportevent event) { system.out.println(event.geteventtype()); string line; try { line = is.readline(); if (line == null) { system.out.println("eof on serial port."); system.exit(0); } os.println(line); } catch (ioexception ex) { system.err.println("io error " + ex); } 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: system.out.println("event.geteventtype()"); break; * */ case serialportevent.data_available: system.out.println("inside event handler data available"); byte[] readbuffer = new byte[20]; try { while (inputstream.available() > 0) { int numbytes = inputstream.read(readbuffer); } system.out.print(new string(readbuffer)); system.exit(1); } catch (ioexception e) { system.out.println(e); } break; } } } // portlistopen
i opening port on main method , sending message on button click event inside application.
.available()
can not used in inter-process communication (serial included), since checks if there data available (in input buffers) in current process.
in serial communication, when send messaga , call available()
0 serial port did not yet reply data.
the solution use blocking read()
in separate thread (with interrupt()
end it):
thread interrupt not ending blocking call on input stream read
Comments
Post a Comment